Array

Array is like a variable that can store many numbers of values but same type for example many numbers of integer type values can store in a variable.

 Need of array

Suppose you want to store result total marks of 100 student.Then you need to declare 100 numbers of variables.In this way program size increase and run time of program increases.But by using of array value of 100 students result can store in single variable.

Array declaration 

Syntax of array declaration

Data types variable_name [size];

Example    float  result[5]; 

You can see here we have to declared result of 5 student floating data types.Size is 5 taken because we want to store result marks of 5 students.Size of array may be greater than number of value or equal but not less that we want to store.

 Memory concept

Let us assuming to understand how to memory takes place by array.If we want to store 5 students result then we need to give size 5 during declaration of an array.Size of array may be equal or greater than numbers of values but not be less than numbers of values.

float result[5];

                        result

150

177

180

144

158

0               1             2           3          4

 In this case 5 block take space in memory.Block number always started from 0.Here in given example block numbers started from 0 to 4.These block numbers are called indices or index number.The size of each block taking 4 bytes of memory.

 Notes 

1.) Size of each block depend on data type.

2.) Here size of array is 20 because of (size of array=numbers of blocks*size of each block).

3.)  result is array name above the table.

 Array initialization

We can initialize an array with declaration in given syntax

 1.)  float result[5]={150,177,180,144,158};

                           result

150

177

180

144

158

0                  1            2         3           4

 

2.)  float result[ ]={150,177,180,144,158};

                       result

150

177

180

144

158

    0            1              2          3          4

Here we have not declared size but this syntax is correct because compiler read size that depend on numbers of initialized element.

 Accessing arrays elements

Element can be access by using index numbers.

 float result[5 ]={150,177,180,144,158};

 

                           result

150

177

180

144

158

    0            1             2          3          4

result[0] give 150.000000

Result[2] give 180.000000 

Example : program to access array element.

#include<stdio.h>

int main()

{

float result[5]={150,177,180,144,158};

printf("%f %f",result[0],result[2]);

return 0;

}

Output : 150.000000  180.000000

Run this code

 Declaration in deep

Correct and incorrect syntax of declaration of array in program 

int a[ ];      // incorrect

int a[ 5];     //correct

int a[5]={150,177,180,144,158};  //correct

int a[ ]={150,177,180,144,158};   //correct

int a[5]={150,177,180,144};   //correct

int a[5]={150,177,180,144,158,132,144};  //incorrect

At declaration time if numbers of values less than number of indices then remaining indices automatically fill up with zeros in case initialization.

Example

#include<stdio.h>

int main()

{

int a[5]={121,122,123};

printf("%d,%d,%d",a[1],a[2],a[4]);

return 0;

}

Output : 122,123,0

Run this code

At declaration time if there is not initialization and if we want to assign values in some indices then remaining indices fill up garbage values.

Example

#include<stdio.h>

int main()

{

int a[5];

a[0]=5,a[2]=9;

printf("%d,%d,%d",a[1],a[2],a[4]);

return 0;

}

Output : 4202016,9,35

Run this code

 Notes : garbage values may be any number.

User taking input

For taking input by user we will use index numbers.

Example 1 

#include<stdio.h>

int main()

{

int a[5];

printf("Enter the values:");

scanf("%d",&a[0]);

scanf("%d",&a[1]);

scanf("%d",&a[2]);

scanf("%d",&a[3]);

scanf("%d",&a[4]);

printf("%d,%d,%d,%d,%d",a[0],a[1],a[2],a[3],a[4]);

return 0;

}

Output : Enter the value: 1

                      2

                      3

                      4

                      5

1,2,3,4,5

Run this code

Example 2

#include<stdio.h>

int main()

{

int i;

int a[5];

printf("Enter the values:");

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

{

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

}

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

{

printf("%d ",a[i]);

}

return 0;

}

Output : Enter the value: 1

                      2

                      3

                      4

                      5

1 2 3 4 5

Run this code

Any method can be use that are given but second is more comfort to write.When you want to enter 100 values then second is more comfort so you will see every where in programs second methods use more.

Note

1.) During user taking input it necessary to give size of array otherwise program give error.

2.) Size may be greater or equal but size is necessary to write in program when input taking by the user.

Value updating or change

The values given in initialization can be update or change with help of index numbers.

Example

#include<stdio.h>

int main()

{

int arr[5]={1,2,3,4,5};

//see output before updating of value

printf("Values before updating:%d,%d",arr[0],arr[2]);

arr[0]=5;

arr[2]=4;

//see output after updating of value

printf("\nValues after updating:%d,%d",arr[0],arr[2]);

return 0;

}

Values before updating:1,3

Values after updating:5,4

 Run this code

 <<Prev                                 Next>>