An array is a collection of similar data items, that are stored under a common name. A value in an array is identified by index or subscript enclosed in square brackets with the array name in the C language.
The individual data items can be integers, floating-point numbers, characters, and so on, but they must be of the same type and storage class.
Each array element is referred to by specifying the array name with a subscript, each subscript enclosed in square brackets and each subscript must be a non-negative integer.
Thus ‘n‘ elements array ‘M‘ and the elements are
M[0],M[1], M[2], M[3],…, M[n]
Why need an array?
The C language has the capability that enables us to define a set of similar data items known as an array.
Suppose you have a set of marks that you want to read into the computer, and perform some operations on these marks, such as rank them in ascending order, compute their average, or find their median.
If you think about the process of ranking a set of marks, you quickly realize that you cannot perform such an operation until each and every grade has been entered.
Therefore, using the techniques described previously, you would read in each marks and store it into a unique variable, perhaps with sequence statements such as
printf("Enter marks1\n"); scanf("%i", &marks1); printf("Enter marks2\n"); scanf("%i", &marks2); printf("%i", &marks3\n"): scanf("%i", &marks3); . . .
After the marks have been entered, you can proceed to rank them.
This can be done by setting up a series of if statements to compare each value to determine the smallest grade, the next smallest grade, and so on until the maximum grade has been determined.
Arrays can be classified into three arrays
¤ One-Dimensional arrays
¤ Two-Dimensional arrays
¤ Multi-Dimensional arrays
One dimensional array in C language
The collection of data items can be stored under a one variable name using only one subscript, such a variable is called the one-dimensional array in the C programming language.
A one-dimensional array or dingle dimensional array is a type of linear array, whose elements are accessed with a single subscript, which can either represent a row or column.
Array declaration
Arrays are declared in the same manner as ordinary variables except that each array name must have the size of the array.
Syntax: data_type array_variable[size];
Description: data _type = Specifies the type of data that will be contained in the array.
array _variable = Specified the name of the array
Size = Specifies the maximum number of elements that the array can hold.
Example: int a[5];
int marks [4];
char name [11];
float avg [9];
‘a‘ is the name of the array with 5 subscripts of integer data types and the computer reserves five storage locations, the name is a character array of 10, and the avg is a floating variable.


Feature of an array in C language
- An array is a derived data type. It is used to represent a collection of elements of the same data type.
- The elements can be accessed with the base address(index) and the subscripts define the position of the element.
- In an array, the elements are stored in the continuous memory location. The starting memory location is represented by the array name and it is known as the base address of the array.
- It is easier to refer to the array elements by simply incrementing the value of the subscript.
Array Initialisation
The values can be initialized to an array when they are declared like ordinary variables, otherwise, they hold garbage values.
The array can be initialized in two ways
- At compile time
- At run time
At compile time
Syntax: data_type array_name[size] = {List of values};
Description: the list of values must be separated by commas.
Example: int marks[3] = {70, 80, 90};
This statement declares the variable marks as an array of 3 elements and will be assigned to values specified in the list.
Like ordinary variables, the values to the array can be initialized as
marks[0] = 70;
marks[1] = 80;
marks[2] = 90;
These elements can be used like ordinary variables. Character array can also be initialized in a similar fashion.
Example:
char name[] = {'M','S','T'};
The above statement declares the name as an array of characters initialized with the string ‘MST’.
At Run time
The array can be explicitly initialized at run time.
Example:
while(i<=13) { if(i<7) sum[i] = 0; else sum[i] = sum[i] + i; }
Explanation: The while loop checks the condition i<=13. If condition is true then if statement checks the condition i<7, if condition is true then sum[i] the assigned to zero sum[i] assigned as sum[i]+i.
Like, the array can also be initialized by reading data items from the input.
Example:
int n[2]; scanf("%d%d", &n[0], &n[1]);
Here array n is declared as integer type variable, n[0], n[1] it reads through the scanf() function.
We can assign initial values to the elements of an array just as you can assign initial values to variables when they are declared.
This is done by simply listing the initial values of the array, starting from the first elements.
Values in the list are separated by commas and the entire list is enclosed in a pair of braces.
The statement int A[5] = {0, 0, 0, 0, 0}; declares an array called A to contain five integer values and initializes each of these elements to Zero. In a similar fashion, the statement int B[5] = {1, 2, 3, 4, 5} sets the value of B[0] to 1, B[1] to 2, B[2] to 3, and so on.
Arrays of characters are initialized in a similar manner; thus the statement char characters[5] = {‘a’, ‘b’, ‘c’, ‘d’, ‘e’}; defines the character array letters and initializes the five elements to the characters ‘a’, ‘b’, ‘c’, ‘d’, and ‘e’ respectively.
It is not necessary to completely initialize an entire array. If fewer initial values are specified, only an equal number of elements are initialized.
The remaining values in the array are set to zero.
Example:
float data[100] = {10, 20, 30};
The above initializes the first three values of data to 10, 20, and 30, and sets the remaining 97 elements to zero.
Example: Initializing Arrays
#include<stdio.h> int main (void) { int values[10] = {1, 2, 3, 4,5}; int i; for (i=5;i<10;++i) values[i] = i *i; for (i=0;i<10;++i) printf("array values[%i] = %i\n", i, values[i]); return 0; }
Entering data into the array
The data can be entered into an array using the input statement as well as loop statements.
Example program
Program to accept 5 numbers and print whether the number is even or odd.
/* Program to print whether the number is odd or even */ #include <stdio.h> #include <conio.h> int main() { /* Local definitions */ int a[5], i; for(i=0;i<5;i++) { printf("Enter the %d number : ", i+1); scanf("%d", &a[i]); } /* for */ for(i=0;i<5;i++) { if(a[i]%2==0) printf("%d Number is even. \n", a[i]); else printf("%d Number is odd. \n", a[i]); } /* for */ getch(); } /* main */
Explanation: This program reads 5 integers into an array, then array values one by one and evaluates whether the number is even or odd number and prints them.
Example program in c language
Program to sort the given strings alphabetically
/* Program to sort the given strings alphabetically */ #include <stdio.h> #include <string.h> #include <conio.h> int main() { int i, j; char b[20]; printf("Enter the text to sort :"); gets(b); printf("Sorted Text are :"); for(i=65;i<=90;i++) { for(j=0;j<=30;j++) { if (b[j] == toupper(i) || b[j] == tolower(i)) printf("%c", b[j]); } /* for */ } /* for */ getch(); } /* main function */ Output Enter the text to sort : I learn C Programming Language Sorted Text are : aaaaCeeggggIilLmmnnnoPrrru
Explanation: The above program gets the string characters from the keyboard. ASCII equivalent of alphabets is used to sort out the given string.
The toupper() and tolower() string functions in if statement used to ignore priority of characters i.e., capital or small letters are treated the same. If the character value given by for loop i and string b[ ] value denoted by for loop j is same that value gets printed.
Because the value of a character supplied by the outer for loop is taken alphabetically.
Two-dimensional array in c language
In the C programming language, Two-dimensional arrays are used in a situation where a table of values needs to be stored in an array.
These can be defined in the same fashion as in one-dimensional arrays, except a separate pair of square brackets is required for each subscript.
Two pairs of square brackets are required for a two-dimensional array and three pairs are required for three-dimensional arrays and so on.
Syntax: data_type – array_name[row size][column size];
Description: data_type – Specifies the type of data that will be contained in the array.
array_name – specifies the name of the array.
[row size] – specifies the name of the row.
[column size] – specifies the size of the column.
Example: int a[3][3];
Two-dimensional arrays are stored in a row-column matrix, where the left index indicates the row and the right indicates the column.
Example:
int a[3][3];
Where ‘a‘ is the array name and it reserves 3 rows and 3 columns of memory.
The individual elements are identified by the index or subscript of an array from the above table example.
a[0,0] | a[0,1] | a[0,2] |
a[1,0] | a[1,1] | a[1,2] |
a[2,0] | a[2,1] | a[2,2] |
Example program
Program to specify the two-dimension array
/* Program using two dimension array */ #include <stdio.h> void main() { /* Local definitions */ int stud[4][2]; /* An array name with 4 rows and 2 columns */ int i; /* Statements */ for(i=0;i<=3;i++) { printf("\n Enter the %d Student roll no and Mark:", i); scanf("%d%d", &stud[i][0], &stud[i][1]); } /* for */ for(i=0;i<=3;i++) printf("%d Students roll no %d mark %d", i, stud[i][0], stud[i][1]); } /* main */ Output Enter the 0 student roll no and Mark: 7865 87 Enter the 1 student roll no and Mark: 8796 90 Enter the 2 student roll no and Mark: 2343 56 Enter the 3 student roll no and Mark: 6754 79 0 Students roll no 7865 mark 87 1 Students roll no 8796 mark 90 2 Students roll no 2343 mark 56 3 Students roll no 6754 mark 79
Explanation: Look at the scanf() statement used in the first for loop in the above example. In stud[i][1] and stud [i][1] the first subscript of the variable stud is row number which changes for every student.
Now that all data is stored in a continuous location. The second subscript tells the column number. The following diagram shows how the data is stored in the memory.
i.e., 7865 is stored in stud [0][0], 87 is stored in stud [0][1] and so on.
In fact, a two-dimensional array is nothing but a collection of a number of one-dimensional arrays placed one below the other.
Initializing a two-dimensional array
Like one dimensional array, the values can be initialized to the two-dimensional arrays at the time of declaration.
Syntax: data_type array_name[row_size][column_size] = {List of values};
Description : {List of variables} specifies the list of elements.
Example:
int stud [4][2] = { {7865, 87}, {8796, 90}, {2443, 56} {6754, 79} };
Remember that while initializing an array it is necessary to mention the second dimension, whereas the first dimension is optional. So the following will never work.
int arr[2] [ ] = {1, 2, 3, 4, 5, 6};
int arr[ ] [ ] = {1, 2, 3, 4, 5, 6};
We must mention the column size then only the compiler knows where the first row ends. The two sizes are optional if we initialize the array in the declaration part itself.
Example program
Program to illustrate Matrix Multiplication
/* Program for matrix multiplication */ #include <stdio.h> int main() { int a[5][5], b[5][5], c[5][5],r1 ,r2, c1, c2, i, j, k; Step1: printf("\nEnter the size of the matrix A...."); scanf("%d%d", &r1, &c1); printf("\nEnter the size of the matrix B...."); scanf("%d%d", &r2, &c2); if(c1 == c2) goto step2; else printf("\nMultiplication is not possible"); goto step1; Step2: printf("\nEnter matrix A elements...\n"); for(i=0;i<r1;i++) { for(j=0;j<c1;j++) scanf("%d", &a[i][j]); } printf("\nEnter matrix B elements...\n"); for(i=0;i<r2;i++) { for(j=0;j<c2;j++) scanf("%d", &b[i][j]); } /* for */ for(i=0;i<r1;i++) { for(j=0;j<c1;j++) { c[i][j] = 0; for(k=0;k<c1;k++) c[i][j] = c[i][j] + a[i][k] * b[k][j]; } /* for */ } /* for */ printf("\nThe resultant matrix is ....\n"); for(i=0;i<r1;j++) { for(j=0;j<c1;j++) printf("%d\t", c[i][j]); printf("\n"); } /* for */ } /* main */
Output:
Enter the size of the matrix A....3 3 Enter the size of the matrix B....2 2 Mulplication is not possible Enter the size of the matrix A....3 3 Enter the size of the matrix B....3 3 Enter matrix A elements... 2 2 2 2 2 2 2 2 2 Enter matrix B elemrnts... 3 3 3 3 3 3 3 3 3 The resultant matrix is .... 18 18 18 18 18 18 18 18 18 ....
Explanation: The program reads the size of the matrix, then checks rows and columns are equal or not.
If rows and columns are equal further execution starts, else it prints multiplication is not possible. The i and j for loop are used to get both A and B matrices.
The other i and j for loop are used to calculate the matrix multiplication and the final i and j for loop are used to print the resultant matrix.
Multi-dimensional array in c language
Similarly, like one and two-dimensional arrays, the C language allows multi-dimensional arrays.
The dimension with three or more is called multi-dimensional arrays.
Syntax: data_type array_name[size1][size2]……[size n];
Description:
datatype is the type of data, that will be stored in an array.
array_name is the name of the array.
size1, size2…sizen are the sizes of the dimensions or subscripts.
Example:
int a[3][3][3];
float table, b[4][4][4][4];
here,
a is the three-dimensional array declared as an integer type and can hold 27 elements.
b is the four-dimensional array declared as float type and can hold 256 elements.
The array of more than three dimensions is not used often. Multi-dimensional arrays are slower than the single dimension in execution.


One of the most natural applications for a multi-dimensional array arises in the case of a matrix.
Consider the 4 x 4 matrix shown below
In mathematics, it is quite common to refer to an element of a matrix by the use of a double subscript.
Matrix Method
So if you call the preceding matrix M, the notation M(i, j) refers to the element in the ith row, jth column, where ‘i‘ ranges from 1 to 4, and ‘j’ ranges from 1 to 4.
The notation M2, 2 refers to the value 2, which is found in the 2nd row, 2nd column of the matrix.
In a similar fashion, M4, 4 refers to the elements contained on the 4th row, 4th column: the value 76.
In C programming language you can use an analogous notation when referring to elements of a two-dimensional array.
However, because the C language likes to start numbering things at zero, 1the row of the matrix is actually row 0, and the 1st column of the matrix is column 0. The proceeding matrix would then have row and column designations.
Whereas in mathematics the notation Mi, j is used, in C language the equivalent notation is
M[ i ][ j ]
Remember, the first index number refers to the row number, whereas the second index number refers to the column.
Multi-dimensional arrays can be initialized in a manner analogous to their one-dimensional counterparts. When listing elements for initialization, the values are listed by row.
Brace pairs are used to separate the list of initializers for one row from the next. So to define and initialize the array M to the elements listed below.
int M[4][4] = { {9, 3, 19, 82},
{9, 2, 20, 07},
{13, 8, 20, 13},
{17, 7, 19, 76},
};
Note that commas are required after each brace that closes off a row, except in the case of the final row.
The use of the inner pairs of braces is actually optional. If not supplied, initialization proceeds by row. Thus, the preceding statement could also have been written as follows
int M[4][4] = {9, 3, 19, 82, 9, 2, 20, 07, 13, 8, 20, 13, 17, 7, 19, 76};
Example program
Write a program to explain the working of the four-dimensional array.
/* Program to explain the working of four dimensional array */ #include <stdio.h> #include <conio.h> int main() { /* 4 dimension array declaration */ int array_4d[3][3][3][3]; int a, b, c, d; for(a=0;a<3;a++) for(b=0;b<3;b++) for(c=0;c<3;c++) for(d=0;d<3;d++) array_4d[a][b][c][d]= a+b+c+d; for(a=0;a<3;a++) { printf("\n"); for(b=0;b<3;b++) { for(c=0;c<3;c++) { for(d=0;d<3;d++) printf("%3d", array_4d[a][b][c][d]); printf("\n"); } /* for */ } /* for */ getch(); } /* for */ getch(); } /* main */ Output: 0 1 2 1 2 3 2 3 4 1 2 3 2 3 4 3 4 5 2 3 4 3 4 5 4 5 6 1 2 3 2 3 4 3 4 5 2 3 4 3 4 5 4 5 6 3 4 5 4 5 6 5 6 7 . . . . . .
Explanation: In the program, the four-dimensional array array_4d is declared. The first four for loops are used for adding the values of a, b, c and d.
Using the second four for loops the elements are printed as the output given above.
Compute mean, median, and mode in c language
Mean, Median and Mode are three kinds of “averages”. There are many “averages” in statistics, but there are the three most common.
The “mean” is the “average” used, where you add up all the numbers and then divide by the number of numbers.
The “median” is the “middle” value in the list of numbers. To find the median, your numbers have to be listed in numerical order, so you may have to rewrite your list first.
The “mode” is the value that occurs most often. If no number is repeated, then there is no mode for the list.
Example: Find the mean, median, mode, and range for the following list of values: 13, 18, 13, 14, 13, 16, 14, 21, 13.
The mean is the usual average, so:
(13+18+13+14+13+16+14+21+13) ÷ 9 = 15
The median is the middle value, so rewrite the list in order.
13, 13, 13, 13, 14, 14, 16, 18, 21
There are nine numbers in the list, so the middle one will be the (9+1) ÷ 2 = 10 ÷ 2 = 5th number in 13, 13, 13, 13, 14, 14, 16, 18, 21 is 14.
Mean: 15
Median: 14
Mode: 13
Example program
/* Program ind the mean, median, mode, and range */ #include<stdio.h> int main() { int i, j, a[20] = {0}, sum = 0, n, t, b[20] = {0}, k = 0, c = 1, max = 0, mode; float x = 0.0, y = 0.0; printf("\nEnter the limit\n"); scanf("%d", &n); printf("Enter the set of numbers\n"); for(i = 0;i<n;i++) { scanf("%d", &a[i]); sum = sum + a[i]; } x = (float) sum/(float)n; printf("Mean\t = %f", x); for(i=0;i<n;i++) { for(j=i+1;j<n;j++) { if(a[i]>a[j]) { t = a[i]; a[i] = a[j]; a[j] = t; } } } if(n%2 == 0) y = (float)(a[n/2] + a[(n-1)/2])/2; else y = a[(n-1)/2]; printf("\nMedian\t = %f", y); for(i=0;i<n-1;i++) { mode = 0; for(j=i+1;j<n;j++) { if(a[i] == a[j]) mode++; } if(mode>max && mode != 0) { k = 0; max = mode; b[k] = a[i]; k++; } else if(mode == max) { b[k] = a[i]; k++; } } for(i=0;i<n;i++) { if(a[i] == b[i]) c++; } if(c == n) printf("\nThere is no mode"); else { printf("\nMode\t = "); for(i=0;i<k;i++) printf("%d", b[i]); } }
Output:
Enter the limit 5 Enter the set of numbers 6 4 3 2 4 Mean = 3.800000 Median = 4.000000 Mode = 4