Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

#include #include #include #include using namespace std; void printit ( int data [ 5 ] [ 5 ] ) { for ( int i =

#include
#include
#include
#include
using namespace std;
void printit(int data[5][5]){
for (int i =0; i <5; i++){
for (int j =0; j <5; j++){
cout << data[i][j]<<"";
}
cout << endl;
}
cout << endl;
}
void clear(int data[5][5]){
for (int i =0; i <5; i++){
for (int j =0; j <5; j++){
data[i][j]=0;
}
}
}
int main(){
string sentence;
string firstword;
int x;
int row, col;
int args;
int arr[5][5];
cout <<"u # will move the one up # times" << endl;
cout <<"d # will move the one down # times" << endl;
cout <<"l # will move the one left # times" << endl;
cout <<"r # will move the one right # times" << endl;
cout << "exit will leave" << endl << endl;
clear(arr);
arr[2][2]=1;
printit(arr);
row =2;
col =2;
while (true){
cout << "enter your command>";
getline(cin, sentence);
args = sscanf_s(sentence.c_str(),"%s %d", firstword.c_str(),(unsigned)firstword.length(), &x);
if (firstword == "exit"){
cout << "goodbye" << endl;
break;
}
else if (firstword =="u"|| firstword =="d"|| firstword =="l"|| firstword =="r"){
if (args <2) x =1;
cout << "row is "<< row <<" col is "<< col << endl;
for (int i =0; i < x; i++){
if (firstword =="u"){
row =(row -1+5)%5; // Ensure it wraps around if it goes negative
}
else if (firstword =="d"){
row =(row +1)%5;
}
else if (firstword =="l"){
col =(col -1+5)%5; // Ensure it wraps around if it goes negative
}
else if (firstword =="r"){
col =(col +1)%5; // Ensure it wraps around if it goes beyond the last column
}
clear(arr);
arr[row][col]=1;
printit(arr);
}
}
else {
cout << "don't understand that" << endl;
}
}
return 0;
} Help fix my code please I want my output to be able to Do a
l 3(goes left and prints the matrix a total of three times (notice how it should wrap around) to the right
Then do a
u 2(goes up and prints the matrix a total of two times)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Concepts of Database Management

Authors: Philip J. Pratt, Joseph J. Adamski

7th edition

978-1111825911, 1111825912, 978-1133684374, 1133684378, 978-111182591

More Books

Students also viewed these Databases questions

Question

What is your theoretical orientation? (For Applied Programs Only)

Answered: 1 week ago