C: Using arrays to show guessed numbers -


i having issues assignment, looks right me, not. need write program person guesses 5 - 9 digit number depending on selection, , when each number guessed correctly needs show in order guessed , keep asking until numbers correct. number seeded randomly s(rand).

here assignment instructions

problem: codebreaking!  basic idea this: 1) begin selecting length , difficulty code. set of numbers generated. 2) guess magnitude , placement of each of numbers. 3) correct revealed. 4) correct value noted, not revealed. 5) can guess many times want.  when correct code entered, program end. 

output sample

welcome codebreaker training! presented set of hidden values. job guess these values , correct order. after each guess given feedback. values correct in magnitude , in correct location revealed. these perfect matches. told how many other numbers correct in magnitude only. these imperfect matches. no additional information revealed. choose length code (5-10 numbers). 6 choose difficulty (1, 2, or 3). 1 there 6 numbers ranging 1 3. - - - - - - enter guess: 111111 have 1 perfect matches , 0 imperfect matches. - - - - 1 - enter guess: 232112 have 1 perfect matches , 2 imperfect matches. - - - - 1 - enter guess: 222113 have 3 perfect matches , 0 imperfect matches. - 2 - - 13 enter guess: 323313 have 6 perfect matches , 0 imperfect matches. congratulations! guessed values! 

here code have

//pre-processor directives #include <stdio.h> #include <stdlib.h> #include <time.h>  //function prototypes - 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 - final version of main. changes make while //creating functions should removed prior submission. int main() { //length determines number of hidden values must guessed //difficulty determines range of hidden values int length, difficulty; //answer, revealed, , guess integer arrays //since length undefined @ point, no sizes have been assigned. //this done in create...list functions. //answer correct answer of hidden values //revealed list shown user //guess user's guess int * answer, * revealed, * guess;  //seed random number generator srand(time(0));  //begin program showing initial greeting user greeting();  //ask user length , difficulty setparameters(&length, &difficulty);  //create initial arrays of size length answer = createhidelist(length, difficulty); revealed = createshowlist(length); guess = createshowlist(length);  printf("\nthere %d numbers ranging 1 %d.\n\n", length, difficulty*3);  //loop until user solves puzzle while (!solved(answer, guess, length)) { //show information gathered far , prompt user next guess display(revealed, length); getguess(guess, length); processguess(answer, revealed, length, guess); }  printf("congratulations! guessed values!\n\n");  //these functions necessary because using memory allocation functions in create...list free(answer); free(revealed); free(guess);  return 0; }  //functions //pre-conditions: none //post-conditions: prints welcome message screen void greeting() { printf("welcome codebreaker training!\n\n"); printf("you presented set of hidden values. job guess these\n"); printf("values , correct order. after each guess given feedback.\n\n"); printf("values correct in magnitude , in correct location\nwill revealed. these "); printf("perfect matches.\n\nthen told how many other numbers correct in magnitude only.\n"); printf("these imperfect matches. no additional information revealed.\n\n"); return; }  //pre-conditions: length , difficulty valid values established setparameters function //post-conditions: create array of size length , populate random // values based on difficulty int * createhidelist(int length, int difficulty) { //i counter //array integer array, allocated "length" integers int i; int * array = (int *)malloc(sizeof(int) * length);  //traverse valid indices of array //assign each index random value 1 - difficulty*3 for(i=0; i<length; i++) array[i] = rand() % (difficulty*3) + 1;  //return beginning of array main function return array; }  //pre-conditions: length has valid value established setparameters function //post-conditions: create array of size length , initialize 0 int * createshowlist(int length){ //i counter //array integer array, allocated "length" integers int i; int * array = (int *)malloc(sizeof(int) * length);  //traverse valid indices of array //assign each index initial value of 0 for(i=0; i<length; i++) array[i] = 0;  //return beginning of array main function return array; }  //pre-conditions: length , difficulty integer pointers //post-conditions: length , difficulty set user // function should verify these values valid  //what in function: prompt user length of code. remember //that parameter length pointer, cannot store integer value. need additional //variables. ensure user gives value between 5 , 10, inclusive. if //value invalid, continue prompt them until provide valid one. assign length location //of valid integer. repeat process difficulty parameter. difficulty //must 1, 2, or 3 continue. void setparameters(int * length, int * difficulty) {    int temp;    printf("enter length of code: ");    scanf("%d", &temp);    while(temp < 5 || temp > 10)    {    printf("length should between 5 , 10 inclusive.\n");    printf("enter length of code: ");    scanf("%d", &temp);    }    *length = temp;     printf("enter difficulty of code: ");    scanf("%d", &temp);    while(temp < 1 || temp > 3)    {    printf("difficulty should either 1, 2, or 3.\n");    printf("enter difficulty of code: ");    scanf("%d", &temp);    }    *difficulty = temp;  }  //pre-conditions: showlist integer array of size length //post-conditions: displays contents of showlist using - 0  //what in function: traverse showlist array , print - whenever value 0 //if value not zero, print actual value. example, if code 12311 , //user has identified location of 1s, showlist/revealed array contain 10011 //and should display 1--11 void display(int showlist[], int length) {    int i;    for(i=0; < length; i++)    if(showlist[i] == 0)    printf("-");    else    printf("%d", showlist[i]); }  //pre-conditions: guess integer array of size length //post-conditions: reads in guess user string , converts integer array  //what in function: user enter values together. example: 12311 //read in string , store each individual integer value in guess array. //since elements of input characters, need convert integers. example //you need convert '1' 1. character representations of digits start '0' ascii value 48. void getguess(int guess[], int length) {    int i;    char string[10];    scanf("%s", &string);    for(i=0; < length; i++)    guess[i] = string[i] - '0';  }  //pre-conditions: both hidelist , guess integer arrays of size length // hidelist represents answer user looking // guess represents user's current guess //post-conditions: determines if user has found of hidden values  //what in function: traverse arrays , check see if of values //in hidelist same values in guess. if return 1 true, //return 0 false otherwise. int solved(int hidelist[], int guess[], int length) {    int i;    for(i=0; < length; i++)    if(hidelist[i] != guess[i])    return 0;    return 1;  }  //pre-conditions: hidelist, showlist, , guess integer arrays of size length // hidelist represents answer user looking // showlist shows values user has discovered // guess user's current guess //post-conditions: evaluates current guess , updates showlist  //what in function: compare hidelist guess. if values match exactly, //update showlist reflect information user has discovered. then, print out //the number of perfect matches user has calling perfectmatches function. print //out number of imperfect matches user has calling imperfectmatches function. //use statement samples printf control string. void processguess(int hidelist[], int showlist[], int length, int guess[]) {    int i;    for(i=0; < length; i++)    {    if(hidelist[i] == guess[i])    showlist[i] = guess[i];    }    printf("you have %d perfect matches , %d imperfect matches.\n", perfectmatches(hidelist, guess, length), imperfectmatches(hidelist, guess, length));  }   //pre-conditions: hidelist , guess integer arrays of size length // hidelist represents answer user looking // guess user's current guess //post-conditions: evaluates current guess , returns number of perfect matches  //what in function: compare hidelist guess. return number of times //the value in guess matches corresponding value in hidelist. int perfectmatches(int hidelist[], int guess[], int length) {    int count = 0, i;    for(i=0; < length; i++)    if(hidelist[i] == guess[i])    count++;    return count; }  //pre-conditions: hidelist , guess integer arrays of size length // hidelist represents answer user looking // guess user's current guess //post-conditions: evaluates current guess , returns number of imperfect matches  //what in function: create temporary copies of both arrays calling copyarray function //twice. necessary because we'll need make changes both arrays without modifying //originals. mark out spots on arrays match exactly. counted in perfect //matches instead. use value won't present in hidelist.  //go through hidelist array , matches in guess array. anytime value matches, //regardless of location, count imperfect match , mark won't counted again.  //return number of imperfect matches //hint: need nested loop. int imperfectmatches(int hidelist[], int guess[], int length) {    int count = 0, i, j;    int hidelistdup[length], guessdup[length];    copyarray(hidelistdup, hidelist, length);    copyarray(guessdup, guess, length);    for(i=0; < length; i++)    if(hidelistdup[i] == guessdup[i])    hidelistdup[i] = guessdup[i] = -1;    for(i = 0; < length; i++)    {    for(j=0; j < length; j++)    if(guess[i] == hidelist[j] && != j && guess[i] != -1)    {    count++;    hidelist[j] = -1;    }    }    return count; }  //pre-conditions: dest , source integer arrays of size length // source represents source array: array copied // dest represents desitnation array: location of copy //post-conditions: dest contain copy of source  //what in function: traverse source array , copy each element //the corresponding location of dest array void copyarray(int dest[], int source[], int length) {    int i;    for(i=0; < length; i++)    dest[i] = source[i];  } 

my problem output ends reverses imperfect , perfect numbers, not sure why.

enter length of code: 5 enter difficulty of code: 1  there 5 numbers ranging 1 3.  -----11111 have 0 perfect matches , 2 imperfect matches. ---11 

please if can appreciated.


Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -