Array An array is a group (or collection) of same data types. For example an integer array holds the elements of integer types while a float array holds the elements of float types. Why we need Array in C Programming? Consider a scenario where you need to find out the average of 100 integer numbers entered by user. In C, you have two ways to do this: 1) Define 100 variables with int data type and then perform 100 scanf() operations to store the entered values in the variables and then at last calculate the average of them. 2) Have a single integer array to store all the values, loop the array to store all the entered values in array and later calculate the average. Which solution is better according to you? Obviously the second solution, it is convenient to store same data types in one single variable Array • It is a group of elements or collection of variables of the same data type. • Array data type can be: • 1. numerical array – all numerical values like integer, float that can store numbers. • 2. character array- it means store characters. Any characters found in the keyboard small and capital letters, number from 0 to 9, and special characters. Array have dimensions • 1. one dimensional array it represented by [ ] square brackets--------called subscript one square brackets contain one subscript Ex. grade[ ]; • 2. two dimensional array – it contain 2 subscripts • Ex. grade [ ] [ ] • 3. multi dimensional array – it contain 3 or more subscripts • Note. Using these dimensions of array depends upon on the problem encountered. Syntax of One dimensional Array data type array name [size]; Maximum number of elements Example: declaration int a[4]; Called index value start from 0 to 0, 1, 1 2, 3 called elements under the same name (value) a[0], a[1], a[2], a[3] Initialization of Arrays • Syntax: • data type array_name[size] ={ value1, value2, ….}; • Example • int grade[4]={ 75,65,90,88}; / int grade [ ] = { 75,65,90,88}; • grade [0] = 75 • grade [1] = 65 assess individual elements of an array • grade [2] = 90 can store number of elements in • grade [3] = 88 the array size • Initialization of Array It can be given initial values during the declaration. This is called array initialization • int grade [3]; scanf(“%d,%d,%d”,&grade[0], &grade[1],&grade[2]); • Or we can use a loop statement if multiple variable should be use like • • for(i=0; i<3; i++) { scanf (“%d”,&grade[i]); } How to print/ display an array elements • int grade [3]; • printf(“%d,%d,%d”,%grade[0], &grade[1],&grade[2]); • Or we can use a loop statement if multiple variable should be use like • • • • for(i=0; i<3; i++) { printf (“%d”,grade[i]); } Write a program that will print the values of the following array: int(5)={89,67,54,90,39} #include <stdio.h> #include <stdlib.h> /* run this program using the console pauser or add your own getch, system("pause") or input loop */ int main(int argc, char *argv[]) { int n[5]={89,67,54,90,39}; int x; for (x=0; x<=4; x++) printf("\nElement %d is %d \n\n", x, n[x]); return 0; }