C Programming IT Programming Tutorials

Arrays in C

Normally we use variables to store and access values but when there is a need to handle huge data, we will go for arrays.

Arrays in C are used to store a group of values of a similar data type. It is stored in contiguous memory. Each element of an array can be accessed using the index. An array index starts with 0 and ends with n-1(where n is the total number of elements).

Declaring arrays in C:

Syntax:

datatype array_name [array_size];

The data type can be int, float, double, char, etc.

array_name can be any name of your choice but must follow the same rules that need to be followed while naming a variable.

The array_size represents the total number of elements in an array.

Example: 1

int a[10];
float average[50];xzsxc
char s[5];

Initializing arrays in C:

Assigning values to an array is referred to as initializing an array.

The maximum number of elements the array can hold is given by the array size.

Example 2:

int a[5]={10,20,30,40,50};
float average[50]={25.5, 50.3, 45.2, 20.6, 30.0};
char s[5]="hai"; or
int a[]={1,2,3,4,5,6,7,8,9,10}; 

when an array is declared without mentioning the size, the array will allocate memory large enough to hold the assigned values.

In the above example, a[0]=1,a[1]=2….last element will be a[n-1].

Example 3:

Explanation:

In the above example, we get values for each element of an array by iterating through the loop, where (i=0 to the size of the array). The second for loop access each element of an array and prints.

Example 4:

Explanation: 

In this example, we receive input from the user and calculate the average of the array elements.

Types of Arrays in C:

The Multi-dimensional array includes size1,size2… sizeN arrays.
 Syntax:   

DataType array_name [size1][size2]....[sizeN];
 int a[2][3];   
 or     
 int multid[3][4][5];

Two-Dimensional Arrays:

The list of one-dimensional arrays is known as two-dimensional arrays. The two-dimensional arrays are represented by a[i][j] where i and j are the subscripts that refer to rows and columns.

It can be initialized in two ways, for eg:

int a[2][3]={10,80,12,20,36,70};

or

 int a[2][3]={  
             {10, 80, 12},
             {20, 36, 70},
             };

we can access two-dimensional arrays using the row and column index. For example, the value of a[1][1] =>36

Example 5:

Explanation:

In this example, we first display the given elements in matrix form, to do that we use two for loops, one refers to the row and the other to the column and prints the element.

The second printf is used at the end of the inner for loop so that the next element gets printed in a new line.

To get the transpose of a matrix we need to change row elements to columns(value at a[0][1] will be moved to a[1][0]). To do that max values of subscripts i and j are swapped and a[j][i] is assigned to transpose matrix b[i][j].

Passing Arrays To Functions:

Example 6:

Explanation:

We pass the array and size of the array inside the function, and the base address of the array is passed inside the function definition. Each element of the array is accessed using the index and the greater value is assigned to the variable x and returned to the main function.

Example 7:

Explanation:

In this example, we get row and column size from the user and then use two for loops to iterate through rows and columns and get values for each element of the matrix.

Similarly, for the second matrix, we get values and add elements of a[i][j] with b[i][j] and store the result in sum[i][j].

Example 8:

Explanation:

In matrix multiplication, we multiply each element of the first row of the first matrix with each element of the first column of the second matrix and add everything, the result is placed in the respective position.    

To access row and column we use two for loops i and j and initialize the resultant matrix c[i][j] with 0. Since we need to sum all products of a[i][j] and b[j][i] elements and initialize c[i][j] to 0 only while moving to next column we use another for loop.

We repeat the same process and store in the resultant c matrix.

Recommended Articles

Leave a Reply

Your email address will not be published. Required fields are marked *