0% found this document useful (0 votes)
5 views

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)
5 views

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/ 3

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][0])
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

You might also like