Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ PROGRAM - ARRAY Create a two dimensional array called Matrix1 of size 3X3. P

ID: 3580039 • Letter: C

Question

C++ PROGRAM - ARRAY

Create a two dimensional array called Matrix1 of size 3X3.

Populate it with 1 through 9 using nested loops.

Print Matrix1 row by row, it must look like:

1          4          7

2          5          8

3          6          9

Pass Matrix1 to a function called diagonal, in which you will change only the diagonal values to zero.

Print Matrix1 which now looks like:

0          4          7         

2          0          8

3          6          0

No extra/unnecessary variables are allowed. No hardcoding is acceptable. You must accomplish this with as few lines of code as possible.  

Explanation / Answer

#include <iostream>

using namespace std;
void diagonal(int Matrix1[][3], int rows, int cols){
for(int i=0; i<rows; i++){
Matrix1[i][i]=0;
}
  
}
int main()
{
int Matrix1[3][3];
  
for(int i=0,k=1; i<3; i++){
for(int j=0; j<3; j++,k++){
Matrix1[i][j] = k;
}
}
cout<<"Matrix is "<<endl;
for(int i=0; i<3; i++){
for(int j=0; j<3; j++){
cout<<Matrix1[i][j]<<" ";
}
cout<<endl;
}
diagonal(Matrix1,3,3);
cout<<"Matrix is "<<endl;
for(int i=0; i<3; i++){
for(int j=0; j<3; j++){
cout<<Matrix1[i][j]<<" ";
}
cout<<endl;
}
return 0;
}

Output:

sh-4.2$ g++ -o main *.cpp                                                                                                                                                                                                                              

sh-4.2$ g++ -o main *.cpp                                                                                                                                                                                                                              

sh-4.2$ main                                                                                                                                                                                                                                           

Matrix is                                                                                                                                                                                                                                              

1 2 3                                                                                                                                                                                                                                                  

4 5 6                                                                                                                                                                                                                                                  

7 8 9                                                                                                                                                                                                                                                  

Matrix is                                                                                                                                                                                                                                              

0 2 3                                                                                                                                                                                                                                                  

4 0 6                                                                                                                                                                                                                                                  

7 8 0

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote