174k views
2 votes
Assume that a two-dimensional rectangular array of integers called matrix has been declared with six rows and eight columns. Write a for loop to exchange the contents of the second column with the fifth column.

User Karle
by
4.6k points

1 Answer

3 votes

Answer:

following are the program in C++ which is given below

Step-by-step explanation:

following are the program in C++

#include <bits/stdc++.h>

using namespace std;

int main()

{

int matrix[6][8]={{1,2,3,4,5,6,7,8},{9,10,11,12,13,14,15,16},{3,3,3,3,3,3,3,3},{4,4,4,4,4,4,4,4},{5,5,5,5,5,5,5,5},{6,6,6,6,6,6,6,6}};

cout<<"Matrix befor swapping:"<<endl;

for(int i=0;i<6;i++)

{

for(int j=0;j<8;j++)

{

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

}

cout<<endl;

}

for(int i=0;i<6;i++) //iterating over the loop

{

int temp=matrix[i][1];

matrix[i][1]=matrix[i][7];

matrix[i][7]=temp;

}

cout<<"Matrix After swapping:"<<endl;

for(int i=0;i<6;i++) //iterating over the loop

{

for(int j=0;j<8;j++)

{

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

}

cout<<endl;

Output

attachment are added for the above output.

following are the description of the program

1) read the matrix [i][j] by the user

2) in the loop we exchange the content of 2nd column with 5th column

Assume that a two-dimensional rectangular array of integers called matrix has been-example-1
User SeaFuzz
by
5.1k points