Question: Introduction to C Programming Objectives 1. To learn how to write functions given specifications 2. To learn how to the use pass by value and
Introduction to C Programming Objectives 1. To learn how to write functions given specifications 2. To learn how to the use pass by value and pass by reference variables 3. Review previous concepts like if-statements and loops.
Introduction: Ninja Academy Ninjas are awesome! Your friend has not stopped talking about how cool ninjas and how they would like to become a ninja. To amuse your friend, you have decided to create a series of programs about ninjas.
Problem: Codebreaking! You are almost ready to graduate from the Ninja Academy. Only one set of lessons remains: code breaking. You will need to learn how to break numerical codes of varying lengths and difficulties. The basic idea is this: 1) You begin by selecting a length and difficulty for the code. A set of numbers will be generated. 2) You will guess the magnitude and placement of each of the numbers. 3) Anything exactly correct will be revealed. 4) Anything with the correct value will be noted, but not revealed. 5) You can guess as many times as you want. When the correct code is entered, the program will end.
Make comments throughout the code
Output Sample #1 Welcome to Codebreaker Training! You will be presented with a set of hidden values. Your job is to guess these values and their correct order. After each guess you will be given feedback. Values that are correct in magnitude and in the correct location will be revealed. These are perfect matches. Then you will be told how many other numbers are correct in magnitude only. These are imperfect matches. No additional information will be revealed. 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!
Output Sample #2 Welcome to Codebreaker Training! You will be presented with a set of hidden values. Your job is to guess these values and their correct order. After each guess you will be given feedback. Values that are correct in magnitude and in the correct location will be revealed. These are perfect matches. Then you will be told how many other numbers are correct in magnitude only. These are imperfect matches. No additional information will be revealed. Choose a length for the code (5-10 numbers). 3 Choose a length for the code (5-10 numbers). 11 Choose a length for the code (5-10 numbers). 5 Choose a difficulty (1, 2, or 3). 0 Choose a difficulty (1, 2, or 3). 5 Choose a difficulty (1, 2, or 3). 3 There are 5 numbers ranging from 1 to 9. - - - - - Enter your guess: 12345 You have 2 perfect matches and 1 imperfect matches. 12 - - - Enter your guess: 12434 You have 3 perfect matches and 0 imperfect matches. 124 - - Enter your guess: 12467 You have 3 perfect matches and 1 imperfect matches. 124 - - Enter your guess: 12476 You have 4 perfect matches and 0 imperfect matches. 124 - 6 Enter your guess: 12486 You have 4 perfect matches and 0 imperfect matches. 124 - 6 Enter your guess: 12496 You have 5 perfect matches and 0 imperfect matches. Congratulations! You guessed all the values!
Scaffold (partially coded, only fill in the functions)
//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
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
