Introduction to 2 Dimensional Arrays

2 Dimensional Arrays

In this part you will learn: 1. C syntax 2. 2D arrays 3. Nested for loops 4. Showing output In this tutorial I will teach you about 2D arrays. 2D arrays can be called array of arrays. First we had a single array that was in just one single dimension and we accessed its elements by writing the index in the subscript. In 2d arrays we have arrays that go in both direction. So now we access its elements by mentioning the row number as well as the column number in the subscript separately. Matrix Operation Basic Step: Open Dev C++ then File > new > source file and start writing the code below.
  1. #include<stdio.h>
  2. #include<conio.h>
These two are the most common and basic header files. The first one stdio.h is used to grant us most of the basic functions of C like Input and Output functions. The second one conio.h provides us functions like getch() which is used to pause our screen until we press a button.
  1. int main()
  2. {
  3. int matrix[100][100];
  4.  
  5. int rows,columns;
  6. printf("Enter the number of rows:");
  7. scanf("%d",&rows);
  8.  
  9. printf("Enter the number of columns:");
  10. scanf("%d",&columns);
  11.  
  12. printf("Please enter the elements of matrix:\n");
  13.  
  14. int i,j;
  15. for( i=0;i<rows;i++)
  16. {
  17. for( j=0;j<columns;j++)
  18. {
  19. scanf("%d",&matrix[i][j]);
  20. }
  21.  
  22. }
In this program we declared a 2d array of integers. We will treat it like a matrix in mathematics. This is because the concept of 2d arrays can be very well understood by with the implementation of matrixes. We will calculate the sum of diagonals of matrix separately in this program. At first we declared a 2d array of integers with 100 rows and 100 columns. The first number in the subscript is always the number of rows of the matrix and the second in the subscript is the total column number. Then we took input from the user the number of rows and columns. This is because we want the user to tell us how big he wants to build a matrix. For this program the user should enter same number of rows and columns because the diagonal sum can only be calculated of square matrix. Then we wrote a nested for loop to take input the elements of the matrix. The first for loop is to shift the rows of the matrix and the second or the inner for loop is to iterate between the columns of the matrix. For every row of the matrix the inner for loop runs column times so we can enter the value of each element of the matrix.
  1. printf("The matrix you entered\n");
  2. int k,l;
  3. for( k=0;k<rows;k++)
  4. {
  5. for( l=0;l<columns;l++)
  6. {
  7. printf("%d",matrix[k][l]);
  8. printf(" ");
  9. }
  10. printf("\n");
  11.  
  12. }
Now after taking input all the elements of the matrix we are printing them using the similar nested for loops but this time we are printing the value of the elements columnwise.
  1. int sum=0,m;
  2.  
  3. for(m=0;m<rows;m++)
  4. {
  5. sum=sum+matrix[m][m];
  6. }
  7. printf("\nSum of the left diagonal is:%d",sum);
In this for loop we are calculating the sum of the principal diagonal or the left diagonal of matrix. What we are doing is we are running a single for loop that is adding all the elements of the matrix with same row and column number. Then after the loop we are printing the value of the sum of the diagonal.
  1. j=columns-1;
  2. sum=0;
  3. for(m=0;m<rows && j>=0;m++,j--)
  4. {
  5. sum=sum+matrix[m][j];
  6. }
  7. printf("\nSum of the right diagonal is:%d",sum);
  8.  
  9. }
In this part we are calculating the sum of the right diagonal of the matrix. For this we have to increase the row number while decreasing the column number. For this we initialized a variable j with column -1 so that it becomes the last index of the column. The rest of the program is same. We accumulated the sum in a single variable called sum and then at the end we printed the value. Execute > compile then Execute > run Output picture viewer

Add new comment