Here We learn Array in C how to Declare Initialize and access array and operations on array.
Array is a data structure that can store elements of the same type.
Array is a collection of similar type elements
It stores the elements in a contiguous memory locations.
1 Why Array Required?
Array is used to store similar types of values under same name
We want to declare 100 integer variable
Instead of declaring 100 individual variables, such as
int number0, number1,number2,number3, ... …., number99;
you can declare one integer array variable such as
int number[100];
here number[100]
is an integer array of size 100, means this array can store 100 integer value.
Array indexing starts from index 0 to n-1. Means first integer number store in array number[0]
, second integer number store in array number[1]
, third integer number store in array number[2]
, and so on. Here 100th number store in array numbers[99]
.
2 How to Declare an Array in C?
Syntax of declare an array in C is as below
DataType arrayName [ arraySize ];
This is called a single-dimensional array.
DataType : any valid C data type like int, char, float, double, long
array Name: Any valid c identifier
arraySize: an integer constant greater than zero.
For Instance
int age[5];
long length[8];
float price[7];
double value[5];
char ch[5];
Above all are example of array.
- age is a integer array of size 5.
- length is a long array of size 8
- price is a float array of size 7
- value is double array of size 5
- ch is a character array of size 5
Size tells us that a array can store maximum element up to size.
Size must be a integer constant. based on size array occupies memory location for elements.
There are multi dimensional array.
3 Array Initialization
Giving initial value to array is know as array initialization.
A Initializing each member
We can initialize each element one by one as below.
int age[5]; //declaration
age[0]=21;
age[1]=24;
age[2]=25;
age[3]=27;
age[4]=26;
Array uses zero based indexing so first element is stored in age[0] and last element will store in size-1
position here age[4];
B Declare and initialize
general syntax is
DataType arrayName[size]={value1,value2,.............,valueN};
example
int age[5]={21,24,25,27,26};
value 21 will store in age[0]
and value 24 will store in age[1]
and similar for other values.
While initializing array giving size is optional;
like
float price[]={22.2,44.2,67.33,55.66,43.2};
Some times we want to initialize only few elements of array in that case we have to provide size of array and initialize only few values other values are initialized by zero.
double value[20]={10.0,21.0};
Here from value[3]
to value[19]
will be initialize by 0.0
4 Access Array Elements
Array uses zero based indexing to access elements.
to access element of an array, we can use syntax
arrayName[index];
int age[5]={21,24,25,27,26};
age[0], age[1],age[2],age[3],age[4],
will access 21,24,25,27 and 26 respectively.
Lets see few examples to store values in array and access array values.
Example: write a program to create integer array and store 5 integer number and print.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | #include <stdio.h> #include<stdio.h> int main() { int num[5]; // array num can store 5 integer value num[0] = 20; // store 20 at array index 0 num[1] = 11; // store 11 at array index 1 num[2] = 20; // store 20 at array index 2 num[3] = 33; // store 33 at array index 3 num[4] = 55; // store 55 at array index 55 printf("\nnum[0] = %d" , num[0]); printf("\nnum[1] = %d", num[1]); printf("\nnum[2] = %d" , num[2]); printf("\nnum[3] = %d", num[3]); printf("\nnum[4] = %d" , num[4]); return 0; } |
1 2 3 4 5 | num[0] = 20 num[1] = 11 num[2] = 20 num[3] = 33 num[4] = 55 |
Description : In the above program, int num[5]
array can store 5 element in a contiguous memory locations from index 0 to 4.
Example: write a program to take 5 number from user and store integer number in integer array and print.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | #include <stdio.h> #include<stdio.h> int main() { int i; int num[5]; // array num can store 5 integer value printf("Enter 5 integer number"); for (i = 0; i < 5; i++) // for loop to take 5 integer number from user { scanf("%d", & num[i]); } printf("\n Entered number is"); for (i = 0; i < 5; i++) // for loop to take 5 integer number from user { printf("\n num[%d] =%d", i, num[i]); } return 0; } |
OUTPUT
1 2 3 4 5 6 7 8 9 10 11 12 | Enter 5 integer number 21 // enter by user 32 33 44 35 Entered number is num[0] =21 num[1] =32 num[2] =33 num[3] =44 num[4] =35 |
Example: Print the sum of all elements of an array
1 2 3 4 5 6 7 8 9 | #include<stdio.h> int main() { int marks[]={88,91,93,90,86,89}; int i,sum=0; for(i=0;i<6;i++){ sum+=marks[i]; } printf("Total marks : %d ",sum); } |
Output
1 | Total marks : 537 |
5 Operations on Array
On array we can perform following basic operations
- Traverse– accessing all element of array
- Insertion– adding a new element at specified position
- Update – changing the existing value at specified position
- Deletion– deleting an element from specified position
- Search– search a element in array
A Traverse an array
Traversing is accessing the element of array.
Here we will access each element and print it
1 2 3 4 5 6 7 8 9 | #include<stdio.h> int main() { int age[]={22,23,26,29,21,20}; int i; for(i=0;i<6;i++){ printf("age[%d]=%d\n",i,age[i]); } } |
Output
1 2 3 4 5 6 | age[0]=22 age[1]=23 age[2]=26 age[3]=29 age[4]=21 age[5]=20 |
B. Inserting an element in Array
add a new element in array can be done at various position
- Insertion at the beginning of array
- add at the give index position
- Insert at last
Here we will insert a element based on index position.
On inserting a new element at specified position next element will move one space forward in array.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | #include<stdio.h> int main() { int arr[] = {22, 23, 26, 29, 21, 20}; int i, j, position = 0, element = 0; int arraySize =sizeof(arr)/sizeof(int); printf("Array is \n"); for (i = 0; i < arraySize; i++) { printf("arr[%d]=%d ", i, arr[i]); } printf("\nEnter index position on which you insert new element\n"); scanf("%d", &position); printf("\nEnter element to insert\n"); scanf("%d", &element); if(position<=arraySize){ /*move all element one place forward from user insertion position*/ for (j = arraySize; j >= position; j--) { arr[j + 1] = arr[j]; } /*insert element at specified position*/ arr[position]=element; /*incrementing array size*/ arraySize++; /*printing all elements*/ printf("Array after inserting element\n"); for (i = 0; i < arraySize; i++) { printf("arr[%d]=%d ", i, arr[i]); } }else{ printf("Please enter a valid position between 0 to %d",arraySize); } } |
Output
1 2 3 4 5 6 7 8 | Array is arr[0]=22 arr[1]=23 arr[2]=26 arr[3]=29 arr[4]=21 arr[5]=20 Enter index position on which you insert new element 1 Enter element to insert 99 Array after inserting element arr[0]=22 arr[1]=99 arr[2]=23 arr[3]=26 arr[4]=29 arr[5]=21 arr[6]=20 |
C Update an element in Array
To update an array element we have to select element based on index value and provide new value to replace the existing one.
Simple program to update array value based on index is as below
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | #include<stdio.h> int main() { int arr[] = {22, 23, 26, 29, 21, 20}; int i, j, position = 0, element = 0; int arraySize =sizeof(arr)/sizeof(int); printf("Array is \n"); for (i = 0; i < arraySize; i++) { printf("arr[%d]=%d ", i, arr[i]); } printf("\nEnter index position on which you want to update element\n"); scanf("%d", &position); printf("\nEnter element to insert\n"); scanf("%d", &element); if(position<arraySize){ /*update element at specified position*/ arr[position]=element; /*printing all elements*/ printf("Array after updating element\n"); for (i = 0; i < arraySize; i++) { printf("arr[%d]=%d ", i, arr[i]); } }else{ printf("Please enter a valid position between 0 to %d",arraySize-1); } } |
Output
1 2 3 4 5 6 | Array is arr[0]=22 arr[1]=23 arr[2]=26 arr[3]=29 arr[4]=21 arr[5]=20 Enter index position on which you want to update element Array after updating element arr[0]=22 arr[1]=23 arr[2]=26 arr[3]=2 arr[4]=21 arr[5]=20 |
D Delete an element in Array
To delete an array element First select index value of element.
After deleting array element other elements are shifted forward to fill the gap of deleted element.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | #include<stdio.h> int main() { int arr[] = {22, 23, 26, 29, 21, 20}; int i, j, position = 0, element = 0; int arraySize =sizeof(arr)/sizeof(int); printf("Array is \n"); for (i = 0; i < arraySize; i++) { printf("arr[%d]=%d ", i, arr[i]); } printf("\nEnter index position to delete element\n"); scanf("%d", &position); if(position<arraySize){ /*move all element one place backward position*/ for (j = position; j <arraySize; j++) { arr[j] = arr[j+1]; } /*decrement array size*/ arraySize--; /*printing all elements*/ printf("Array after deleting element\n"); for (i = 0; i < arraySize; i++) { printf("arr[%d]=%d ", i, arr[i]); } }else{ printf("Please enter a valid position between 0 to %d",arraySize-1); } } |
Output
1 2 3 4 5 6 | Array is arr[0]=22 arr[1]=23 arr[2]=26 arr[3]=29 arr[4]=21 arr[5]=20 Enter index position to delete element 2 Array after inserting element arr[0]=22 arr[1]=23 arr[2]=29 arr[3]=21 arr[4]=20 |
E Search an element in Array
Search operation is used to find an element in array.
If element fond return index and element value.
If not it will return -1.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | #include<stdio.h> int main() { int arr[] = {22, 23, 26, 29, 21, 20}; int i, j, position = -1, element = 0; int arraySize =sizeof(arr)/sizeof(int); printf("Array is \n"); for (i = 0; i < arraySize; i++) { printf("arr[%d]=%d ", i, arr[i]); } printf("\nEnter element to search in array\n"); scanf("%d", &element); /*checking element one by one*/ for (j =0; j <arraySize; j++) { if(arr[j]==element) position=j; break; } if(position>=0){ printf("You searched for arr[%d]=%d",position,element); }else{ printf("Element not found"); } } |
Output
1 2 3 4 5 | Array is arr[0]=22 arr[1]=23 arr[2]=26 arr[3]=29 arr[4]=21 arr[5]=20 Enter element to search in array 22 You searched for arr[0]=22 |
F Sorting elements in Array
Arranging array elements in ascending or in descending order is known as sorting of array.
Following program is used to sort array elements in ascending order.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | #include<stdio.h> int main() { int arr[] = {22, 23, 26, 29, 21, 20}; int i, j,temp=0; int arraySize =sizeof(arr)/sizeof(int); printf("Array is \n"); for (i = 0; i < arraySize; i++) { printf("arr[%d]=%d ", i, arr[i]); } for(i=0;i<arraySize;i++){ for(j=i+1;j<arraySize;j++){ if(arr[i]>arr[j]){ temp=arr[i]; arr[i]=arr[j]; arr[j]=temp; } } } /*sorted array is*/ printf("\nSorted array is\n"); for (i = 0; i < arraySize; i++) { printf("arr[%d]=%d ", i, arr[i]); } } |
Output
1 2 3 4 | Array is arr[0]=22 arr[1]=23 arr[2]=26 arr[3]=29 arr[4]=21 arr[5]=20 Sorted array is arr[0]=20 arr[1]=21 arr[2]=22 arr[3]=23 arr[4]=26 arr[5]=29 |