int 25 30 40 size of the array score is automatically calculated

Int 25 30 40 size of the array score is automatically

This preview shows page 8 - 13 out of 14 pages.

int scores[]  = {20, 10, 25, 30, 40};  // size of the array score is             // automatically  calculated as 5
9 Array Subscripts We use subscripts/indices to differentiate between the individual array elements We can use any expression of type int as an array subscript. However, to create a valid reference, the value of this subscript must lie between 0 and one less than the array size. It is essential that we understand the distinction between an array subscript value and an array element value. int x[2]; int y = 1; x[y] = 5; The subscript is y (which is 1 in this case), and the array element value is 5 C compiler does not provide any array bound checking. As a programmer it is your job to make sure that every reference is valid (i.e. it falls within the boundary of the array).
10 Accessing Array elements 1. point[1] // the 2 nd element of array point is accessed 2. point[9] = 20; // the 10 th element of array point is assigned // the value 20 3. We can use a loop to access all the elements of an array Example: Adding the values of all array elements Two alternative style for loops for ( i = 0; i < ARRAY_SIZE; i++)               sum += a[i];  for ( i = 0; i <=  ARRAY_SIZE  -1; i++)           sum += a[i];  Note : The array element is a single valued variable of the corresponding type and can be manipulated as a variable of that type.
11 Example 1 /* Reads five grades and print them */ #include<stdio.h> #define SIZE 5 int main(void) { double grades[SIZE] ; // array declaration int i ; printf("Enter five grades to store in array : \n"); printf("*********************************\n\n"); for (i = 0; i < SIZE; ++i) // loop to read the five grades into the array { printf ("Enter the %d element of array : ", i ) ; scanf ( "%lf", &grades[i] ) ; } printf("\n"); for (i = 0; i < SIZE; ++i) // loop to display five grades stored in the array printf ("The %d th element of array is %f\n", i, grades[i]) ; system("pause"); return 0; }
12 Example 2 /* Reads data into two arrays and subtract their corresponding elements, storing the result in another array. */ #include<stdio.h>

You've reached the end of your free preview.

Want to read all 14 pages?

• Spring '10
• baleh
• Array, #include, #define, Array Array Initialization