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

Please create a loop in the bold section of the code that exchanges the second c

ID: 3551607 • Letter: P

Question

Please create a loop in the bold section of the code that exchanges the second columm and fouth columm of the arry.



#include <iostream>

#include <iomanip>

using namespace std;


int main()

{

int arr[3][5] = {{-39, 29, 89, -71, 12},

{48, -63, 90, 2, -45},

{-59, -83, 32, 73, 13}};

// display table

int i, j;

for (i=0; i<3; i++)

{

for (j=0; j<5; j++)

cout << setw(4) << arr[i][j];

cout << endl;

}

cout << " ";

// use loop to exchange the second column and the fourth column

  

  

// display again

for (i=0; i<3; i++)

{

for (j=0; j<5; j++)

cout << setw(4) << arr[i][j];

cout << endl;

}

return 0;

}

Explanation / Answer

#include <iostream>

#include <iomanip>

using namespace std;


int main()

{

int arr[3][5] = {{-39, 29, 89, -71, 12},

{48, -63, 90, 2, -45},

{-59, -83, 32, 73, 13}};

int temp;

// display table

int i, j;

for (i=0; i<3; i++)

{

for (j=0; j<5; j++)

cout << setw(4) << arr[i][j];

cout << endl;

}

cout << " ";

// use loop to exchange the second column and the fourth column


for(j = 0; j< 3; j++){

temp = arr[j][1];

arr[j][1] = arr[j][3];

arr[j][3] = temp;

}



// display again

for (i=0; i<3; i++)

{

for (j=0; j<5; j++)

cout << setw(4) << arr[i][j];

cout << endl;

}

return 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