Wednesday, July 4, 2018

C Programming Arrays

Array in C language is a collection or group of elements (data). All the elements of c array are homogeneous (similar). It has contiguous memory location.

C array is beneficial if you have to store similar elements. Suppose you have to store marks of 50 students, one way to do this is allotting 50 variables. So it will be typical and hard to manage. For example we can not access the value of these variables with only 1 or 2 lines of code.

Another way to do this is array. By using array, we can access the elements easily. Only few lines of code is required to access the elements of array.

Advantage of C Array

1) Code Optimization: Less code to the access the data.

2) Easy to traverse data: By using the for loop, we can retrieve the elements of an array easily.

3) Easy to sort data: To sort the elements of array, we need a few lines of code only.

4) Random Access: We can access any element randomly using the array.

Disadvantage of C Array

1) Fixed Size: Whatever size, we define at the time of declaration of array, we can't exceed the limit. So, it doesn't grow the size dynamically like LinkedList which we will learn later.

TYPES OF C ARRAYS:

There are 2 types of C arrays. They are,

  1. One dimensional array
  2. Multi dimensional array
    • Two dimensional array
    • Three dimensional array
    • four dimensional array etc…

1. ONE DIMENSIONAL ARRAY IN C:

Declaration of C Array

We can declare an array in the c language in the following way.





data_type array_name[array_size];



Now, let us see the example to declare array.





int marks[5];



Here, int is the data_type, marks is the array_name and 5 is the array_size.

Initialization of C Array

A simple way to initialize array is by index. Notice that array index starts from 0 and ends with [SIZE - 1].





marks[0]=80;//initialization of array
marks[1]=60;
marks[2]=70;
marks[3]=85;
marks[4]=75;



initialization of array

initialization of array in c language

C array example





#include<stdio.h>

int main()
{
int i;
int arr[5] = {10,20,30,40,50};

// declaring and Initializing array in C
//To initialize all array elements to 0, use int arr[5]={0};
/* Above array can be initialized as below also
arr[0] = 10;
arr[1] = 20;
arr[2] = 30;
arr[3] = 40;
arr[4] = 50; */

for (i=0;i<5;i++)
{
// Accessing each variable
printf("value of arr[%d] is %d \n", i, arr[i]);
}

}



COMPILE & RUN

OUTPUT:


value of arr[0] is 10
value of arr[1] is 20
value of arr[2] is 30
value of arr[3] is 40
value of arr[4] is 50





#include
int main(){
int i=0;
int marks[5];//declaration of array
marks[0]=80;//initialization of array
marks[1]=60;
marks[2]=70;
marks[3]=85;
marks[4]=75;
//traversal of array
for(i=0;i<5;i++){
printf("%d \n",marks[i]);
}//end of for loop
return 0;
}



Output


80
60
70
85
75

C Array: Declaration with Initialization

We can initialize the c array at the time of declaration. Let's see the code.





int marks[5]={20,30,40,50,60};



In such case, there is no requirement to define size. So it can also be written as the following code.





int marks[]={20,30,40,50,60};



Let's see the full program to declare and initialize the array in C.





#include
int main(){
int i=0;
int marks[5]={20,30,40,50,60};//declaration and initialization of array
//traversal of array
for(i=0;i<5;i++){
printf("%d \n",marks[i]);
}
return 0;
}



Output


20
30
40
50
60




Instagram



No comments:

Post a Comment