0% found this document useful (0 votes)
41 views39 pages

2 Darrays

Uploaded by

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

2 Darrays

Uploaded by

01fe21bec052
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 39

2D ARRAYS

Q1: What is 2D array?


A1: A 2D array is stored in the form of rows and columns. (Matrix form)
Q2: Give some examples where 2D arrays are needed?
A2:
a. To store periodic table
b. Store the distance between 'N' cities.
Q3: How will you declare 2D arrays. Give examples.
A3:
General Syntax:
data-type matrix-name[Number of rows][Number of columns];
Ex: int table[4][5];
This gives declaration of matrix which stores integers of order 4X5
Q4: From where the index of the matrix starts?
A4: It starts from 0th row and 0th column.
Q5: Draw the table / matrix of the following declaration.
float mat[3][2];
A5:
Row /Column Column 0 Column 1
Row 0 mat[0][0] mat[0][1]
Row 1 mat[1][0] mat[1][1]
Row 2 mat[2][0] mat[2][1]
Q6: Explain compile time initialization of 2D arrays.
A6:
General Syntax:
data-type matrix-name [Row SIZE][Col SIZE]={elements};
Ex1:
int matrix[2][3]={1,2,3,4,5,6};
Matrix Representation:
1 2 3
4 5 6
--------------------------------------------------------------------------------------
Ex2:
int matrix[2][3]={1,2,3,4};
1 2 3
4 0 0
We have initialized only 4 elements out of 6 (2*3), The last elements are
filled with default values. (Default value for int,float and double is ZERO
and for char it is NULL).
---------------------------------------------------------------------------------------
Ex 3:
int matrix[2][3]={{1,2,3},{4,5,6}};
1 2 3
4 5 6
Here we have given flower brackets for individual rows.
-------------------------------------------------------------------------------------
Ex 4:
int matrix[2][3]={{1,2},{3,4}};
1 2 0
3 4 0
Here the last elements of the row is filled with ZERO. (Compare with
Ex2)
Q7: What are the two ways in which elements can be stored in a 2D array
A7:
a. row major
b. column major
Q8: Explain row major and column major with an example.
A8: In row major first the rows are filled. In case of column major first
the columns are filled. The row major or column major depends on
computer architecture. Normally many computers use row major.
Ex: int mat[2][3]={1,2,3,4,5,6};
Row Major:
1 2 3
4 5 6
Column Major:
1 3 5
2 4 6
Q9: Explain in memory how does the 2D array is stored.
A9: 2D array is stored as 1D array in memory. Comsider the following
example, int a[3][3]={{1,2,3},{4,5,6},{7,8,9}};
a[0][0] a[0][1] a[0][2]
1 2 3
a[1][0] a[1][1] a[1][2]
4 5 6
a[2][0] a[2][1] a[2][2]
7 8 9
The data-type of the matrix is int and it takes 4 bytes, in memory it is
stored as,
1 2 3 4 5 6 7 8 9
1000 1004 1008 1012 1016 1020 1024 1028 1032
The second row represents the memory locations (address)
Q10: How will you access the 2D array elements.
A10: They can be accessed with the row and column subscript.
Ex: a[3][0]; //will give matrix element of 3rd row and 0th column
Q11: How will you find the address of element a[1][2] in the following
declaration. If the base address is 1000.
int a[3][3]={{1,2,3},{4,5,6},{7,8,9}};
A11: Base address is the address of starting element. (0th row and 0th
column).
Formula:
Base-Address + number_of_columns * sizeof(data-type) * row_number +
column_number * sizeof(data-type)

Therefore, address of a[1][2] is:


number_of_columns = 3
row_number = 1 //Note:a[1][2]
column_number = 2
sizeof(data-type)=4
1000 + 3*4*1 + 2 * 4
1000 + 12 + 8
1020
Q12: How will you access the element '5' in the following declaration.
int a[3][3]={{1,2},{0,5,6},{7,8,9}};
A15: a[1][1];
Explanation: Draw the matrix
Q16: Explain run time initialization of 2D array.
A16: Run time initialization can be done with scanf function. The values
are read at run-time (During execution of program from user)
Martix has 2 indices one for row and one for column. Therefore we need
two loops, which reads elements row by row.

Ex:
for(i=0;i<2;i++) This reads matrix i=0, 0<2 (T)
{ of order 2X2. j=2, 2<2 (F)
for(j=0;j<2;j++) ------------------- -------------------
{ i=0, 0<2 (T) i=1, 1<2 (T)
scanf(“%d”,&a[i][j]); j=0, 0<2 (T) j=0, 0<2 (T)
} It reads a[0][0] It reads a[1][0]
} ------------------- -------------------
i=0, 0<2 (T) i=1, 1<2 (T)
j=1, 1<2 (T) j=1, 1<2 (T)
It reads a[0][1] It reads a[1][1]
Q17: How will you pass 2D arrays to functions. Explain with an example
A17: 2D arrays are passed to functions with the array name (same as 1D
arrays). Here we need to specify the number of rows and columns in the
declaration and definition.
Ex: To read and display matrix.
#include<stdio.h>
#include<stdlib.h>
void read_matrix(int a[100][100],int rows,int cols);
void display_matrix(int a[100][100],int rows,int cols);

main()
{
int a[100][100];
int rows;
int cols;
int test;
printf(“Enter number of rows and columns\n”);
test = scanf(“%d%d”,&rows,&cols);
if(test!=2)
{
printf(“Enter valid data\n”);
exit(0);
}
if(rows<=0 || cols<=0)
{
printf(“Order of matrix should be greater than Zero\n”);
}
else if(rows>100 || cols>100)
{
printf(“Order of matrix should be less than %d X %d\n”,rows,cols);
}
else
{
read_martix(a,rows,cols);
display_matrix(a,rows,cols);
}
}
void read_matrix(int a[100][100],int rows, int cols)
{
int i;
int j;
int test;
for(i=0;i<rows;i++)
{
for(j=0;j<cols;j++)
{
test = scanf(“%d”,&a[i][j]);
if(test!=1)
{
printf(“Enter valid data\n”);
exit(0);
}
}
}
}
--------------------------------------------------------------------------------------
void display_matrix(int a[100][100],int rows, int cols)
{
int i;
int j;
for(i=0;i<rows;i++)
{
for(j=0;j<cols;j++)
{
printf(“%d\t”,a[i][j]);
}
printf(“\n”);
}
}
Q18: Write a modular C program to find trace and norm of a matrix.
Trace: sum of principal diagonal elements
Norm: √sum of square of all matrix elements
A18: We can find trace only if the matrix is square.
#include<stdio.h>
#include<math.h>
#include<stdlib.h>

void read_matrix(int a[100][100],int rows,int cols);


void display_matrix(int a[100][100],int rows,int cols);
int trace(int a[100][100],int rows,int cols);
float norm(int a[100][100],int rows,int cols);

main()
{
int a[100][100];
int rows;
int cols;
int t;
float n;
int test;

printf(“Enter number of rows and columns\n”);


test = scanf(“%d%d”,&rows,&cols);
if(test!=2)
{
printf(“Enter valid data\n”);
exit(0);
}
if(rows<=0 || cols<=0)
{
printf(“Order of matrix should be greater than Zero\n”);
}
else if(rows>100 || cols>100)
{
printf(“Order of matrix should be less than %d X %d\n”,rows,cols);
}
else if( rows!=cols)
{
printf(“Matrix should be square\n”);
}
else
{
read_martix(a,rows,cols);
display_matrix(a,rows,cols);
t = trace(a,rows,cols);
n = norm(a,rows,cols);
printf(“Trace of matrix = %d\n”,t);
printf(“Norm of matrix = %f\n”,n);
}
}

void read_matrix(int a[100][100],int rows, int cols)


{
int i;
int j;
int test;
for(i=0;i<rows;i++)
{
for(j=0;j<cols;j++)
{
test = scanf(“%d”,&a[i][j]);
if(test!=1)
{
printf(“Enter valid data\n”);
exit(0);
}
}
}
}
--------------------------------------------------------------------------------------
void display_matrix(int a[100][100],int rows, int cols)
{
int i;
int j;
for(i=0;i<rows;i++)
{
for(j=0;j<cols;j++)
{
printf(“%d\t”,a[i][j]);
}
printf(“\n”);
}
}
--------------------------------------------------------------------------------------
void trace(int a[100][100],int rows, int cols)
{
int i;
int j;
int sum = 0;
for(i=0;i<rows;i++)
{
for(j=0;j<cols;j++)
{
if(i==j)
sum = sum + a[i][j];
}
}
return sum;
}
float norm(int a[100][100],int rows, int cols)
{
int i;
int j;
float sum = 0;
for(i=0;i<rows;i++)
{
for(j=0;j<cols;j++)
{
sum = sum + a[i][j]*a[i][j];
}
}
return(sqrt(sum));
}
Q19: Write a modular C program to find sum of secondary diagonal
elements.
A19: In the following matrix,
a[0][0] a[0][1] a[0][2]
1 2 3
a[1][0] a[1][1] a[1][2]
4 5 6
a[2][0] a[2][1] a[2][2]
7 8 9
The secondary diagonal elements are 3,5 and 7. See that sum of secondary
diagonal elements is 2. (Number of rows-1)

#include<stdio.h>
#include<stdlib.h>

void read_matrix( float a[100][100],int row,int col);


void display_matrix( float a[100][100],int row,int col);
float sum(float a[100][100],int row,int col);
main()
{
float a[100][100];
int row;
int col;
float s;
int test;
printf(“Enter number of rows and columns\n”);
test = scanf(“%d%d”,&row,&col);
if(test!=2)
{
printf(“Enter valid data\n”);
exit(0);
}
if(row<=0 || col<=0)
{
printf(“Order of matrix should be greater than Zero\n”);
}
else if(row>100 || col>100)
{
printf(“Order of matrix should be less than %d X %d\n”,rows,cols);
}
else if( row!=col)
{
printf(“Matrix should be square\n”);
}
else
{
read_martix(a,row,col);
display_matrix(a,row,col);
s = sum(a,row,col);
printf(“Sum of secondary diagonal elments = %f\n”,s);
}
}

void read_matrix( float a[100][100],int row, int col)


{
int i;
int j;
int test;
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
test = scanf(“%f”,&a[i][j]);
if(test!=1)
{
printf(“Enter valid data\n”);
exit(0);
}
}
}
}
--------------------------------------------------------------------------------------
void display_matrix( float a[100][100],int row, int col)
{
int i;
int j;
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
printf(“%f\t”,a[i][j]);
}
printf(“\n”);
}
}
---------------------------------------------------------------------------------------
float sum(float a[100][100],int row,int col)
{
float s = 0;
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
if(i+j == row-1)
s= s + a[i][j];
}
}
return s;
}
Q20: Write a modular C program to print all the upper diagonal elements
A20:

a[0][0] a[0][1] a[0][2]


1 2 3
a[1][0] a[1][1] a[1][2]
4 5 6
a[2][0] a[2][1] a[2][2]
7 8 9
The upper diagonal elements are 2,3 and 6. See that
column_number>row_number in upper triangular matrix.

#include<stdio.h>
#include<stdlib.h>

void read_matrix( float a[100][100],int row,int col);


void display_matrix( float a[100][100],int row,int col);
void print_upper_diagonal(float a[100][100],int row,int col);
main()
{
float a[100][100];
int row;
int col;
int test;
printf(“Enter number of rows and columns\n”);
test = scanf(“%d%d”,&row,&col);
if(test!=2)
{
printf(“Enter valid data\n”);
exit(0);
}
if(row<=0 || col<=0)
{
printf(“Order of matrix should be greater than Zero\n”);
}
else if(row>100 || col>100)
{
printf(“Order of matrix should be less than %d X %d\n”,rows,cols);
}
else if( row!=col)
{
printf(“Matrix should be square\n”);
}
else
{
read_martix(a,row,col);
display_matrix(a,row,col);
print_upper_diagonal(a, row,col);
}
}

void read_matrix( float a[100][100],int row, int col)


{
int i;
int j;
int test;
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
test = scanf(“%f”,&a[i][j]);
if(test!=1)
{
printf(“Enter valid data\n”);
exit(0);
}
}
}
}
--------------------------------------------------------------------------------------
void display_matrix( float a[100][100],int row, int col)
{
int i;
int j;
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
printf(“%f\t”,a[i][j]);
}
printf(“\n”);
}
}
---------------------------------------------------------------------------------------
void print_upper_diagonal(float a[100][100],int row,int col)
{
int i;
int j;
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{

if(j>i)
printf(“%f\t”,a[i][j]);
}
}
}
Q21: Write a modular C program to find sum of each row of a matrix.
A21:
a[0][0] a[0][1] a[0][2]
1 2 3
a[1][0] a[1][1] a[1][2]
4 5 6
a[2][0] a[2][1] a[2][2]
7 8 9
The sum of each row is:
Row 0: 6
Row 1: 15
Row 2: 24
Note: In case of row sum the row number remains fixed and column
number changes.
#include<stdio.h>
#include<stdlib.h>
void read_matrix( float a[100][100],int row,int col);
void display_matrix( float a[100][100],int row,int col);
void sum_each_row(float a[100][100],int row,int col);
main()
{
float a[100][100];
int row;
int col;
int test;

printf(“Enter number of rows and columns\n”);


test = scanf(“%d%d”,&row,&col);
if(test!=2)
{
printf(“Enter valid data\n”);
exit(0);
}
if(row<=0 || col<=0)
{
printf(“Order of matrix should be greater than Zero\n”);
}
else if(row>100 || col>100)
{
printf(“Order of matrix should be less than %d X %d\n”,rows,cols);
}
else
{
read_martix(a,row,col);
display_matrix(a,row,col);
sum_each_row(a, row,col);
}
}
------------------------------------------------------------------------------------
void read_matrix( float a[100][100],int row, int col)
{
int i;
int j;
int test;

for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
test = scanf(“%f”,&a[i][j]);
if(test!=1)
{
printf(“Enter valid data\n”);
exit(0);
}
}
}
}
--------------------------------------------------------------------------------------
void display_matrix( float a[100][100],int row, int col)
{
int i;
int j;
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
printf(“%f\t”,a[i][j]);
}
printf(“\n”);
}
}
---------------------------------------------------------------------------------------
void sum_each_row(float a[100][100],int row,int col)
{
float sum;
int i;
int j;
for(i=0;i<row;i++)
{
sum = 0 ; //Sum should be made ZERO for each row.
for(j=0;j<col;j++)
{
sum = sum+a[i][j];
}
printf(“Sum of Row %d = %f\”,i,sum);
}
}
Q22: Write a modular C program to find maximum element in a matrix.
A22:
#include<stdio.h>
#include<stdlib.h>

void read_matrix( float a[100][100],int row,int col);


void display_matrix( float a[100][100],int row,int col);
void findMax(float a[100][100],int row,int col);

main()
{
float a[100][100];
int row;
int col;
int test;

printf(“Enter number of rows and columns\n”);


test = scanf(“%d%d”,&row,&col);
if(test!=2)
{
printf(“Enter valid data\n”);
exit(0);
}
if(row<=0 || col<=0)
{
printf(“Order of matrix should be greater than Zero\n”);
}
else if(row>100 || col>100)
{
printf(“Order of matrix should be less than %d X %d\n”,rows,cols);
}
else
{
read_martix(a,row,col);
display_matrix(a,row,col);
findMax(a, row,col);
}
}
------------------------------------------------------------------------------------
void read_matrix( float a[100][100],int row, int col)
{
int i;
int j;
int test;
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
test = scanf(“%f”,&a[i][j]);
if(test!=1)
{
printf(“Enter valid data\n”);
exit(0);
}
}
}
}
--------------------------------------------------------------------------------------
void display_matrix( float a[100][100],int row, int col)
{
int i;
int j;
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
printf(“%f\t”,a[i][j]);
}
printf(“\n”);
}
}
---------------------------------------------------------------------------------------
void findMax(float a[100][100],int row,int col)
{
int i;
int j;
float max = a[0][0]; //Assuming 1st element is max

for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
if(a[i][j]>max)
max=a[i][j];
}
}
printf(“Maximum element of matrix = %f\n”,max);
}
Q23: Write a modular C program to find maximum element in each
column.
A23:
a[0][0] a[0][1] a[0][2]
1 8 3
a[1][0] a[1][1] a[1][2]
6 5 7
a[2][0] a[2][1] a[2][2]
9 2 4
Maximum element in each column:
Column 0: 9
Column 1: 8
Column 2: 7

Note: In order to access the elements column by column, the column


number remains fixed and row number changes.

#include<stdio.h>
#include<stdlib.h>
void read_matrix( float a[100][100],int row,int col);
void display_matrix( float a[100][100],int row,int col);
void findMax(float a[100][100],int row,int col);
main()
{
float a[100][100];
int row;
int col;
int test;

printf(“Enter number of rows and columns\n”);


test = scanf(“%d%d”,&row,&col);
if(test!=2)
{
printf(“Enter valid data\n”);
exit(0);
}
if(row<=0 || col<=0)
{
printf(“Order of matrix should be greater than Zero\n”);
}
else if(row>100 || col>100)
{
printf(“Order of matrix should be less than %d X %d\n”,rows,cols);
}
else
{
read_martix(a,row,col);
display_matrix(a,row,col);
findMax(a, row,col);
}
}
------------------------------------------------------------------------------------
void read_matrix( float a[100][100],int row, int col)
{
int i;
int j;
int test;
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
test = scanf(“%f”,&a[i][j]);
if(test!=1)
{
printf(“Enter valid data\n”);
exit(0);
}
}
}
}
--------------------------------------------------------------------------------------
void display_matrix( float a[100][100],int row, int col)
{
int i;
int j;
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
printf(“%f\t”,a[i][j]);
}
printf(“\n”);
}
}
---------------------------------------------------------------------------------------
void findMax(float a[100][100],int row,int col)
{
float max;
int i;
int j;
for(j=0;j<col;j++)
{
max=a[j][0]; //1st element of each column is assumed as max
for(i=0;i<row;i++)
{
if(a[i][j]>max)
max=a[i][j];
}
printf(“Maximum element in Column %d = %f\n”,j,max);
}
}
Q24: Write a modular C program to find transpose of a matrix.
A24: #include<stdio.h>
#include<stdlib.h>
void read_matrix( float a[100][100],int row,int col);
void display_matrix( float a[100][100],int row,int col);
void transpose(float a[100][100],int row,int col);
main()
{
float a[100][100];
int row;
int col;
int test;
printf(“Enter number of rows and columns\n”);
test = scanf(“%d%d”,&row,&col);
if(test!=2)
{
printf(“Enter valid data\n”);
exit(0);
}
if(row<=0 || col<=0)
{
printf(“Order of matrix should be greater than Zero\n”);
}
else if(row>100 || col>100)
{
printf(“Order of matrix should be less than %d X %d\n”,rows,cols);
}
else
{
read_martix(a,row,col);
display_matrix(a,row,col);
transpose(a, row,col);
}
}
------------------------------------------------------------------------------------
void read_matrix( float a[100][100],int row, int col)
{
int i;
int j;
int test;
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
test = scanf(“%f”,&a[i][j]);
if(test!=1)
{
printf(“Enter valid data\n”);
exit(0);
}
}
}
}
--------------------------------------------------------------------------------------
void display_matrix( float a[100][100],int row, int col)
{
int i;
int j;
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
printf(“%f\t”,a[i][j]);
}
printf(“\n”);
}
}
---------------------------------------------------------------------------------------
void transpose(float a[100][100],int row,int col)
{
float aTranspose[100][100];
int i;
int j;
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
aTranspose[j][i] = a[i][j];
}
}
//Display the transposed matrix
printf(“Transpose of matrix:\n”);

//In the transposed matrix row changes to col and col to row.
display_matrix(aTranspose,col,row);
}
Q25: Write a modular C Program to add two matrices.
A25: #include<stdio.h>
#include<stdlib.h>
void read_matrix( float a[100][100],int row,int col);
void display_matrix( float a[100][100],int row,int col);
void add_matrix(float a[100][100],float b[100][100],int row,int col);
main()
{
float a[100][100];
float b[100][100];
int row1;
int col1;
int row2;
int col2;
int test;
printf(“Enter number of rows and columns for matrix1\n”);
test = scanf(“%d%d”,&row1,&col1);
if(test!=2)
{
printf(“Enter valid data\n”);
exit(0);
}
printf(“Enter number of rows and columns for matrix2\n”);
test = scanf(“%d%d”,&row2,&col2);
if(test!=2)
{
printf(“Enter valid data\n”);
exit(0);
}

if(row1<=0 || col1<=0 || row2<=0 || col2<=0)


{
printf(“Order of matrix should be greater than Zero\n”);
}
else if(row1>100 || col1>100 || row2>100 || col2>100)
{
printf(“Order of matrix should be less than %d X %d\n”,100,100);
}
else if(row1!=row2 || col1!=col2)
{
printf(“Order of both matrix should be same\n”);
}
else
{
printf(“Enter elements of matrix 1\n”);
read_matrix(a,row1,col1);
printf(“Enter elements of matrix 2\n”);
read_matrix(b,row2,col2);
}
}
------------------------------------------------------------------------------------
void read_matrix( float a[100][100],int row, int col)
{
int i;
int j;
int test;
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
test = scanf(“%f”,&a[i][j]);
if(test!=1)
{
printf(“Enter valid data\n”);
exit(0);
}
}
}
}
--------------------------------------------------------------------------------------
void display_matrix( float a[100][100],int row, int col)
{
int i;
int j;
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
printf(“%f\t”,a[i][j]);
}
printf(“\n”);
}
}
---------------------------------------------------------------------------------------
void add(float a[100][100],float b[100][100],int row,int col)
{
float c[100][100];
int i;
int j;
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
c[i][j] = a[i][j] + b[i][j];
}
}
//Display the resultatnt matrix
display_matrix(a,row,col);
printf(“\t\t+\n”);
display_matrix(b,row,col);
printf(“Addition of matrix:\n”);
display_matrix(c,row,col);
}
Q26: Write a modular C Program to multiply two matrices.
A26: #include<stdio.h>
#include<stdlib.h>
void read_matrix( float a[100][100],int row,int col);
void display_matrix( float a[100][100],int row,int col);
void multiply_matrix(float a[100][100],float b[100][100],int row1,int
col1,int row2,int col2);
main()
{
float a[100][100];
float b[100][100];
int row1;
int col1;
int row2;
int col2;
int test;
printf(“Enter number of rows and columns for matrix1\n”);
test = scanf(“%d%d”,&row1,&col1);
if(test!=2)
{
printf(“Enter valid data\n”);
exit(0);
}
printf(“Enter number of rows and columns for matrix2\n”);
test = scanf(“%d%d”,&row2,&col2);
if(test!=2)
{
printf(“Enter valid data\n”);
exit(0);
}
if(row1<=0 || col1<=0 || row2<=0 || col2<=0)
{
printf(“Order of matrix should be greater than Zero\n”);
}
else if(row1>100 || col1>100 || row2>100 || col2>100)
{
printf(“Order of matrix should be less than %d X %d\n”,100,100);
}
else if(col1!=row2)
{
printf(“Column of matrix1 should be equal to row of matrix 2\n”);
}
else
{
printf(“Enter elements of matrix 1\n”);
read_matrix(a,row1,col1);
printf(“Enter elements of matrix 2\n”);
read_matrix(b,row2,col2);
multiply_matrix(a,b,row1,col1,row2,col2);
}
}
------------------------------------------------------------------------------------
void read_matrix( float a[100][100],int row, int col)
{
int i;
int j;
int test;
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
test = scanf(“%f”,&a[i][j]);
if(test!=1)
{
printf(“Enter valid data\n”);
exit(0);
}
}
}
}
--------------------------------------------------------------------------------------
void display_matrix( float a[100][100],int row, int col)
{
int i;
int j;
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
printf(“%f\t”,a[i][j]);
}
printf(“\n”);
}
}
---------------------------------------------------------------------------------------
void multiply_matrix(float a[100][100],float b[100][100],int row1,int
col1,int row2,int col2)
{
int i,j,k;
float c[100][100]={0};
for(i=0;i<row1;i++)
{
for(j=0;j<col2;j++)
{
for(k=0;k<row2;k++)
{
c[i][j] = c[i][j] + a[i][k] * b[k][j];
}
}
}
//Display the resultant matrix
display_matrix(a,row1,col1);
printf(“\t\t*\n”);
display_matrix(b,row2,col2);
printf(“\t=\n”);
display_matrix(c,row1,col2);
}
Q27: Write a modular C program to find if matrix is identity or not.
A27: A matrix is identity if principal diagonal elementa are one and rest
all elements are zero.
#include<stdio.h>
#include<stdlib.h>
void read_matrix( float a[100][100],int row,int col);
void display_matrix( float a[100][100],int row,int col);
void check_identity(float a[100][100],int row,int col);
main()
{
float a[100][100];
int row;
int col;
int test;
printf(“Enter number of rows and columns\n”);
test = scanf(“%d%d”,&row,&col);
if(test!=2)
{
printf(“Enter valid data\n”);
exit(0);
}
if(row<=0 || col<=0)
{
printf(“Order of matrix should be greater than Zero\n”);
}
else if(row>100 || col>100)
{
printf(“Order of matrix should be less than %d X %d\n”,rows,cols);
}
else if( row!=col)
{
printf(“Matrix should be square\n”);
}
else
{
read_martix(a,row,col);
display_matrix(a,row,col);
check_identity(a, row,col);
}
}
void read_matrix( float a[100][100],int row, int col)
{
int i;
int j;
int test;
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
test = scanf(“%f”,&a[i][j]);
if(test!=1)
{
printf(“Enter valid data\n”);
exit(0);
}
}
}
}
--------------------------------------------------------------------------------------
void display_matrix( float a[100][100],int row, int col)
{
int i;
int j;
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
printf(“%f\t”,a[i][j]);
}
printf(“\n”);
}
}
---------------------------------------------------------------------------------------
void check_identity(float a[100][100],int row,int col)
{
int i;
int j;
int flag = 0;
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
if(i==j && a[i][j]!=1)
flag = 1;
if(i!=j && a[i][j]!=0)
flag =1;
}
}
if(flag==0)
printf(“Matrix is Identity\n”);
else
printf(“Matrix is not identity\n”);
}
Q28: Write a modular C program to generate pascal triangle.
A28:
#include<stdio.h>
#include<stdlib.h>

#define MAX 100

void generateTriangle(int a[100][100],int lines);

main()
{
int a[MAX][MAX]={0};
int test;
int lines;

printf(“Enter the number of lines to be displayed\n”);


test = scanf(“%d”,&lines);

if(test!=1)
{
printf(“Enter integers only\n”);
exit(0);
}

else if(lines<=0)
printf(“Number of lines should be greater than zero\n”);

else if(lines>MAX)
printf(“Nunber of lines should be less than %d\n”,MAX);

else
{
generateTriangle(a,lines);
}
}

void generateTriangle(int a[100][100],int lines)


{
int row =2;
int col, i, j;
a[0][0] = a[1][0] = a[1][1] = 1;

while(row<=lines)
{
a[row][0] = 1;
for(col=1;col<=row;col++)
a[row][col]=a[row-1][col-1] + a[row-1][col];

row++;
}

for(i=0;i<lines;i++)
{
printf(“\n”);
for(j=0;j<=i;j++)
printf(“%d\t”,a[i][j]);
}
}
Q29: Write a modular C program to check if the matrix is sparse matrix or
dense matrix.
A29:
#include<stdio.h>
#include<stdlib.h>

#define MAX 100

void readArray(float a[100][100], int rows, int cols);


int isSparse(float a[100][100], int rows, int cols);
main()
{
float a[100][100];
int row;
int col;
int test;
int result;
printf(“Enter number of rows and columns\n”);
test = scanf(“%d%d”,&row,&col);
if(test!=2)
{
printf(“Enter valid data\n”);
exit(0);
}
if(row<=0 || col<=0)
{
printf(“Order of matrix should be greater than Zero\n”);
}
else if(row>100 || col>100)
{
printf(“Order of matrix should be less than %d X %d\n”,MAX,MAX);
}
else if( row!=col)
{
printf(“Matrix should be square\n”);
}
else
{
read_martix(a,row,col);
result = isSparse(a,row,col)
if(result == 1)
printf(“Matrix is sparse\n”);
else if(result == 2)
printf(“Matrix is dense\n”);
else
printf(“Matrix is neither sparse nor dense\n”);
}
}
void read_matrix( float a[100][100],int row, int col)
{
int i;
int j;
int test;
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
test = scanf(“%f”,&a[i][j]);
if(test!=1)
{
printf(“Enter valid data\n”);
exit(0);
}
}
}
}

int isSparse(float a[100][100], int rows, int cols)


{
int zero_count = 0;
int non_zero_count = 0;

int i,j;

for(i=0;i<rows;i++)
{
for(j=0;j<cols;j++)
{
if(a[i][j]==0)
zero_count++;
else
non_zero_count++;
}
}

if(zero_count == non_zero_count)
return 3;
else if(zero_count > non_zero_count)
return 1;

else
return 2;
}

Try it yourself (2D Arrays)


Q1: Write a modular C program to print lower triangular elements.
Q2: Write a modular C program to find sum of each column.
Q3: Write a modular C program to find minimum element of matrix
Q4: Write a modular C program to find maximum element in each row
Q5: Write a modular C program to find minimum element in each column
Q6: Write a modular C program to check if the matrix is scalar or not.
Q7: In a small company there are five salesmen. Each salesman is
supposed to sell three products. Write a program using appropriate
construct to print
i)the total sales by each salesman
ii)total sales of each item
Q8: Consider an array MARKS[20][5] which stores the marks obtained
by 20 students in 5 subjects. Write a program to
i)find average marks obtained in each subject
ii)find number of students who have scored below 50 in their average
iii)find average marks obtained by every student
Q9: The temperature is measured 3 times (morning, noon and night) a day
for 7 days. Write a modular C program using 2D arrays to:-
• Read the temperature measurements and store in a 2D arrays
• Display all temperature readings
• Find the average temperature for morning time (across 7 days)
Find the maximum temperature for day 5

Solutions (Try it yourself) : 2D Arrays


A1: Refer the solution to upper triangular matrix.
A2: Refer the solution to sum of each row in a matrix.
A3: Refer solution to maximum element of matrix
A4: Try it yourself
A5: Try it yourself
A6:
int flag = 0;
int element = a[0][0];
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
if(i==j && a[i][j]!=element)
flag = 1;
if(i!=j && a[i][j]!=0)
flag =1;
}
}
if(flag==0)
printf(“Matrix is Identity\n”);
else
printf(“Matrix is not identity\n”);
A7: It is same as row sum and column sum. Represent sales of salesman
in row and item in column.
A8: It is same as row sum and column sum. Represent students
roll_number as rows and marks as columns.
A9:It is same as row sum and column sum. Represent days as rows and
temperature as columns.

You might also like