// 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 any previous, the program will say "Snap!"
// Marc Chee (marc.chee@unsw.edu.au) 14/3/2019, modified June 2019

#include <stdio.h>

#define NUM_TURNS 10

void numberCheck(int prevNumbers[], int number);

int main (void) {
    int prevNums[NUM_TURNS] = {0};
    
    printf("Welcome to my game of Snap!\n");
    printf("You can type in any number until %d turns are over.\n", NUM_TURNS);
    printf("If I've seen that number before, I will say SNAP!\n");
    
    // main game loop. Take one int each turn
    // compare it against all previous ints
    // then add it to prevNums
    int i = 0;
    while (i < NUM_TURNS) {
        printf("Please type in a number: ");
        int input;
        scanf("%d", &input);
        numberCheck(prevNums, input);
        prevNums[i] = input;
        i++;
    }
    return 0;
}

void numberCheck(int prevNumbers[], int number) {
    int i = 0;
    // print "Snap!" if the number has been seen before
    while (i < NUM_TURNS) {
        if (number == prevNumbers[i]) {
            printf("Snap!\n");
        }
        i++;
    }
    return;
}






Resource created Tuesday 25 June 2019, 08:39:50 PM, last modified Sunday 30 June 2019, 02:47:06 AM.

file: snapFinish.c


Back to top

COMP1511 19T2 (Programming Fundamentals) is powered by WebCMS3
CRICOS Provider No. 00098G