WHAT'S NEW?
Loading...

Array in C Programming (With Examples)

An array is a group of related data items that share a common name. In other words, an array is a data structure that store a number of data items as a single entity. The individual data items are called elements and all of them have same data types. Array is used when multiple data items that have common characteristics are required. In other words, an array is a collection of individual data elements that is ordered, fixed in size and homogeneous. Suppose we have 30 numbers of type integer and we have to sort them in ascending or descending order. If we have no array, we have to define 30 different variables like n1, n2, n3, ..... n30 of type int to store these twenty numbers which will be possible but inefficient. If the number of integer increases the number of variable will also be increased and defining different variables for different numbers will be impossible and inefficient. In such situation where we have multiple data items of same type to be stored, we can use array.

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(); 
}
Output
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(); 
}
Output
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

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(); 
}
Output
Enter any 10 Integer Numbers :
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

  1. 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. 
  2. 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. 
  3. The element number or array index in the array plays an important role for calling each element. 
  4. Any particular element of an array can be modified separately without disturbing other elements. 
  5. 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