Array in C programming
Why do we need an array?
/*program to read and display average age of 7 students*/
#include<stdio.h>
#include<conio.h>
void main()
{
int age1,age2,age3,age4,age5,age6,age7;
float avg;
clrscr();
printf("enter the age1\n");
scanf("%d",&age1);
printf("enter the age2\n");
scanf("%d",&age2);
printf("enter the age3\n");
scanf("%d",&age3);
printf("enter the age4\n");
scanf("%d",&age4);
printf("enter the age5\n");
scanf("%d",&age5);
printf("enter the age6\n");
scanf("%d",&age6);
printf("enter the age7\n");
scanf("%d",&age7);
avg=(age1+age2+age3+age4+age5+age6+age7)/7;
printf("the average age of seven student is %", avg);
getch();
}
Output:
Enter the age1
23
Enter the age2
20
Enter the age3
21
Enter the age4
22
Enter the age5
16
Enter the age6
17
Enter the age7
18
The average age of seven students is 19.00
The above program reads the age of seven students only so we use seven different variables if we have to enter the age of 100 students. Would we declare the 100 different variables then? Of course not. So clearly, we have a convenient way to refer to such collections of similar data elements with the help of an array. And this problem is solved by the use of an array by providing a way to refer to individual items in a collection by using the same variable name.
Types of Array
One Dimensional Array
Declaration of one Dimensional Array
Here data_type denotes any valid C data types and array_name denotes the name of the array and it can be any valid C identifier. The size of the array specifies the number of elements that can be stored in the array. It may be a positive integer constant or constants integer expression.
Here are some examples of a valid array declaration:
int age[100];
float salary[500];
char grade[20];
Following are some of the invalid array declarations:
int value[0]; //zero subscripted array is not supported
int marks[0.15] // subscript value must be an integer
int n[-40]; // subscript value must be non-negative
Accessing array elements
Let us take an array:-
The elements of this array are:
arr[0], arr[1], arr[2], arr[3], arr[4]
The subscript can be any expression that yields an integer value. It can be any constant, integer variable, integer expression or return value(int) from a function call. For example, if i and j are integer variables then these are some valid subscripted array elements-
arr[3];
arr[i];
arr[i+j];
Processing array elements
1. Reading values in arr[10]
for(i=0;i<10;i++)
scanf(“%d”,&arr[i]);
2. Displaying values of arr[10]
for(i=0;i<10;i++)
printf(“%d”,arr[i]);
3. Adding all elements of arr[10]
sum =0;
for(i=0;i<10;i++)
sum+=arr[i]; or sum=sum+arr[i];
/*program to input values into an array and display them*/
#include<stdio.h>
#include<conio.h>
void main()
{
int arr[10],i;
clrscr();
for(i=0;i<10;i++)
{
printf("Enter the value for arr[%d] :",i);
scanf("%d",&arr[i]);
}
printf("The array elements are :\n");
for(i=0;i<10;i++)
printf("%d\t",arr[i]);
printf("\n");
getch();
}
Output:
Enter the value for arr[0]:10
Enter the value for arr[1]:78
Enter the value for arr[2]:90
Enter the value for arr[3]:67
Enter the value for arr[4]:60
Enter the value for arr[5]: 50
Enter the value for arr[6]:34
Enter the value for arr[7]:23
Enter the value for arr[8]:12
Enter the value for arr[9]:100
The array of elements are:
10 78 90 67 60 50 34 23 12 100
Initialization of array
Each array element can be initialized when an array is declared. The initial values must appear in the order in which they will be assigned to the individual array elements, enclosed in curly braces {} and separated by commas. The syntax for initialization of array is:-
data_type array_name[size]={ value1, value2, value3…………..valueN};
Example is:
int digit[5]={1, 2, 3, 4, 5};
float salary[4]={ 200.50, 69.90, 8.6, 7.87};
char color[3]={‘R’, ‘G’, ‘B’};
Here the first one has five element values five of which are of integer types. The second one has four element values all are of float types and the last one has three elements all are of char types.
Multidimensional array (two-dimensional arrays)
Declaration of 2D array
data_type array_name[row_size][column_size];
Example:
int mat[3][2];
So the above example declaration is a 2D array of integer type. This integer array has been named mat and it can hold up to 6 elements (3rows x 2columns).
|
0 |
1 |
0 |
[0][0] |
[0][1] |
1 |
[1][0] |
[1][1] |
2 |
[2][0] |
[2][1] |
In the above example, the position of each element in the 2D array is given.
No comments:
Post a Comment