One Dimensional Array

 An Array is a collection of similar type of data.
 One Dimensional array consists of one row and multiple columns or multiple rows and one column.
 Array Initialization:   
int  array[5]={10,20,30,40,50};
float a[8]= {1.5,3.6,4.8,5.5,8.2};

   In the first integer array, word array is the variable name ( we can use any variable name), [5] is the size of the array, and 10,20…..50 are values initialized to the array.                    

  •  We can also initialize in following was
  •  int array[ ]={10,20,30,40,50};
  •  We can not write an array in the following way if we write in the following manner, it will show an error.
      1. int array[3]={10, 20,30,40,50};
// here its exceeding the size of array,  array size is 3 and values stored are 5.

      2. int array[ ];  

          // here no size is declared that’s why error will generate.

      3. int i=5;i

           int a[i] ;

// error will occur because constant expression is required.

  •  We can also write array in the following way,

           int a[5]={10,20,30} 

// here array size is 5 and values stored are 3, in other two spaces zero value will be stored. 

Let us take an example and perform some operations.

Take an array of marks of 10 students size 10. 

int students[ ]={60,50,75,82,90,92,65,88,76,85};

array-c

  • For better understanding, we have created a table above, let us consider the columns in the table as the Memory boxes where the values are stored.
  •  The values stored in each memory boxes are in continuous memory order.
  • The numbers 100, 102……….118  given are the address given for each memory allocation, and the address given is just for understanding, the address can be any number. Here we have used integer data type. Hence, each memory box requires 2 bytes of memory. Each box is allocated with 2 bytes of memory for each box.
  • The numbers 0,1,2……9 given are the subscript, with the help of we can fetch the value present under any of any memory box.
  •  For example,   student[5]=92.
  • Under each memory box, marks are stored as 60, 50, 75……….85, our main motive is those marks. 
If we want to print all the values than we have to use a loop

#include<stdio.h>

{
 int main()

int i, a[10]={60,50,75,82,90,92,65,88,76,85};

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

{

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

}

return 0;

}

Output::  60 50 75 82 90 92 65 88 76 85


 #include<stdio.h>

{
 int main()

int i, a[10]={60,50,75,82,90,92,65,88,76,85};

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

{

 printf(“%d ”,i);

}

return 0;

}

Output:: 0 1 2 3 4 5 6 7 8 9


#include<stdio.h>

{
 int main()

int i, a[10]={60,50,75,82,90,92,65,88,76,85};

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

{

 printf(“%u ”,a);

}

return 0;

Output ::  100 100 100 100 100 100 100 100 100 100 // base address for 10 times


 #include<stdio.h>

{
 int main()

int i, a[10]={60,50,75,82,90,92,65,88,76,85};

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

{

 printf(“%u ”,&a[i]);

}

return 0;

}

Output:: 100 102 104 106 108 110 112 114 116 118   // base  address of all the values

 Runtime Input in an array

In the Runtime input, we just declare an array in the code, and after the compilation of the program we feed the input values in an array

Demo program

Take the runtime input of marks of 10 students

#include<stdio.h>
int main()
{
 int student[10], i;
printf(" Enter the marks of ten students::");
for(i=0;i<10;i++)
{
 scanf("%d ", &student[i]);
}
    printf("\n print the marks::");
for(i=0;i<10;i++)
{
 printf("%d ", student[i]);
}
return 0;
}
 
output:
 Enter the marks of ten students::
 60 66 72 45 65 76 87 98 67 87
 
  print the marks::
   60 66 72 45 65 76 87 98 67 87
  
 
 



refer also::