The program below contains at least five syntax and/or logic errors. Assuming the comments are correct, modify the code so that it would compile cleanly and behave as specified in the comments.
// Given two arrays that are the same width, secretCode contains a code the
// user is trying to break and guess contains the user's guess at the code.
// The secret code is not necessarily in base 10, NUMBASE indicates which base
// it is in. So if NUMBASE is 4 then only digits 0, 1, 2, and 3 are possible.
#include <cstdio>
const int WIDTH = 5;
// count and return the number of times the value target
// appears in the array
int countOccurances(int array[], int target);
// This function counts how many of the digits in guess are a correct digit,
// whether or not they are in the right location, by taking the minimum of the
// number of times they occur in each of the two arrays.
int evaluateGuess(int secretCode[], int guess[]);
int main()
{
int code[] = { 1, 2, 3 };
int guess[] = { 3, 1, 5 };
printf("%d are correct\n", evaluateGuess(code, code);
}
int evaluateGuess(int secretCode[], int guess[])
{
int result;
int inGuess;
int inAnswer;
for (int i = 1; i <= NUMBASE; i++) {
inGuess = countOccurances(guess, i);
inAnswer = countOccurances(secretCode, i);
if (inGuess > inAnswer) {
result += inGuess;
} else {
result += inAnswer;
}
}
return true;
}
int countOccurances(int array[], int target)
{
int count = 0;
for (int i = 0; i < WIDTH; i++) {
if (array[i] != target) {
count++;
}
}
return count;
}