Wednesday, May 6, 2020
Programing Assignment of C Language
Question 1: Create a 2 dimensional array (10 by 10) that represents a maze: fill the maze with asterisks and then make a 'random' path of blanks from the lower left (0,0) element to the upper right (9,9) element. Create a maze running program that prints out the correct path. Your program should move square by square - if it finds and asterisk blocking its path, it searches the next adjacent square, etc. Submit 10 separate trials proving your program discovers the correct path. Each path MUST be random. Answer 1: #include int safeZone(char m[10][10], int i, int j){ if(i = 0 i 10 j = 0 j 10 m[i][j] == ' ') return 1; return 0;}int check(char m[10][10], int i, int j, char solMaze[10][10]){ if(i == 9 j == 9) { solMaze[i][j] = ' '; return 1; } if(safeZone(m, i, j) == 1) { solMaze[i][j] = ' '; if (check(m, i+1, j, solMaze) == 1) return 1; if (check(m, i, j+1, solMaze) == 1) return 1; solMaze[i][j] = '*'; return 0; } return 0;} void printMazeSol(char solMaze[10][10]){ int i, j; printf("n|--------------------|"); for (i = 0; i 10; i++) { printf("n|"); for (j = 0; j 10; j++) printf("%c ", solMaze[i][j]); printf("|"); } printf("n|--------------------|");} int mazeSolution(char m[10][10]){ char solMaze[10][10] = { {'*', '*', '*', '*', '*', '*', '*', '*', '*', '*'}, {'*', '*', '*', '*', '*', '*', '*', '*', '*', '*'}, {'*', '*', '*', '*', '*', '*', '*', '*', '*', '*'}, {'*', '*', '*', '*', '*', '*', '*', '*', '*', '*'}, {'*', '*', '*', '*', '*', '*', '*', '*', '*', '*'}, {'*', '*', '*', '*', '*', '*', '*', '*', '*', '*'}, {'*', '*', '*', '*', '*', '*', '*', '*', '*', '*'}, {'*', '*', '*', '*', '*', '*', '*', '*', '*', '*'}, {'*', '*', '*', '*', '*', '*', '*', '*', '*', '*'}, {'*', '*', '*', '*', '*', '*', '*', '*', '*', '*' } }; if(check(m, 0, 0, solMaze) == 0) { printf("Solution doesn't exist"); return 0; } printMazeSol(solMaze); return 1;} int main(){ char m[10][10] = { {' ', '*', '*', '*', '*', '*', ' ', '*', '*', '*'}, {' ', '*', '*', '*', '*', '*', ' ', '*', '*', '*'}, {' ', ' ', ' ', '*', '*', '*', ' ', '*', '*', ' '}, {'*', '*', ' ', '*', '*', '*', ' ', '*', '*', ' '}, {'*', '*', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '}, {'*', '*', ' ', '*', '*', '*', '*', '*', ' ', '*'}, {'*', '*', ' ', '*', '*', '*', '*', '*', ' ', '*'}, {'*', '*', ' ', '*', '*', '*', '*', '*', ' ', '*'}, {'*', '*', ' ', '*', '*', '*', '*', '*', ' ', ' '}, {'*', '*', ' ', '*', '*', '*', '*', '*', '*', ' '} }; mazeSolution(m); getchar();} Output : Question 2: Write a program that outputs all numbers of n digits or less that meet the following constraint:let the number beX and let the number of digits of the number be called n with each digit being d1,d2,..,dn. The d1**n + d2**n + .. + dn**n = X. For example : 371 meets this constraintbecause 3**3 + 7**3 + 1**3 = 371 ! But 11 does NOT meet the constraint because 1**2 + 1**2 does not equal 11. Test your code for N = 30 Answer 2: #include void main(){ long number, tempNum, result = 0, mul = 1; int N, i, j; int arrNum[50]; printf("Enter Number: "); scanf("%d", number); N = 0; tempNum = number; while(tempNum 0) { arrNum[N] = tempNum % 10; tempNum = tempNum/10; N = N + 1; } for(i=0; iN; i++) { mul = 1; for(j=0; jN; j++) { mul = mul * arrNum[i]; } result = result + mul; } if(result == number) printf("n%d meets constraint", number); else printf("n%d does not meet constraint", number); getch();} Output : Enter number: 30 30 does not meet constraint Enter number: 371 371 meet constraint Enter number: 11 11 does not meet constraint.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.