Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I have written this program using C Programming language which puts a 1 in the center of a 5 x 5 array and then has

I have written this program using C Programming language which puts a 1 in the center of a 5x5 array and then has four commands up down left right to move the 1 around in the array.
I already implemented the down command. So you would just need to implement/finish the other three (up, left, right). Basically, you just need to figure out how to change the row,col correctly for the other three commands so its only a few lines of code to add-
Here is the code:
#include
#include
#include
#define _CRT_SECURE_NO_WARNINGS
void printit(int data[5][5]){
for (int i =0; i <5; i++){
for (int j =0; j <5; j++){
printf("%d ", data[i][j]);
}
printf("
");
}
printf("
");
}
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(){
char sentence[50];
char firstword[50];
int x;
int row, col;
int args;
int arr[5][5];
printf("u # will move the one up # times
");
printf("d # will move the one down # times
");
printf("l # will move the one left # times
");
printf("r # will move the one right # times
");
printf("exit will leave
");
clear(arr);
arr[2][2]=1;
printit(arr);
row =2;
col =2;
for (;;){
printf("enter your command>");
// empty out the string just to be safe
strcpy_s(firstword,"");
strcpy_s(sentence,"");
// suck in a sentence from the user into memory
gets_s(sentence);
// suck from our sentence a word followed by two doubles
args = sscanf_s(sentence,"%s %d", firstword, (unsigned)_countof(sentence), &x);
// compare the first word to quit
if (strcmp(firstword, "exit")==0){
printf("goodbye");
break;
}
else if (strcmp(firstword,"u")==0){
if (args <2) x =1;
printf("row is %d col is %d
", row, col);
}
else if (strcmp(firstword,"d")==0){
if (args <2) x =1;
printf("row is %d col is %d
", row, col);
for (int i =0; i < x; i++){
row = abs((row +1)%5);
clear(arr);
arr[row][col]=1;
printit(arr);
}
}
else if (strcmp(firstword,"l")==0){
if (args <2) x =1;
printf("row is %d col is %d
", row, col);
}
else if (strcmp(firstword,"r")==0){
if (args <2) x =1;
printf("row is %d col is %d
", row, col);
}
else {
printf("don't understand that");
}
}
return(0);
}
Once you finish the code I want you to turn it in testing it on following output:
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)
IN ThE LAST MATRIX THE ONE SHOULD WIND UP IN THE RIGHT TOP CORNER OF THE GRID

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

MFDBS 89 2nd Symposium On Mathematical Fundamentals Of Database Systems Visegrad Hungary June 26 30 1989 Proceedings

Authors: Janos Demetrovics ,Bernhard Thalheim

1989th Edition

3540512519, 978-3540512516

More Books

Students also viewed these Databases questions