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;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.