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

I need help with the following C++ code. The part of the code that is black is t

ID: 3865395 • Letter: I

Question

I need help with the following C++ code. The part of the code that is black is the part I need help with. I am trying to swap the values in row two with the values in row three. There are five total rows. Please keep the code exactly the same. The only changes should be to the bolded part.

case '3':

       cout<<"After swapping the values in row two with the values in row three: ";

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

        {

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

        {

       user_temp[0][j] = array[1][j];

        array[0][j] = array[3][j];

        array[0][j] = user_temp[0] [j];

        cout<<array[i][j]<<" ";

        }

        cout<<endl;

        }

        break;

Explanation / Answer

Here is your code

cout<<"After swapping the values in row two with the values in row three: ";

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

        {

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

        {

       user_temp[0][j] = array[1][j];

        array[0][j] = array[3][j]; // change this to array[1][j] = array[2][j] ;

        array[0][j] = user_temp[0] [j];    // change this to array[2][j] = user_temp[0][j] ;

        cout<<array[i][j]<<" ";

        }

        cout<<endl;

        }

        break;

This will swap 2nd and 3rd rows

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

        {

       user_temp[0][j] = array[1][j];         // realy no need to use user_temp array you can use a

                                                           //simple   variable temp;

        array[1][j] = array[2][j] ; // assign 2nd row to 3rd row value

        array[2][j] = user_temp[0][j] ;    // assinn 3rd row to 2nd row value ie we stored in temp

        cout<<array[i][j]<<" ";

        }

But there is a mistake in your program , ie this will swap 5 times ,

we need only one loop to swap 2nd and 3rd row elements . ie only the second loop ie for(j=0;j<5;j++)

the loop of (i) is no need . in the case of printing , use seperate two loops .

When you run this code you don't get correct out put because when printing the second row it will swap the second time so that will be the same . then 3rd row it will same the second row .

so kindly avoid the first loop ie for(i=0;i<5;i++)

and put cout<<arra[i][j] in a seperate for loop at last

ie

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

        {

       user_temp[0][j] = array[1][j];         // realy no need to use user_temp array you can use a

                                                           //simple   variable temp;

        array[1][j] = array[2][j] ; // assign 2nd row to 3rd row value

        array[2][j] = user_temp[0][j] ;    // assinn 3rd row to 2nd row value ie we stored in temp

        }

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

        {

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

        {

        cout<<array[i][j]<<" ";

        }

        cout<<endl;

        }