Patch Up Two Matrices

Patch Up Two Matrices

For this challenge, you need to take 2 matrices as an input from the stdin , add them and print the resultant matrix to the stdout.

Input Format
Two matrices to be taken as an input. 
For each matrix, on first line you need to tell that how many rows and columns your matrix need to have and these values should be separated by space. 
Then after that, each line will represent will represent each row and you need to enter numbers which each rows should have separated by a space. 

Constraints
1 <  (n,m) < 100
1 <  (p,q) < 100

Output Format
Print the resultant matrix to the stdout where each each line should represent
Note : Please do not include space after the numbers which are in the last column as it will affect your submission and you will not get marks.
each row and values in the row should be separated by a space. 


Sample TestCase 1
Input
3 3
1 2 3
4 5 6
7 8 9
3 3
2 3 4
5 6 7
7 8 9
Output
3 5 7
9 11 13
14 16 18
Explanation
Two matrices must have an equal number of rows and columns to be added. The sum of two matrices A and B will be a matrix which has the same number of rows and columns as do A and B. The sum of A and B, denoted A + B, is computed by adding corresponding elements of A and B.


OUTPUT:

#include<stdio.h>
#include<stdlib.h>
int main()
{
int *aa,*bb;
int m,n,p,q;
int i,j,x,y;
scanf("%d %d",&m,&n);
x = m*n;
aa = (int *)malloc(x * sizeof(int));
for(i=0;i<x;i++)
scanf("%d",&aa[i]);
scanf("%d %d",&p,&q);
y = p*q;
bb = (int *)malloc(y * sizeof(int));
for(i=0;i<x;i++)
scanf("%d",&bb[i]);
for(i=0;i<x;i++)
aa[i] = aa[i] + bb[i];
for(i=0;i<x;i++)
{
j = 0;
while(j<n)
{
printf("%d",aa[i]);
i++;
j++;
if(j!=n)
printf(" ");
}
if(i!=x)
printf("\n");
i--;
}
return 0;
}

Comments

Popular posts from this blog

Write a function that receives 5 integers and returns the sum , average and standard deviation of these numbers. Call this function from main( ) and print the result in main( ).

A 5-digit positive integer is entered through the keyboard ,write a recursive and non-recursive function sum of the 5-digit number.