// A Modified Snap game
// The user will input numbers one at a time
// The program will remember previous input from the user
// If the current input matches previous, the program will say "Snap!"
// Marc Chee 14/3/2019
#include <stdio.h>
#define NUM_TURNS 10
void numberCheck(int guess, int numbers[]);
int main (void) {
int prevNums[NUM_TURNS] = {0};
printf("Welcome to my game of Snap!\n");
printf("Please type in a number %d times.\n", NUM_TURNS);
printf("If I've seen that number before, I will say SNAP!\n");
int counter = 0;
while (counter < NUM_TURNS) {
// take user input and store it in the array
printf("Please type in a number: ");
int inputNumber;
scanf("%d", &inputNumber);
numberCheck(inputNumber, prevNums);
prevNums[counter] = inputNumber;
counter++;
}
return 0;
}
// loop through an array and print SNAP! if it finds the guess in the array
void numberCheck(int guess, int numbers[]) {
int counter = 0;
int exitLoop = 0;
while(counter < NUM_TURNS && exitLoop == 0) {
if (guess == numbers[counter]) {
printf("SNAP!\n");
printf("I found %d at %d\n", guess, counter);
exitLoop = 1;
}
counter++;
}
return;
}
Resource created Friday 15 March 2019, 11:49:51 PM, last modified Friday 15 March 2019, 11:50:24 PM.
file: snapLec.c