C++ Follow the following steps to code this program 1. Take two two-dimensional
ID: 3625628 • Letter: C
Question
C++Follow the following steps to code this program
1. Take two two-dimensional arrays as A[3][3]and B[3][3] and one-dimensional array C[] to keep the result.
2. Prompt the user to enter the elements of first matrix
3. Prompt the user to enter the elements of second matrix
4. Now take first element of first matrix and check in both matrix weather it is greatest or any other number is the greatest and put the greatest number at the first index of the one dimensional array. Follow this procedure for other elements and at the end one dimensional array would be in descending order
5. Display this one-Dimensional sorted array on the screen
Following is the sample program structure
2 3 5 2 8 7
A= 6 5 9 B= 0 1 1
1 0 2 4 7 9
Output should be like this
Please enter the Elements of First Matrix:
Please enter the Elements of second Matrix:
Sorted Values from Both the Matrices are:
-----------------------------------------------------------------
9 9 8 7 7 6 5 5 4 3 2 2 2 1 1 1 0 0
-----------------------------------------------------------------
C++
Explanation / Answer
please rate - thanks
#include <iostream>
#include <iomanip>
using namespace std;
void fill(int[][3]);
void print(int[][3]);
void printa(int[]);
void combine(int[][3],int[][3],int[]);
int main()
{int a[3][3],b[3][3],c[18];
cout<<"Matrix a ";
fill(a);
cout<<"Matrix b ";
fill(b);
cout<<"Matrix a ";
print(a);
cout<<"Matrix b ";
print(b);
combine(a,b,c);
cout<<"The sorted merged array ";
printa(c);
system("pause");
return 0;
}
void printa(int a[])
{int i;
for(i=0;i<18;i++)
cout<<setw(4)<<a[i];
cout<<endl;
}
void fill(int a[][3])
{int i,j;
cout<<"Enter the elements of the matrix : ";
for(i=0;i<3;i++)
for(j=0;j<3;j++)
{cout<<"Enter element ["<<i<<"]["<<j<<"]: ";
cin>>a[i][j];
}
}
void print(int a[][3])
{int i,j;
for(i=0;i<3;i++)
{for(j=0;j<3;j++)
cout<<setw(4)<<a[i][j];
cout<<" ";
}
cout<<" ";
}
void combine(int a[][3],int b[][3],int c[])
{bool atemp[3][3]={false};
bool btemp[3][3]={false};
int i,j,upto=0,it,jt,in,max;
for(upto=0;upto<18;upto++)
{
max=-99999;
for(i=0;i<3;i++)
for(j=0;j<3;j++)
if(!atemp[i][j])
if(a[i][j]>max)
{max=a[i][j];
it=i;
jt=j;
in=0;
}
for(i=0;i<3;i++)
for(j=0;j<3;j++)
if(!btemp[i][j])
if(b[i][j]>max)
{max=b[i][j];
it=i;
jt=j;
in=1;
}
c[upto]=max;
if(in==0)
atemp[it][jt]=true;
else
btemp[it][jt]=true;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.