Hey I need help modifying my code: IT in Cprogramming
//this is the library use in C# include <stdio.h># include <stdlib.h># include <time.h>
//This is where I declare my functionvoid setBoard(int*);void setComputerBoard(int*);int playGame(int*, int*);int main(){ int y[2], z[2], t; setBoard(y); srand(time(0)); setComputerBoard(z); t = playGame(y, z); if(t==1){ printf("\nHuman wins!"); } else{ printf("\nComputer wins!"); } return 0;}//This function to set the human boardvoid setBoard(int *x){ for(int i=0; i<2; i++){ printf("Enter %dst position: ", i+1); scanf("%d", &*(x+i) ); }}//This function set the computer boardvoid setComputerBoard(int*x){ int num1, num2; num1 = rand()%10; if(num1==9){ num1==8; num2==9; } else { num2 = num1+1; } *x = num1; *(x+1) = num2;}//This is the function to set the gameint playGame(int *x, int *y){ // n is the input number from user, b is randomnumber int n, b; int r =0; int a=0;
//This is the arrays of lements on human board char arrs[10]; char arrs1[10]; for(int e=0; e<10; e++){ arrs[e] = '*'; arrs1[e]= '*'; } //This invoke S to printed off in the slots where thehuman ship is located arrs[*x]='S'; arrs[*(x+1)]= 'S'; arrs1[*y]='S'; arrs1[*(y+1)]= 'S'; while ( r<2 && a<2) { //This invoke the computer to guess b=rand()%10-1; printf("\nComputer guess %d\n", b); //This set the condition when computer hithuman ship if(b==*(x) || b==*(x+1)){ printf("HIT!\n"); arrs='H'; r++; } else{ printf("MISS!\n"); arrs='M'; } //This invoke a print statement for the humanand computer board when computer guesses. printf("\nHuman Board:\n0 1 2 3 4 5 6 7 89\n"); for(int k=0; k<10; k++){ printf("%c ", arrs[k]); } printf("\nComputer Board:\n0 1 2 3 4 5 6 7 89\n"); for(int l = 0; l<10; l++){ printf("%c ", arrs1[l]); } //This invoke the second for the humanguesses printf("\nEnter guess: "); scanf("%d", &n); printf("\nyou guessed %d\n", n); if(n==*y|| n==*(y+1)){ printf("HIT "); arrs1[n]='H'; a++; } else{ printf("MISS "); arrs1[n]='M'; } // Again this is my print statement for thehuman and computer board when human guesses printf("\nHuman Board:\n0 1 2 3 4 5 6 7 89\n"); for(int k=0; k<10; k++){ printf("%c ", arrs[k]); } printf("\nComputer Board:\n0 1 2 3 4 5 6 7 89\n"); for(int l = 0; l<10; l++){ printf("%c ", arrs1[l]); } } //This function return 1 if the humanwins, // and 0 if the computer wins if(a>r){ return 1; } else if(a<r) { return 0; }
Important: You should use pointer notation when referencing values inside an array as opposed to array notation. For example, use *ptroArray instead of array[0] and *(ptrToArray+1) instead of array[1], etc. You will lose 10 points for not following this notation. This homework can be solved using char pointers as well. I would accept that as a valid solution too. It is your choice whether you want to use int pointers or char pointers. You can also modify the parameters mentioned for the methods. As long as you have the methods performing the tasks stated in the problem, you will get credit.
Hey I need help modifying my code: IT in C programming //this is the library use in C # include # include
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am