Tuesday, 25 December 2012

Array in C Tutorial

An array is a collection of similar elements. These similar elements could be all ints, or all floats, or all chars, etc. Usually, the array of characters is called a ‘string’, whereas an array of ints or floats is called simply an array. Remember that all elements of any given array must be of the same type. i.e. we cannot have an array of 10 numbers, of which 5 are ints and 5 are floats.

Program Using Array
Let us try to write a program to find average marks obtained by a class of 30 students in a test.


main ( )
{
  int avg, sum = 0 ;
  int i ;
  int marks[30] ; /* array declaration */

  for ( i = 0 ; i <= 29 ; i++ )
  {
    printf ( "\nEnter marks " ) ;
    scanf ( "%d", &marks[i] ) ; /* store data in array */
  }

  for ( i = 0 ; i <= 29 ; i++ )
  {
    sum = sum + marks[i] ; /* read data from an array*/
  }

  avg = sum / 30 ;
  printf ( "\nAverage marks = %d", avg ) ;
}


No comments:

Post a comment

LinkWithin

Related Posts Plugin for WordPress, Blogger...