Read and Display an 2D Array (Matrix) by row wise
#include <stdio.h>
int main()
{
int row, column;
printf("Enter number of rows: ");
scanf("%d", &row);
printf("Enter number of columns: ");
scanf("%d", &column);
int A[row][column];
printf("Enter elements of the A:\n");
for(int i = 0; i < row; i++)
{
for(int j = 0; j < column; j++)
{
printf("A[%d][%d] = ", i, j);
scanf("%d", &A[i][j]);
}
}
printf("A:\n");
for(int i = 0; i < row; i++)
{
for(int j = 0; j < column; j++)
{
printf("%10d ", A[i][j]);
}
printf("\n");
}
}
Read and Display an 2D Array (Matrix) by column wise
#include <stdio.h>
int main()
{
int row, column;
printf("Enter number of rows: ");
scanf("%d", &row);
printf("Enter number of columns: ");
scanf("%d", &column);
int A[row][column];
printf("Enter elements of the A:\n");
for(int j = 0; j < column; j++)
{
for(int i = 0; i < row; i++)
{
printf("A[%d][%d] = ", i, j);
scanf("%d", &A[i][j]);
}
}
printf("A:\n");
for(int i = 0; i < row; i++)
{
for(int j = 0; j < column; j++)
{
printf("%10d ", A[i][j]);
}
printf("\n");
}
}
Read elements randomly using ‘rand()’ of an 2D Array
(Matrix) and Display it
#include <stdio.h>
int main()
{
int row, column;
printf("Enter number of rows: ");
scanf("%d", &row);
printf("Enter number of columns: ");
scanf("%d", &column);
int A[row][column];
printf("Enter elements of the A:\n");
for(int i = 0; i < row; i++)
{
for(int j = 0; j < column; j++)
{
A[i][j] = rand();
}
}
printf("A:\n");
for(int i = 0; i < row; i++)
{
for(int j = 0; j < column; j++)
{
printf("%10d ", A[i][j]);
}
printf("\n");
}
}
Sum of the elements of Main Diagonal of a Matrix
#include <stdio.h>
int main()
{
int row, column, sum = 0;
printf("Enter number of rows: ");
scanf("%d", &row);
printf("Enter number of columns: ");
scanf("%d", &column);
int A[row][column];
printf("Enter elements of the A:\n");
for(int i = 0; i < row; i++)
{
for(int j = 0; j < column; j++)
{
A[i][j] = rand();
}
}
printf("A:\n");
for(int i = 0; i < row; i++)
{
for(int j = 0; j < column; j++)
{
printf("%10d ", A[i][j]);
}
printf("\n");
}
for(int i = 0; i < row; i++)
{
sum = sum + A[i][i];
}
printf("Sum of the elements of Main Diagonal of A: %d\n", sum);
return 0;
}
Sum of all the elements of first row of a Matrix:
Sum of all the elements of second row of a Matrix:
Sum of all the elements of third row of a Matrix:
Sum of all the elements of last row of a Matrix:
Sum of all the elements of first column of a Matrix:
Sum of all the elements of second column of a Matrix:
Sum of all the elements of third column of a Matrix:
Sum of all the elements of last column of a Matrix:
Sum of all elements of Matrix:
#include <stdio.h>
#include <stdlib.h>
int main()
int row, column, sum = 0;
printf("Enter number of rows: ");
scanf("%d", &row);
printf("Enter number of columns: ");
scanf("%d", &column);
int A[row][column];
printf("Enter elements of the A:\n");
for(int i = 0; i < row; i++)
for(int j = 0; j < column; j++)
A[i][j] = rand();
}
printf("A:\n");
for(int i = 0; i < row; i++)
for(int j = 0; j < column; j++)
printf("%10d ", A[i][j]);
printf("\n");
for(int i = 0; i < row; i++)
for(int j = 0; j < column; j++)
sum = sum + A[i][j];
printf("Sum of all the elements of A: %d", sum);
return 0;
}
sum of all the elements along the outermost edge of a 2D
array
#include <stdio.h>
int main()
{
int row, column, sum = 0;
printf("Enter number of rows: ");
scanf("%d", &row);
printf("Enter number of columns: ");
scanf("%d", &column);
int A[row][column];
printf("Enter elements of the A:\n");
for(int i = 0; i < row; i++)
{
for(int j = 0; j < column; j++)
{
A[i][j] = rand();
}
}
printf("A:\n");
for(int i = 0; i < row; i++)
{
for(int j = 0; j < column; j++)
{
printf("%10d ", A[i][j]);
}
printf("\n");
}
for(int j = 0; j < column; j++)
{
sum = sum + A[0][j];
sum = sum + A[row - 1][j];
}
for(int i = 1; i < row - 1; i++ )
{
sum = sum + A[i][0];
sum = sum + A[i][column - 1];
}
printf("Sum of all the elements along the outermost edge of A: %d", sum);
return 0;
}