Question: What is wrong with this code?(C programming) It was running fine and then I must've changed something but I cant figure it out. *This program

What is wrong with this code?(C programming)

It was running fine and then I must've changed something but I cant figure it out.

*This program generates and reveals a set of number if the magnitude and placement of the numbers is correct */

//Replace or modify header to include your information

//pre-processor directives #include #include #include

//Function Prototypes - do not change these void greeting(); void setParameters(int * length, int * difficulty); int * createHideList(int length, int difficulty); int * createShowList(int length); void display(int showlist[], int length); void getGuess(int guesss[], int length); void processGuess(int hidelist[], int showlist[], int length, int guess[]); int solved(int hidelist[], int guess[], int length); int PerfectMatches(int hidelist[], int guess[], int length); int ImperfectMatches(int hidelist[], int guess[], int length); void copyArray(int dest[], int source[], int length);

//Main function - This is the final version of main. Any changes you make while //creating the functions should be removed prior to submission. int main() { //Length determines the number of hidden values that must be guessed //Difficulty determines the range of the hidden values int length, difficulty; //Answer, revealed, and guess are all integer arrays //Since length is undefined at this point, no sizes have been assigned. //This will be done in the create...List functions. //Answer is the correct answer of the hidden values //Revealed is the list that is shown to the user //Guess is the user's guess int * answer, * revealed, * guess;

//seed the random number generator srand(time(0));

//Begin the program by showing the initial greeting to the user greeting();

//Ask the user for the length and difficulty setParameters(&length, &difficulty);

//Create the initial arrays of size length answer = createHideList(length, difficulty); revealed = createShowList(length); guess = createShowList(length);

printf(" There are %d numbers ranging from 1 to %d. ", length, difficulty*3);

//Loop until the user solves the puzzle while (!solved(answer, guess, length)) { //Show the information gathered so far and prompt the user for their next guess display(revealed, length); getGuess(guess, length); processGuess(answer, revealed, length, guess); }

printf("Congratulations! You guessed all the values! ");

//These functions are necessary because we are using memory allocation functions in create...List free(answer); free(revealed); free(guess);

return 0; }

//Functions //Pre-conditions: none //Post-conditions: Prints a welcome message to the screen void greeting() { printf("Welcome to Codebreaker Training! "); printf("You will be presented with a set of hidden values. Your job is to guess these "); printf("values and their correct order. After each guess you will be given feedback. "); printf("Values that are correct in magnitude and in the correct location will be revealed. These are "); printf("perfect matches. Then you will be told how many other numbers are correct in magnitude only. "); printf("These are imperfect matches. No additional information will be revealed. "); return; }

//Pre-conditions: length and difficulty are valid values established by the setParameters function //Post-conditions: create an array of size length and populate it with random // values based on the difficulty int * createHideList(int length, int difficulty) { //i is a counter //array is an integer array, allocated "length" integers int i; int * array = (int *)malloc(sizeof(int) * length);

//traverse the valid indices of the array //assign each index a random value from 1 - difficulty*3 for(i=0; i

//return the beginning of the array to the main function return array; }

//Pre-conditions: length has a valid value established by the setParameters function //Post-conditions: create an array of size length and initialize to zero int * createShowList(int length){ //i is a counter //array is an integer array, allocated "length" integers int i; int * array = (int *)malloc(sizeof(int) * length);

//traverse the valid indices of the array //assign each index an initial value of 0 for(i=0; i

//return the beginning of the array to the main function return array; }

//Pre-conditions: length and difficulty are integer pointers //Post-conditions: length and difficulty will be set by the user // Function should verify that these values are valid

//What to do in this function: Prompt the user for the length of the code. Remember //that the parameter length is a pointer, so it cannot store an integer value. You will need additional //variables. Ensure that the user gives you a value between 5 and 10, inclusive. If their //value is invalid, continue to prompt them until they provide a valid one. Assign length the location //of the valid integer. Repeat this process for the difficulty parameter. Difficulty //must be 1, 2, or 3 to continue. void setParameters(int * length, int * difficulty) {

//Integer variables for length and difficulty of the code int codeLength; int codeDifficulty;

//Ask user for a code length printf("Choose a length for the code (5-10 numbers). "); scanf("%d", &codeLength);

//It is invalid if the code is not between 5 and 10, inclusive. Prompt user again while (codeLength < 5 || codeLength > 10) { printf(" Sorry, that is invalid. "); printf(" Choose a length for the code (5-10 numbers). "); scanf("%d", &codeLength); }

//Go to where length is pointing and give it the value of the users input for code length *length = codeLength;

//Ask user for the code difficulty printf(" Choose a difficulty. (1, 2, or 3) "); scanf("%d", &codeDifficulty);

//It is invalid if the difficulty level is not between 1 and 3, inclusive. Prompt user again while (codeDifficulty < 1 || codeDifficulty > 3) { printf(" Sorry, that is invalid. "); printf(" Choose a difficulty. (1, 2, or 3) "); scanf("%d", &codeDifficulty); }

//Go to where difficulty is pointing and give it the value of the users input for code difficulty *difficulty = codeDifficulty;

return;

}

//Pre-conditions: showlist is an integer array of size length //Post-conditions: displays the contents of showlist using - for 0

//What to do in this function: Traverse the showlist array and print a - whenever the value is 0 //If the value is not zero, print the actual value. For example, if the code is 12311 and the //user has identified the location of the 1s, the showlist/revealed array will contain 10011 //and you should display 1--11 void display(int showlist[], int length) {

//i is a counter int i;

//traverse showlist array and print - when the value is 0 for(i=0; i

//print the actual value if its in the correct location else printf("%d", showlist[i]); }

return; }

//Pre-conditions: guess is an integer array of size length //Post-conditions: reads in the guess from a user as a string and converts it to an integer array

//What to do in this function: The user will enter their values all together. For example: 12311 //Read this in as a string and then store each individual integer value in the guess array. //Since the elements of the input will be characters, you will need to convert to integers. For example //you would need to convert from '1' to 1. Character representations of digits start with '0' with ASCII value 48. void getGuess(int guess[], int length) {

//i is a counter //userGuess is used to read the guess from the user as a string int i; char userGuess[10];

//Ask user for the guess printf(" Enter your guess:"); scanf("%s", userGuess);

//Traverse the guess array and subtract the decimal value of character "0" in the userGuess array to get the integer value for(i=0; i

return; }

//Pre-conditions: both hidelist and guess are integer arrays of size length // hidelist represents the answer the user is looking for // guess represents the user's current guess //Post-conditions: determines if the user has found all of the hidden values

//What to do in this function: Traverse the arrays and check to see if all of the values //in hidelist are the same as the values in guess. If they are return 1 for true, //return 0 for false otherwise. int solved(int hidelist[], int guess[], int length) {

//i is a counter int i;

//traverse the arrays and as long as the values in hidelist and guess are not equal, return true for(i=0; i

return 1; }

//Pre-conditions: hidelist, showlist, and guess are integer arrays of size length // hidelist represents the answer the user is looking for // showlist shows the values the user has already discovered // guess is the user's current guess //Post-conditions: evaluates the current guess and updates showlist

//What to do in this function: Compare hidelist to guess. If any values match exactly, //update the showlist to reflect the information the user has discovered. Then, print out //the number of perfect matches the user has by calling the PerfectMatches function. Print //out the number of imperfect matches the user has by calling the ImperfectMatches function. //Use the statement from the samples for your printf control string. void processGuess(int hidelist[], int showlist[], int length, int guess[]) {

//i is a counter int i;

//Traverse the arrays and if values match in hidelist and guess, update to showlist for(i=0; i

//Print the number of perfect matches and imperfect matches by calling their respective function printf("You have %d perfect matches and %d imperfect matches. ", PerfectMatches(hidelist, guess, length), ImperfectMatches(hidelist, guess, length));

return; }

//Pre-conditions: hidelist and guess are integer arrays of size length // hidelist represents the answer the user is looking for // guess is the user's current guess //Post-conditions: evaluates the current guess and returns the number of perfect matches

//What to do in this function: Compare hidelist to guess. Return the number of times //the value in guess exactly matches the corresponding value in hidelist. int PerfectMatches(int hidelist[], int guess[], int length) {

//i is a counter int i;

//numPerfect is a counter for perfect matches int numPerfect = 0;

//Traverse the arrays and count the number of times the guess matched value in hidelist for(i=0; i

//Return the number of perfect matches return numPerfect; }

//Pre-conditions: hidelist and guess are integer arrays of size length // hidelist represents the answer the user is looking for // guess is the user's current guess //Post-conditions: evaluates the current guess and returns the number of imperfect matches

//What to do in this function: Create temporary copies of both arrays by calling the copyArray function //twice. This is necessary because we'll need to make changes to both arrays without modifying the //originals. Mark out the spots on the arrays that match exactly. They will be counted in the perfect //matches instead. Use any value that won't be present in the hidelist.

//Go through the hidelist array and look for matches in the guess array. Anytime a value matches, //regardless of location, count it as an imperfect match and mark it so it won't get counted again.

//Return the number of imperfect matches //Hint: you will need a nested for loop. int ImperfectMatches(int hidelist[], int guess[], int length) {

//i and j are counters //numImperfect is also a counter for number of imperfect matches //tempHide and tempGuess arrays with the size of length in order to make changes int i, j; int numImperfect = 0; int tempHide[length]; int tempGuess[length];

//Call the copyArray function twice to set the temps equal to the original copyArray(tempHide, hidelist, length); copyArray(tempGuess, guess, length);

//Traverse the arrays and find equal values. Mark off since they are perfect matches for(i=0; i

} }

//Traverse through the arrays and find equal values that haven't been counted as perfect matches. //Count them as imperfect matches and mark it off for(i=0; i

} } }

//Return the number of perfect matches return numImperfect;

}

//Pre-conditions: dest and source are integer arrays of size length // source represents a source array: an array to be copied // dest represents a destination array: the location of the copy //Post-conditions: dest will contain a copy of source

//What to do in this function: traverse the source array and copy each element into //the corresponding location of the dest array void copyArray(int dest[], int source[], int length) {

//i is a counter int i;

//Traverse source array and copy into dest array for(i=0; i

return; }

Sample run:

Choose a length for the code (5-10 numbers).

6

Choose a difficulty (1, 2, or 3).

1

There are 6 numbers ranging from 1 to 3.

- - - - - -

Enter your guess: 111111

You have 1 perfect matches and 0 imperfect matches.

- - - - 1 -

Enter your guess: 232112

You have 1 perfect matches and 2 imperfect matches.

- - - - 1 -

Enter your guess: 222113

You have 3 perfect matches and 0 imperfect matches.

- 2 - - 13

Enter your guess: 323313

You have 6 perfect matches and 0 imperfect matches.

Congratulations! You guessed all the values!

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!