0% found this document useful (0 votes)
4 views8 pages

2D Array Operations in C Programming

The document contains multiple C programs that demonstrate how to read and display a 2D array (matrix) in various ways, including row-wise and column-wise. It also includes programs to calculate the sum of elements in the main diagonal, specific rows and columns, all elements, and the outermost edge of the matrix. Each program prompts the user for the number of rows and columns, fills the matrix with user input or random values, and displays the results.

Uploaded by

tanveera2001
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views8 pages

2D Array Operations in C Programming

The document contains multiple C programs that demonstrate how to read and display a 2D array (matrix) in various ways, including row-wise and column-wise. It also includes programs to calculate the sum of elements in the main diagonal, specific rows and columns, all elements, and the outermost edge of the matrix. Each program prompts the user for the number of rows and columns, fills the matrix with user input or random values, and displays the results.

Uploaded by

tanveera2001
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

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;
}

You might also like