0% found this document useful (0 votes)
12 views11 pages

2d Array Questions Solutions

The document contains multiple modular C programs that perform various operations on matrices, including reading and printing 2D arrays, transposing matrices, summing two matrices, calculating the diagonal sum of a square matrix, finding row and column sums, counting even and odd numbers in each row and column, and checking if a matrix is sparse. Each program is structured with functions for specific tasks to enhance modularity and readability. The programs utilize dynamic array sizes based on user input for rows and columns.

Uploaded by

bellakkisahana
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)
12 views11 pages

2d Array Questions Solutions

The document contains multiple modular C programs that perform various operations on matrices, including reading and printing 2D arrays, transposing matrices, summing two matrices, calculating the diagonal sum of a square matrix, finding row and column sums, counting even and odd numbers in each row and column, and checking if a matrix is sparse. Each program is structured with functions for specific tasks to enhance modularity and readability. The programs utilize dynamic array sizes based on user input for rows and columns.

Uploaded by

bellakkisahana
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

1.

Write a moular c program to read and write in 2d array


#include<stdio.h>
void read_array(int r,int c ,int a[r][c]);
void print_array(int r,int c ,int a[r][c]);
main()
{

int r, c;
printf("enter the row and column");
scanf("%d%d",&r,&c);
int a[r][c];
read_array(r,c , a);
print_array(r,c , a);
}
void read_array(int r,int c ,int a[r][c])
{
printf("the array elemnts are");

for(int i=0;i<r;i++)
{

for(int j=0;j<c;j++)
{

scanf("%d",&a[i][j]);
}
}
}
void print_array(int r,int c ,int a[r][c])
{

for(int i=0;i<r;i++)
{

for(int j=0;j<c;j++)
{

printf("%d",a[i][j]);
}
printf("\n");
}
}
2. Write a moular c program to transope of matrix

#include <stdio.h>

void read_array(int r, int c, int a[r][c]);


void print_array(int r, int c, int a[r][c]);
void transpose(int r, int c, int a[r][c], int t[c][r]);
int main()
{
int r, c;
printf("Enter rows and columns: ");
scanf("%d %d", &r, &c);
int a[r][c], t[c][r];
read_array(r, c, a);
printf("Original matrix:\n");
print_array(r, c, a);
transpose(r, c, a, t);
printf("After transpose:\n");
print_array(c, r, t);
return 0;
}
void read_array(int r, int c, int a[r][c])
{
printf("Enter matrix elements:\n");
for(int i = 0; i < r; i++)
{
for(int j = 0; j < c; j++)
{
scanf("%d", &a[i][j]);
}
}
}

void print_array(int r, int c, int a[r][c])


{
for(int i = 0; i < r; i++)
{
for(int j = 0; j < c; j++)
{
printf("%d ", a[i][j]);
}
printf("\n");
}
}
void transpose(int r, int c, int a[r][c], int t[c][r])
{
for(int i = 0; i < r; i++)
{
for(int j = 0; j < c; j++)
{
t[j][i] = a[i][j];
}
}
}

3. Write a modular c program to find sum of 2 matrices

#include<stdio.h>

void read_array(int r, int c, int a[r][c]);

void print_array(int r, int c, int a[r][c]);

void sum_matrix(int r, int c, int a[r][c], int b[r][c], int sum[r][c]);

int main()

int r, c;

printf("Enter the rows and columns: ");

scanf("%d%d", &r, &c);

int a[r][c], b[r][c], sum[r][c];

printf("Enter elements of first matrix:\n");

read_array(r, c, a);

print_array(r, c, a);

printf("Enter elements of second matrix:\n");

read_array(r, c, b);
print_array(r, c, b);

sum_matrix(r, c, a, b, sum);

printf("Sum of two matrices:\n");

print_array(r, c, sum);

return 0;

void read_array(int r, int c, int a[r][c])

for(int i = 0; i < r; i++)

for(int j = 0; j < c; j++)

scanf("%d", &a[i][j]);

void print_array(int r, int c, int a[r][c])

for(int i = 0; i < r; i++)

for(int j = 0; j < c; j++)

printf("%d ", a[i][j]);

printf("\n");
}

void sum_matrix(int r, int c, int a[r][c], int b[r][c], int sum[r][c])

for(int i = 0; i < r; i++)

For(int j = 0; j < c; j++)

{ sum[i][j] = a[i][j] + b[i][j];

4. Write a modular c program to find Diagonal Sum of a Square Matrix


#include <stdio.h>

// Function to read a square matrix


void readMatrix(int n, int a[n][n])
{
printf("Enter the matrix elements:\n");
for(int i = 0; i < n; i++)
{
for(int j = 0; j < n; j++)
{
scanf("%d", &a[i][j]);
}
}
}

// Function to calculate primary diagonal sum


int diagonalSum(int n, int a[n][n])
{
int sum = 0;
for(int i = 0; i < n; i++)
{
sum += a[i][i];
}
return sum;
}

// Function to print the matrix


void printMatrix(int n, int a[n][n])
{
for(int i = 0; i < n; i++)
{
for(int j = 0; j < n; j++)
{
printf("%d ", a[i][j]);
}
printf("\n");
}
}

int main()
{
int n;
printf("Enter the order of the matrix: ");
scanf("%d", &n);

int a[n][n];

readMatrix(n, a);

printf("\nMatrix:\n");
printMatrix(n, a);
int res=diagonalSum(n, a);
printf("\nDiagonal Sum = %d\n", res);

return 0;
}
5. Write a modular cprogram to find Rowsum and Colsum

#include <stdio.h>

// Function to read matrix

void readMatrix(int r, int c, int a[r][c]) {

printf("Enter the elements:\n");


for (int i = 0; i< r; i++) {

for (int j = 0; j < c; j++) {

scanf("%d", &a[i][j]);

// Function to print matrix

void printMatrix(int r, int c, int a[r][c]) {

for (int i = 0; i< r; i++) {

for (int j = 0; j < c; j++) {

printf("%d ", a[i][j]);

printf("\n");

// Function to calculate and print row sums

void rowSum(int r, int c, int a[r][c]) {

printf("\nRow Sums:\n");

for (int i = 0; i< r; i++) {

int sum = 0;

for (int j = 0; j < c; j++) {

sum += a[i][j];

printf("Row %d = %d\n", i, sum);

}
}

// Function to calculate and print column sums

void colSum(int r, int c, int a[r][c]) {

printf("\nColumn Sums:\n");

for (int j = 0; j < c; j++) {

int sum = 0;

for (int i = 0; i< r; i++) {

sum += a[i][j];

printf("Column %d = %d\n", j, sum);

int main() {

int r, c;

printf("Enter rows and columns: ");

scanf("%d %d", &r, &c);

int a[r][c];

readMatrix(r, c, a);

printf("\nMatrix:\n");

printMatrix(r, c, a);
rowSum(r, c, a);

colSum(r, c, a);

return 0;

Write a modular c program Count Even and Odd Numbers in Each Row and column

#include <stdio.h>

void readMatrix(int r, int c, int a[r][c]) {

printf("Enter the elements:\n");

for(int i = 0; i< r; i++)

for(int j = 0; j < c; j++)

scanf("%d", &a[i][j]);

void rowEvenOdd(int r, int c, int a[r][c]) {

for(int i = 0; i< r; i++) {

int even = 0, odd = 0;

for(int j = 0; j < c; j++) {

if(a[i][j] % 2 == 0) even++;

else odd++;

printf("Row %d Even: %d, Odd: %d\n", i, even, odd);

}
void colEvenOdd(int r, int c, int a[r][c]) {

for(int j = 0; j< c; j++) {

int i, even = 0, odd = 0;

for(int i = 0; i < r; i++) {

if(a[i][j] % 2 == 0) even++;

else odd++;

printf("col %d Even: %d, Odd: %d\n", i, even, odd);

int main() {

int r, c;

printf("Enter rows and columns: ");

scanf("%d %d", &r, &c);

int a[r][c];

readMatrix(r, c, a);

rowEvenOdd(r, c, a);

colEvenOdd(r, c, a);

return 0;

6. Write a modular c program to Check if Matrix is Sparse


Sparse: matrix is considered sparse if most entries are zero
#include <stdio.h>
void readMatrix(int r, int c, int a[r][c]) {
for(int i = 0; i< r; i++)
for(int j = 0; j < c; j++)
scanf("%d", &a[i][j]);
}

int isSparse(int r, int c, int a[r][c]) {


int zero = 0;
int total = r * c;

for(int i = 0; i< r; i++)


{
for(int j = 0; j < c; j++)
{ if(a[i][j] == 0) zero++;}
}

return (zero > total / 2);


}

int main() {
int r, c;
scanf("%d %d", &r, &c);

int a[r][c];
readMatrix(r, c, a);

if(isSparse(r, c, a))
printf("Sparse Matrix\n");
else
printf("Not a Sparse Matrix\n");

return 0;
}

You might also like