One dimensional array

one dimensional: The one dimensional array is the simplest form of the array. And is used for the multiple values in a single variable.

Declaration syntax: data-type array-name[size];

e.g: int student[50];

in the above example we have array with the name of student and the size is 50. That means we can store 50 values in this array. And the computer reserves 50 location for the array.

Representation of the array values and can be assigned as follows:

student[0]=1001

student[1]=1002

student[2]=1003

student[3]=1004

.

.

.

student[49]=1050

we can also make array of character and float. Another example as follows:

char name[6];

name[0]=’j’;

name[1]=’a’;

name[2]=’t’;

name[3]=’I’;

name[4]=’n’;

name[5]=’\0’;

Initialization:

 a.      Compile time: the initialization is the same as we initialize the variables, but the difference only we gives the list of values. As follows:

Declaration syntax:   data-type array-name[size]={list of values};

e.g:  int student[5]={1,2,3};

in the above example the values 1,2,3 are initialized to array starting from 0. And the rest of locations are initialized to 0 automatically.

b.      Run time: we can assign the values of array at run time with the help of scanf() function.

For e.g:

suppose the name of array is student.

for(i=0;i<=5;i++)

{

scanf(“%d”,&student[i]);

}

Print the values of array:

e.g:      for(i=0;i<=5;i++)

{

printf(“%d”,student[i]);

}

 Program:

 PROGRAM TO SHOW SUM OF TEN ELEMENTS OF ARRAY AND TO FIND THEIR AVERAGE

#include<stdio.h>

#include<conio.h>

void main()

{

int i,a[10],sum=0;

float avg;

clrscr();

printf(“Enter ten elements in an array: \n”);

for(i=0;i<10;i++)

scanf(“%d”,&a[i]);

printf(“\nEntered elements are:\n”);

for(i=0;i<10;i++)

{

printf(“%d\n”,a[i]);

sum=sum+a[i];

}

printf(“\nSum of given elements is: %d”,sum);

avg=sum/10.0;

printf(“\nAverage of these elements is: %.2f”,avg);

getch();

}

 

Posted in C

Leave a Reply

Your email address will not be published. Required fields are marked *