C Programming Tutorial
Computer Tutorial

Entering Data into an Array



Entering Data into an Array

Like other variables, an array can also be store the data. When we declare the array. The compiler can compile and reserved the space for array to store the data.

There are three way to enter the data into an array :

1. Assigning value to array element
2. Assign value to entire array
3. Using Loop

1. Assigning value to array element

We can assign an values to all array element one by one.
  marks[1] = 79  ;
  marks[2] = 88  ;
  marks[3] = 70  ;
  marks[4] = 56  ;
  marks[5] = 98  ;

2. Assign value to entire array

We can assign entire array at the time of decleration .
int num[6] = { 2, 4, 12, 5, 45, 5 }  ;
int n[ ] = { 2, 4, 12, 5, 45, 5 }  ;
float press[ ] = { 12.3, 34.2 -23.4, -11.3 }  ;

3. Using Loop

Here is the section of code that places data into an array:
for ( i = 1; i <= 5; i ++  )
{

printf("\n Enter marks ") ;
scanf(" %d ",   &marks[i] ) ;

}

The for loop causes the process of asking for and receiving a student’s marks from the user to be repeated 5 times. The first time through the loop, i has a value 0, so the scanf( ) function will cause the value typed to be stored in the array element marks[0], the first element of the array. This process will be repeated until i becomes 4. This is last time through the loop, there is no array element like marks[30].