0% found this document useful (0 votes)
14 views13 pages

C Record

Uploaded by

peddapatiabhinay
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)
14 views13 pages

C Record

Uploaded by

peddapatiabhinay
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/ 13

WEEK 7:

A) Sum of 2-D arrays


Aim: To find the sum of two matrices (2-D arrays).
Description: Consider two matrices A(mxn) and B(pxq).
Precondition for sum of matrices is (m==p)&&(n==q).
2 5 3 4
Ex: A = [ ] B=[ ]
6 4 2 5
Sum = A+B
2 5 3 4
C =[ ]+[ ]
6 4 2 5
2 (𝑎[0][0]) + 3( 𝑏[0][0]) 5 (𝑎[0][1]) + 4 (𝑏[0][1])
=[ ]
6( 𝑎[1][0]) + 2 (𝑏[1][0]) 4 (𝑎[1][1]) + 5 (𝑏[1][1])

5(c[0][0]) 9(𝑐[0][1])
C =[ ]
8(𝑐[1][0]) 9(𝑐[1][1])
Program:
#include<stdio.h>
int main()
{
int a[10][10],b[10][10],c[10][10],m,n,p,q,i,j;
printf("enter no.of rows and colms of matrix A:");
scanf("%d,%d",&m,&n);
printf("enter no.of rows and colms of matrix B:");
scanf("%d,%d",&p,&q);
if((m==p)&&(n==q))
{
printf("enter %d elements into A:",m*n);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("enter %d elements into B:",p*q);
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
{
scanf("%d",&b[i][j]);
}
}
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
c[i][j]=a[i][j]+b[i][j];
}printf("\n");
}
printf("elements of matrix C:\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%d ",c[i][j]);
}printf("\n");
}
}else
{
printf("matrix addition not possible:");
}return 0;
}
Output:
1) enter no.of rows and colms of matrix A:2,2
enter no.of rows and colms of matrix B:3,3
matrix addition not possible
2) enter no.of rows and colms of matrix A:3,3
enter no.of rows and colms of matrix B:3,3
enter 9 elements into A:
123
456
789
enter 9 elements into B:
123
456
789
elements of matrix C:
246
8 10 12
14 16 18
WEEK 7:

B) Multiplication of two 2-D arrays

Aim: To find multiplication of two matrices (2-D arrays)

Description: Take two matrices A(mxn) and B(pxq).

Precondition for multiplication of matrices is (n=p)


2 4
2 4 3
Ex: A = [ ] B = [3 3]
4 3 2
4 2
Multiplication of matrices = A x B
2 4
2 4 3
C =[ ] x [3 3]
4 3 2
4 2

2(𝑎[0][0]) x 2(b[0][0]) + 4(a[0][1])x3(b[1][0]) + 3(a[0][2])x4(b[2][0]) 2x4 + 4x3 + 3x2


=⌈ ⌉
4 x 2 +3 x 3 + 2 x 4 4x4 + 3x3 + 2x2
4 + 12 + 12 8 + 12 + 6
=[ ]
8+9+8 16 + 9 + 4
28(𝑐[0][0]) 26(𝑐[0][1])
C=[ ]
25(𝑐[1][0]) 29(𝑐[1][1])
Program:

#include<stdio.h>

int main()

int a[10][10],b[10][10],c[10][10],m,n,p,q,i,j,k;

printf("enter no.of rows and colms of matrix A:");

scanf("%d,%d",&m,&n);

printf("enter no.of rows and colms of matrix B:");

scanf("%d,%d",&p,&q);

if(n==p)

printf("enter %d elements into A:",m*n);

for(i=0;i<m;i++)

for(j=0;j<n;j++)

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

printf("enter %d elements into B:",p*q);

for(i=0;i<p;i++)

for(j=0;j<q;j++)

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

for(i=0;i<m;i++)

for(j=0;j<n;j++)

c[i][j]=0;

for(k=0;k<n;k++)

c[i][j]=c[i][j]+a[i][k]*b[k][j];

}printf("\n");

printf("elements of matrix C:\n");

for(i=0;i<m;i++)

for(j=0;j<q;j++)

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

}printf("\n");

}else
{

printf("matrix multiplication not possible");

}return 0;

Output:

1) enter no.of rows and colms of matrix A:2,3

enter no.of rows and colms of matrix B:2,3

matrix multiplication not possible

2) enter no.of rows and colms of matrix A:2,3

enter no.of rows and colms of matrix B:3,2

enter 6 elements into A:

243

432

enter 6 elements into B:

24

33

42

elements of matrix C:

28 26

25 29
WEEK 7:
c. Transpose of a Matrix
AIM: To find the transpose of a Matrix.
DESCRIPTION:
The transpose of a matrix is a new matrix that is obtained by exchanging the rows and
columns.
Note: The transpose of an m × n matrix will result in an n × m matrix.
To transpose a matrix in C programming language, we can follow the following steps:
Declare the matrix: First, we need to declare a two-dimensional array to represent our
matrix
Initialize the matrix: Next, we need to initialize the matrix with some values. We can do this
using a nested for loop
Transpose the matrix: Once we have our matrix initialized, we can transpose it using another
nested for loop with the help of indices.
Display the transposed matrix
For example: if the matrix is

1 2
3 4
5 6
Then the resultant matrix will be

1 3 5
2 4 6

PROGRAM:
#include<stdio.h>
int main()
{
int a[100][100],b[100][100];
int c[100][100],i,j,m,n,t;
printf("Enter the rows of matrix A:");
scanf("%d",&m);
printf("Enter the columns of matrix A:");
scanf("%d",&n);
printf("Enter the %d elements of matrix A:\n",m*n);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
}
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
if(j==i)
{
c[i][j]=a[i][j];
}
if(j>i)
{
t=a[j][i];
c[j][i]=a[i][j];
c[i][j]=t;
}
}
}
printf("The resultant matrix is:\n");
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
printf("%3d",c[i][j]);
}
printf("\n");
}

return 0;
}
OUTPUT:
Enter the rows of matrix A:3
Enter the columns of matrix A:2
Enter the 6 elements of matrix A:
12
34
56
The resultant matrix is:
1 3 5
2 4 6
WEEK 7:

D ) Trace of matrix

Aim: To find trace of matrix.

Description: Sum of diagnol elements of a square matrix is known as trace of matrix. Precondition is
it must be a square matrix.
1 2 3
Ex: A = [4 5 6]
7 8 9
Trace of matrix A is 1+5+9=15.

Program:

#include<stdio.h>

int main()

int a[10][10],m,n,i,j,s;

printf("enter no.of rows and colms:");

scanf("%d,%d",&m,&n);

if(m==n)

printf("enter %d elements\n",m*n);

for(i=0;i<m;i++)

for(j=0;j<n;j++)

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

}printf("matrix elements are:\n");

for(i=0;i<m;i++)

for(j=0;j<n;j++)

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

}
printf("\n");

s=0;

for(i=0;i<m;i++)

for(j=0;j<n;j++)

if(i==j)

s=s+a[i][j];

}printf("trace of matrix %d",s);

else

{printf("enter square matrix");

}return 0;

Output:

1) enter no.of rows and colms:2,3

enter square matrix

2) enter no.of rows and colms:3,3

enter 9 elements

123456789

matrix elements are:

123

456

789

trace of matrix 15
WEEK 7:

E) Lower Triangular Matrix

Aim: To print lower triangle matrix in 2-D arrays

Description: Take matrix A(ixj). Precondition is it must be a square matrix.

In order to print lower triangle matrix we have to print the values upto j<=i.

Eg: a([0][0]) , a([1][1]) ,a([3][3])…….

(i) 0 1 2 3 (j)

0 00

1 10 11

2 20 21 22

3 30 31 32 33

Eg: 1

4 5

7 8 9

Program:

#include<stdio.h>

int main()

int a[10][10],m,n,i,j;

printf("enter no.of rows and colms:");

scanf("%d,%d",&m,&n);

if(m==n)

printf("enter %d elements\n",m*n);

for(i=0;i<m;i++)

for(j=0;j<n;j++)

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

}
printf("lower matrix elements are:\n");

for(i=0;i<m;i++)

for(j=0;j<=i;j++)

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

printf("\n");

}else

{printf("enter square matrix");

}return 0;

Output:

1) enter no.of rows and colms:3,2

enter square matrix

2) enter no.of rows and colms:3,3

enter 9 elements

123456789

lower matrix elements are:

45

789

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy