by Dinesh Thakur Category: Array

An array is similar to a list in which the objects are of the same type and stored in sequential memory blocks; this is the only relationship between the elements of an array. The input/output of values of elements of an array cannot be done as whole of array, but is carried out element by element. This may be done by accessing each element by its index value. Therefore, for large arrays, either a for loop or a while loop may be used for accessing each element for its input/output. For example, if elements of an array Array [4] having 4 elements are to be processed for input /output by user of a program, the program code may be written as follows:

int i,j;

int Kim[4];

printf("Write 4 elements of Ele");

for (int i = 0; i<4; i++)

scanf("%d", & Ele[i]); //for input of array elements.

for(j =0; j<4; j++) // for output of array elements.

printf( "%d\t" , Ele[j];

Illustrates input/output of an array with the help of a for loop

void main()
{
  int A [4] , Sum = 0;
  int i,j , k;
  clrscr();
  printf("Write the four elements of array A:");
  for(i =0; i<4; i++) // input of array elements
    scanf("%d",&A[i]);
    for ( j=0; j<4; j++) //output of array elements
    printf("A(%d) = %d, ", j, A[j] );
    for ( k =0; k<4; k++)
      Sum+= A[k]; //manipulation with array elements
      printf ( "\n Sum = %d\n",Sum);
}

 

        C Program input/output of an array with the help of a for loop



About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.



Related Articles