Array Declaration
To begin with, like other variables, an array needs to be declared so that the compiler will know what kind of an array and how large an array we want. In our program, we have done this with the statement.int marks[20];
Here, int specifies the type of the variable, just as it does with ordinary variables and the word marks specifies the name of the variable. The [20] however is new. The number 20 tells how many elements of the type int will be in our array. This number is often called the 'dimension' of the array. The ( [ ] ) tells the compiler that we are dealing with an array.
Example (Simple One)
#include<stdio.h> void main(){ int marks[5], t3; marks[0] = 10; marks[1] = 20; marks[3] = 12; marks[4] = 13; marks[5] = 40; t3 = marks[0] + marks[3] + marks[5]; printf("Value of t3 is %d",t3); getch(); }
Value of t3 is 62
As we have seen in the above program we can define multiple value or item by using single array variable marks;
Array Initialization
It is possible to initialize an array during declaration Let us now see how to initialize an array while declaring it. Following are few examples that demonstrate this.int num[5] = {10, 20, 40, 45, 56, 34};
int marks[] = {50, 80, 40};
While initializing an array dimension of an array is optional, like in second line.
Example
#include<stdio.h> void main(){ int num[6] = {10, 20, 40, 45, 56, 2}; int i; for(i=0; i<=5; i++){ printf("Array of Index num[%d] = %d \n",i, num[i]); } getch(); }
Array of Index num[0] = 10
Array of Index num[1] = 20
Array of Index num[2] = 40
Array of Index num[3] = 45
Array of Index num[4] = 56
Array of Index num[5] = 2
Array of Index num[1] = 20
Array of Index num[2] = 40
Array of Index num[3] = 45
Array of Index num[4] = 56
Array of Index num[5] = 2
Some One Dimension Arrays Examples
As we discussed above these all are one dimension array. In array we can get array elements from the user and store in the the array variable. Lets see the following example.Example: Program to get 10 integers from the user and print their sum on the screen.
#include<stdio.h> void main(){ int num[10]; int i, sum=0; printf("Enter any 10 Integer Numbers : \n"); for(i=0; i<10; i++){ scanf("%d",&num[i]); sum=sum+num[i]; } printf("The Sum is %d",sum); getch(); }
Enter any 10 Integer Numbers :
10
20
30
40
50
60
70
80
90
100
The Sum is 550
10
20
30
40
50
60
70
80
90
100
The Sum is 550
Example : WAP to get 10 numbers and print the greatest number.
Characteristics of Array
- The declaration int a[5] is nothing but creation of 5 variables of integer types in the memory. instead of declaring five variables for five values, the programmer can define them in the array.
- All th elements of an array share the same name, and they are distinguished from one another with the help of an element number or array index.
- The element number or array index in the array plays an important role for calling each element.
- Any particular element of an array can be modified separately without disturbing other elements.
- The single operations, which involves entire arrays, are not permitted in C, Thus if num and list are two similar array(i.e. same data type, dimension and size, then assignment operations, comparison operations etc., involving these two arrays must be carried out on an element-by-element basis.
0 comments:
Post a Comment