Question
This is a C programming code to make the letter A move around a matrix based on user commands: #include #include void display(char a[11][11]) {
This is a C programming code to make the letter A move around a matrix based on user commands:
#include
int main() { char ch; int m, n; char aster = '*'; char matrix[11][11]; //Declaring matrix for (int i = 0; i < 11; i++) { //initializing matrix for(int j=0; j<11; j++) { if (i == 5 && j == 5) matrix[i][j] = 'A'; else matrix[i][j] = '*'; } } while(1){ display(matrix); for (int i = 0; i <= 11; i++) { //Finding the current position of A for(int j=0; j<11; j++){ if(matrix[i][j]=='A'){ m = i; n = j; break; } } } ch=getch(); if(ch=='a'){ if((n-1)>=0){ //If new position is not outside boundary n--; for(int i=0; i<=11; i++){ //then move to new position for(int j=0; j<=11; j++){ if(i==m&&j==n) //set new position of A matrix[i][j] = 'A'; else matrix[i][j] = '*'; } } display(matrix); } continue; } if(ch=='s'){ if((m+1)<=10){ m++; for(int i=0; i<11; i++){ //then move to new position for(int j=0; j<11; j++){ //if(i==0 || i==10 || j==0 || j==10) //break; if(i==m&&j==n) //set new position of A matrix[i][j] = 'A'; else matrix[i][j] = '*'; } } } continue; } if(ch=='d'){ if((n+1)!>10){ //If new position is not outside boundary n++; for(int i=0; i<11; i++){ //then move to new position for(int j=0; j<11; j++){ if(i==m&&j==n) //set new position of A matrix[i][j] = 'A'; else matrix[i][j] = '*'; } } display(matrix); } continue; } if(ch=='w'){ if((m-1)>=0){ //If new position is not outside boundary m--; for(int i=0; i<11; i++){ //then move to new position for(int j=0; j<11; j++){ if(i==m&&j==n) //set new position of A matrix[i][j] = 'A'; else matrix[i][j] = '*'; } } } continue; } } return 0; }
How can I edit this code so the Letter A stops when it reached the border of the matrix?
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started