Hello I'm working on a quick program that generates a master code and the user has to guess the code. When checking the user's guess I use 2 functions one for Exact Matches and the other for close matches (where they got a number that's in the mastercode but not in the right location)
Ex. master code 1 2 3 4
user 2 3 2 4 the output the should show the user has 2 close matches and one exact match. I'm having trouble understanding how to properly return an int.
My ouput just shows the default value of exactMatch and closeMatch when I try to print them in Main. any insight will be greatly appreciated. Thank you.
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<time.h>
#define CODELENGTH 4
#define NUMSYMBOLS 6
int MasterCode[4];
int guess[ 4 ];
int exactMatch;
int closeMatch=0;
void genCode (int MasterCode[])
{
int i=0;
int k;
while (i < CODELENGTH){
MasterCode[i] =rand() %NUMSYMBOLS +1;
i++;
}//end while loop.
for ( k = 0 ; k < 4; k++ ) {
printf( "%d ", MasterCode[ k ] );
}
printf( "\n" );
}
void getGuess (int guess[])
{
int number = 0;
printf( "Please enter your list of 4 numbers between 1 and 6: " );
int j;
int k;
for ( j = 0 ; j < 4; j++ ) {
scanf( "%d", &number );
guess[ j ] = number;
}
printf( "Your array has these values: " );
for ( k = 0 ; k < 4; k++ ) {
printf( "%d ", guess[ k ] );
}
printf( "\n" );
}
int main (int argc, char **argv)
{
srand ( time(NULL) );
genCode(MasterCode);
getGuess(guess);
checkExactMatches(MasterCode, guess, exactMatch);
checkCloseMatches(MasterCode, guess, closeMatch);
printf("%d = Ending exactMatches \n", exactMatch);
printf("%d = Ending closeMatches \n", closeMatch);
}
int checkExactMatches (int MasterCode[], int guess[], int exactMatch )
{
int woot;
for(woot=0; woot<4; woot++){
if (MasterCode[woot] == guess[woot]){
printf("Exact Match found \n");
exactMatch ++;
printf( "%d = Guess \n" , guess[ woot ]);
printf( "%d = MasterCode \n", MasterCode[ woot ]);
printf("%d = exactMatch \n", exactMatch);
}// end if
if (MasterCode[woot] != guess[woot])
printf("No EXACT match \n");
}//end for loop
return exactMatch;
} // end checkExactMatches
int checkCloseMatches (int MasterCode[], int guess[], int closeMatch )
{
int k;
int j;
for(k=0; k<4; k++){
for (j=0; j<4; j++) {
if (MasterCode[k] == guess[j]){
printf("CLOSE Match found \n");
closeMatch ++;
printf( "%d = Guess \n" , guess[ j ]);
printf( "%d = MasterCode \n \n", MasterCode[ k ]);
printf("%d = closeMatch \n \n", closeMatch);
}// end if
if (MasterCode[k] != guess[j])
printf("No CLOSE match \n");
}//end nested for loop
}//end for loop
return closeMatch;
} // end checkCloseMatches
Getting the value returned from a function is actually really easy, it's basically the same as assigning a value to a variable. In your case, the syntax would be something like this:
int result = checkExactMatches(MasterCode, guess, exactMatch);
result will now hold the value returned by the function.
What you need to do is
So
checkExactMatches(MasterCode, guess, exactMatch);
becomes
exactMatch = checkExactMatches(MasterCode, guess);
You need to make appropriate changes to the function header and also avoid using global variables.
Note that while arrays are passed by pointer (meaning that the function can modify the pointed-to pointer), ints are passed by value. Therefore, changes to the int within a function only persist for the duration of the function.
You want to use
exactMatch = checkExactMatches(MasterCode, guess);
and in that case you won't have to pass in exactMatch
any more.
C passes arguments by value, rather than by reference, so passing int exactmatch
into the function creates a duplicate of the data inside exactmatch
, so operations done inside the method don't do anything.
What you want to do is remove exactmatch from the argument list, and instead assign it as zero ( int exactmatch=0;
at the beginning of the method, remove exactmatch from the global variables, and use something like int resultExact = checkExactMatches(MasterCode, guess);
as your function call. You're already returning the answer correctly, you just don't pick up that answer anywhere.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.