So I originally wrote my program using #define since I know it happens as a preprocessor, but my teacher commented that I should use const. I tried replacing #define in my code but it just broke it and I am not sure why. I understand that const is usable like a variable right so my code should just be calling it and would get the same result no? Here is a screenshot of what my code does working and not working Here is the working:
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
//Set constant.Preprocessor for speed repace LENGTH
//With what ever number of digits you would want.
#define LENGTH 4
//Converts the input number into an array of digits
int * digitArray(int number) {
//Creating an array for all digits of the constant length
static int digits[LENGTH];
//Loop through the digits and use the mod operator to get each one of them
for(int i = LENGTH-1; i >= 0; i--) {
digits[i] = number % 10;
number = number / 10;
}
//Finally, return the array pointer
return digits;
}
//Compares the correct number array with the guess array of digits, checking for fermi/pica hints and returns
//the number of exact matches of both arrays
int checkGuess(int *num, int *guess) {
//Create two bool arrays to flag the digits already used
bool usedNum[LENGTH] = {false};
bool usedGuess[LENGTH] = {false};
//First loop to check for number of matches
int matches = 0;
int used = 0;
for(int i = 0; i < LENGTH; i++) {
//Check if the digit is the same for both arrays at i index
if(num[i] == guess[i]) {
//If so there is an exact match
usedNum[i] = true;
usedGuess[i] = true;
matches++;
used++;
}
}
And the not working:
const int LENGTH = 4;
//Converts the input number into an array of digits
int * digitArray(int number) {
//Creating an array for all digits of the constant length
static int digits[LENGTH];
//Loop through the digits and use the mod operator to get each one of them
for(int i = LENGTH-1; i >= 0; i--) {
digits[i] = number % 10;
number = number / 10;
}
//Finally, return the array pointer
return digits;
}
//Compares the correct number array with the guess array of digits, checking for fermi/pica hints and returns
//the number of exact matches of both arrays
int checkGuess(int *num, int *guess) {
//Create two bool arrays to flag the digits already used
bool usedNum[LENGTH] = {false};
bool usedGuess[LENGTH] = {false};
//First loop to check for number of matches
int matches = 0;
int used = 0;
for(int i = 0; i < LENGTH; i++) {
//Check if the digit is the same for both arrays at i index
if(num[i] == guess[i]) {
//If so there is an exact match
usedNum[i] = true;
usedGuess[i] = true;
matches++;
used++;
}
}
I know this is a kind of a repeat of some other questions but I didn't see any answers that specifically address why it does not work in this instances and how to fix it so the code runs.