Loading .gitignore 0 → 100644 +2 −0 Original line number Diff line number Diff line cube-test .*.swp Makefile 0 → 100644 +11 −0 Original line number Diff line number Diff line # # Makefile # Kevin Lyda, 2017-10-12 19:14 # cube-test: cube-test.c cc -Wall -o cube-test cube-test.c # vim:ft=make # cube-test.c 0 → 100644 +51 −0 Original line number Diff line number Diff line /* * cube-test.c * Copyright (C) 2017 Kevin Lyda <kevin@phrye.com> * * Distributed under terms of the GPL license. */ #include <stdio.h> #include <stdlib.h> #include <time.h> int random_two_digit_number() { return (rand() % 90) + 10; } int cube(int n) { return n * n * n; } int read_number() { char buf[10]; if (!fgets(buf, sizeof buf, stdin)) { return 0; } return atoi(buf); } int main(int argc, char *argv[]) { int guess, number; srand(time(NULL)); while (1) { number = random_two_digit_number(); printf("Number is %d. Cube root (0 to exit)? ", cube(number)); guess = read_number(); if (guess == 0) { exit(0); } if (guess == number) { printf("Correct.\n"); } else { printf("Not correct. Number was %d.\n", number); } } } Loading
Makefile 0 → 100644 +11 −0 Original line number Diff line number Diff line # # Makefile # Kevin Lyda, 2017-10-12 19:14 # cube-test: cube-test.c cc -Wall -o cube-test cube-test.c # vim:ft=make #
cube-test.c 0 → 100644 +51 −0 Original line number Diff line number Diff line /* * cube-test.c * Copyright (C) 2017 Kevin Lyda <kevin@phrye.com> * * Distributed under terms of the GPL license. */ #include <stdio.h> #include <stdlib.h> #include <time.h> int random_two_digit_number() { return (rand() % 90) + 10; } int cube(int n) { return n * n * n; } int read_number() { char buf[10]; if (!fgets(buf, sizeof buf, stdin)) { return 0; } return atoi(buf); } int main(int argc, char *argv[]) { int guess, number; srand(time(NULL)); while (1) { number = random_two_digit_number(); printf("Number is %d. Cube root (0 to exit)? ", cube(number)); guess = read_number(); if (guess == 0) { exit(0); } if (guess == number) { printf("Correct.\n"); } else { printf("Not correct. Number was %d.\n", number); } } }