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

Instructions: Write a C++ program that uses a function to shift the rows of a 2D

ID: 3747990 • Letter: I

Question

Instructions: Write a C++ program that uses a function to shift the rows of a 2D Vector. This C++ program needs to compile successfully on Visual Studio 2015 or 2017. Place your name and which version of Visual Studio you used (2015 or 2017) in comments at the top of your program. Instructions: Define a function that will shift a 2D Vector of integers as: “Shift_2D_Vector”. Shift_2D_Vector must only have the following 2 parameters: 1. A 2 dimensional vector of integers. Call this parameter: “table” 2. A unsigned integer that will store the number of shifts. Call this parameter: “num_of_shifts”. The function needs to work with 2D vectors of any size and any number of shifts. Make sure that your function can properly perform shifts that are greater than the size of the vector. Test this function by writing a C++ program that prompts the user to input the number of rows and columns that the vector will have. Additionally, allow the user to input how many times he wants to shift the vector.

Explanation / Answer

#include<iostream>

#include<vector>

using namespace std;

//it performs right shift

vector<vector<int> > Shift_2D_Vector(vector<vector<int> > table,int num_of_shifts)

{

int n = num_of_shifts;

int r =table.size();//number of rows...

int c = table[0].size();//number of cols..

//shifting rows..

int i=0,j=0,k;

while(n>0)

{

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

{

c = table[i].size();

k = table[i].at(0);

for(j=0;j<c-1;j++)

{

table[i][j]=table[i].at(j+1);//shifting each element

}

table[i][j]=k;

}

n--;

}

return table;

}

//method to display vector...

void display(vector<vector<int> > table)

{

int r =table.size();//number of rows...

int c = table[0].size();//number of cols..

int i=0,j=0,k;

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

{

c = table[i].size();//number of cols..

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

{

cout<<table[i].at(j)<<" ";

}

cout<<endl;

}

}

//testing method

using namespace std;

int main()

{

int r,c,i,j;

while(1)

{

//reading input

cout<<" Enter number of rows:";

cin>>r;

cout<<" Enter number of cols:";

cin>>c;

//declaring 2d vector

vector<vector<int> > v;

cout<<"Enter values for 2d vector: ";

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

{

v.push_back(vector<int>());

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

{

cout<<"item:["<<i;

cout<<"]["<<j<<"]:";

int k;

cin>>k;

v[i].push_back(k);

}

}

int m;

cout<<"Enter number of shifts:";

cin>>m;

cout<<"2D Vector: ";

display(v);

//calling method to shift

v=Shift_2D_Vector(v,m);

//after shifting

cout<<"2D Vector after shifting: ";

display(v);

int cp=0;

cout<<"Enter -1 to exit:";

cin>>cp;

if(cp==-1)break;

}

return 0;

}

output:


Enter number of rows:2

Enter number of cols:3
Enter values for 2d vector:
item:[0][0]:1
item:[0][1]:2
item:[0][2]:3
item:[1][0]:4
item:[1][1]:5
item:[1][2]:6
Enter number of shifts:1
2D Vector:
1 2 3
4 5 6
2D Vector after shifting:
2 3 1
5 6 4
Enter -1 to exit:1

Enter number of rows:2

Enter number of cols:3
Enter values for 2d vector:
item:[0][0]:1
item:[0][1]:2
item:[0][2]:3
item:[1][0]:4
item:[1][1]:5
item:[1][2]:6
Enter number of shifts:2
2D Vector:
1 2 3
4 5 6
2D Vector after shifting:
3 1 2
6 4 5
Enter -1 to exit:1

Enter number of rows:2

Enter number of cols:3
Enter values for 2d vector:
item:[0][0]:1
item:[0][1]:2
item:[0][2]:3
item:[1][0]:4
item:[1][1]:5
item:[1][2]:6
Enter number of shifts:5
2D Vector:
1 2 3
4 5 6
2D Vector after shifting:
3 1 2
6 4 5
Enter -1 to exit:-1


Process exited normally.
Press any key to continue . . .