Create a user-defined function that will accept a 3 x 3 matrix (2D Array), set it as a parameter. Say mad [3][3]; feel free to use your initials.
Function name should InitialsMatrix (ie. MadMatrix)
The function should store 9 values to the array; it only accept values 0 and 1.
It should be outputted in a table format
0 1 1
1 1 1
1 0 0
Display the sum of values if it is an even value
Use a double for loop to implement this
Screen Layout
Input 0's and 1's to the mad [3][3] matrix
mad[0][0]:0
mad[0][1]:1
mad[0][1]:1
mad[0][1]:1
mad[0][1]:1
mad[0][1]:1
mad[0][1]:1
mad[0][1]:0
mad[0][1]:0
Output Table Format
0 1 1
1 1 1
1 0 1
Sum is 6
mad[0][0]:0
mad[0][1]:0
mad[0][1]:1
mad[0][1]:1
mad[0][1]:1
mad[0][1]:1
mad[0][1]:1
mad[0][1]:0
mad[0][1]:0
Output Table Format
0 0 1
1 1 1
1 0 0
(Sum is not displayed because it is an odd value)
using namespace std;
#define ROW 3
#define COL 3
void InitialsMatrix(int r, int c)
{
int MadMatrix[r][c],t,Sum=0;
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
t=2;
while(t<0 ||t>1)
{
cout<<"\n\tmad["<<i<<"]["<<j<<"] = "; cin>>t;
MadMatrix[i][j]=t;
}
Sum=Sum+t;
}
}
cout<<"\n\tInput Matrix: "<<"\n";
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
cout<<"\t"<<MadMatrix[i][j];
}
cout<<"\n";
}
if(Sum%2==0) cout<<"\n\n\tSum of Matrix Elements = "<<Sum;
else cout<<"\n\n\tSum is not displayed because it is an odd value.";
}
int main()
{
InitialsMatrix(ROW,COL);
}
Comments
Leave a comment