By using 4*5 matrix and perform the following operations in C.

Question:

Write a program using a 4*5 matrix and perform the following operations in C:

  1. Populate the matrix of the above-mentioned size.
  2. Printing the sum of numbers in all columns. 
  3. Printing the multiplication of numbers in all rows.
  4. Swapping rows with columns and columns with rows and printing the matrix.

Summary:

To know the answers to these questions we must know about the matrix.

Explanation:

Matrix:

It is the two-dimensional array(rectangle, square) of numbers arranged in rows and columns. Here we have to create a 4*5 matrix so there are 4 rows and 5 columns in this matrix. So the given matrix is a two-dimensional rectangular array. Accessing the elements of the matrix is done by indexing.

4*5 matrix indexing:

Let the matrix be the 2D array name and the indexes are:

matrix[0][0] matrix[0][1] matrix[0][2] matrix[0][3] matrix[0][4]
matrix[1][0] matrix[1][1] matrix[1][2] matrix[1][3] matrix[1][4]
matrix[2][0] matrix[2][1] matrix[2][2] matrix[2][3] matrix[2][4]
matrix[3][0] matrix[3][1] matrix[3][2] matrix[3][3] matrix[3][4]

 

 

 

We use nested for loops for accessing rows and columns of the array ‘matrix’. The upper for loop is used for representing the rows and the lower for loop is used to represent the columns in the array ‘matrix’

Steps for using a 4*5 matrix and perform the following operations in C:

  1. Firstly include header files.
  2. Next, initialize the array matrix[4][5] and newMatrix[4][5] to store the resultant array.
  3. And the declare i, j, mul=1, c=0, sum.
  4. After that populate the array.
  5. Further, perform the sum of each column.
  6. Next, write the code for the multiplication of each row.
  7. Finally, do the swapping of the matrix.

Source code:

Code for using 4*5 matrix in C and we have to do populate the matrix, calculate the sum of columns, the multiplication of each row, and swapping the matrix that is changing rows into columns and vice-versa.

#include<conio.h>
#include<stdio.h>
void main()
{	int matrix[4][5], newMatrix[4][5];;
    int i,j,mul=1;
    int c=0;
    int sum[5]={0,0,0,0,0};
        //populate the matrix
    for(i=0; i<4; i++)
        for(j=0; j<5; j++)
            matrix[i][j]=c++;
    printf("\nInitially the matrix is: \n");
    for(i=0; i<4; i++)
    {	for(j=0; j<5; j++)
            printf("%d\t",matrix[i][j]);
        printf("\n");
    }
    
    //calculate sum of columns	
    for(i=0; i<4; i++)
        for(j=0; j<5; j++)
            sum[j]+=matrix[i][j];
    
    printf("\nSum of columns is : \n");
    for(i=0; i<5; i++)
        printf("%d\t",sum[i]);

    //multiplication of each row
    printf("\n\nMultiplication of each row is: \n");
    for(i=0; i<4; i++)
    {	mul=1;
        for(j=0; j<5; j++)
            mul*=matrix[i][j];
        printf("%d\n",mul);
    }
    
    for(i=0; i<4; i++)
        for(j=0; j<5; j++)
            newMatrix[j][i]=matrix[i][j];
    
    //swapping the matrix
    printf("\nAfter swapping the matrix is: \n");
    for(i=0; i<5; i++)
    {	for(j=0; j<4; j++)
            printf("%d\t",newMatrix[i][j]);
        printf("\n");
    }
}

Output:

output for using 4*5 matrix in C

 

Also, read Create an app called RestaurantRater that allows the user to save the name and address of a restaurant into a database table.

 

Share this post

Leave a Reply

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