2D Matrix Modification
A Matrix has R rows and C columns. Get the matrix as input and multiply the value in the cells of the matrix by a value E if it is even and multiply it by a value F if it is odd.
Input Format:
The first line contains R.
The second line contains C.
Next R lines containing the elements of the matrix with each column value seperated by a space.
R+3 line contains E.
R+4 line contains F.
The first line contains R.
The second line contains C.
Next R lines containing the elements of the matrix with each column value seperated by a space.
R+3 line contains E.
R+4 line contains F.
Output Format:
R lines containing the elements of the revised matrix with each column value seperated by a space.
Boundary Conditions:
1 <= R <= 100
1 <= C <= 100
1 <= E,F <= 100
1 <= R <= 100
1 <= C <= 100
1 <= E,F <= 100
Example Input/Output 1:
Input:
3
3
1 2 3
1 2 3
1 2 3
2
3
Input:
3
3
1 2 3
1 2 3
1 2 3
2
3
Output:
3 4 9
3 4 9
3 4 9
3 4 9
3 4 9
3 4 9
Example Input/Output 2:
Input:
4
5
12 19 16 18 12
2 12 14 5 12
8 3 17 4 19
18 11 5 14 9
2
5
Input:
4
5
12 19 16 18 12
2 12 14 5 12
8 3 17 4 19
18 11 5 14 9
2
5
Output:
24 95 32 36 24
4 24 28 25 24
16 15 85 8 95
36 55 25 28 45
24 95 32 36 24
4 24 28 25 24
16 15 85 8 95
36 55 25 28 45
Code:
#include <iostream>
using namespace std;
int main(int argc, char** argv)
{
int i,j,r,c,e,f;
cin>>r>>c;
int arr[r][c];
for(i=0;i<r;i++)
for(j=0;j<c;j++)
cin>>arr[i][j];
cin>>e>>f;
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
if(arr[i][j]%2==0)
cout<<arr[i][j]*e<<" ";
else
cout<<arr[i][j]*f<<" ";
}
cout<<"\n";
}
}
No comments:
Post a Comment