diff options
Diffstat (limited to '10_gdb')
-rw-r--r-- | 10_gdb/README | 80 | ||||
-rw-r--r-- | 10_gdb/game.c | 44 | ||||
-rw-r--r-- | 10_gdb/grade.txt | 4 | ||||
-rw-r--r-- | 10_gdb/input.txt | 3 |
4 files changed, 131 insertions, 0 deletions
diff --git a/10_gdb/README b/10_gdb/README new file mode 100644 index 0000000..3044c3c --- /dev/null +++ b/10_gdb/README @@ -0,0 +1,80 @@ +For this assignment, you will be practicing with the basics +of gdb. You will want to use gdb extensively during the rest +of the semester, as it is an incredibly useful tool to gather +information during the debugging process. For now, we +are just going to get started with some basic commands. +Be sure you have done the readings and watched +the video on gdb. + +Enclosed, you will find game.c and the compiled +binary game, for the most boring guessing game +ever. In the first round, the program thinks +of a secret number (it is the same every single time), +and asks you to guess it. You get exactly one try. +Note that it thinks of this number by calling +getSecretNumber, which is not included in the source. + +If you get that right, in the second round, +it thinks of another number. It does this by repeatedly +calling getOtherSN (also not shown in the source) and +passing in different numbers. It combines these results +together, and does some math to combine the results into +"total". Afterwards, it asks you for your guess, +and again, you have one chance. + +Before you proceed, try to "play" the game once by running: + +./game + + +For example, I ran it and guessed 4: +$ ./game +I'm thinking of a number... +What number do you guess? +4 +I'm sorry, that is not right. You lose + +It would be pretty boring to play this game +until you actually guess the right number and win, +but fortunately, that isn't the point. The +point is to practice with gdb. + +In emacs, do + +ESC-x gdb + +Emacs will prompt you for how to run gdb (the default should be fine), +and then give you the gdb prompt. +Use the "start" commmand to begin execution, then use "next" +and "print" to find the secret number for round 1. +When the program prompts you for this number, you should be able +to guess the right one from the information you gathered. + +For round 2, you do not want to step through 5000+ iterations of the +loop, so set a breakpoint after the loop, continue execution until you +reach it, and the print out the variable "total". Now you should +be able to win round 2 instantly as well! + +Once you have found the two secret numbers, create a file called input.txt +and place them in that file, one per line (round 1's secret number +on the first line, and round 2's on the second line). + +You should be able to run + +./game < input.txt + +and "win" automatically. That is, you should see this output (without +having to type anything else): + +$ ./game < input.txt +I'm thinking of a number... +What number do you guess? +Correct! You win round1! +Ok, time for round 2. I have another secret number. +Your guess: +You win round 2 also! + + +When you finish, add input.txt to git, then commit, push, and grade. + + diff --git a/10_gdb/game.c b/10_gdb/game.c new file mode 100644 index 0000000..a7459d3 --- /dev/null +++ b/10_gdb/game.c @@ -0,0 +1,44 @@ +#include <stdio.h> +#include <stdlib.h> +#include <sys/time.h> +int getSecretNumber(void); //prototype, implemented elsewhere. + +int getOtherSN(int which); //prototype, implemented elsewhere. + +int main(void) { + int guessesMade = 0; + int yourGuess; + char buffer[1024]; + int myNumber = getSecretNumber(); + + printf("I'm thinking of a number...\n"); + printf("What number do you guess?\n"); + if(fgets(buffer, 1024, stdin) == NULL) { + printf("Oh no, you are giving up? You lose...\n"); + return EXIT_FAILURE; + } + yourGuess = atoi(buffer); + if(yourGuess != myNumber) { + printf("I'm sorry, that is not right. You lose\n"); + return EXIT_FAILURE; + } + printf("Correct! You win round1!\n"); + + int total = 0; + for (int i = 0; i <= 5678; i++) { + total = total ^ getOtherSN(i); + } + printf("Ok, time for round 2. I have another secret number.\n"); + printf("Your guess:\n"); + if(fgets(buffer, 1024, stdin) == NULL) { + printf("Oh no, you are giving up? You lose...\n"); + return EXIT_FAILURE; + } + yourGuess = atoi(buffer); + if (yourGuess == total) { + printf("You win round 2 also!\n"); + return EXIT_SUCCESS; + } + printf("Sorry, you did not win the second round\n"); + return EXIT_FAILURE; +} diff --git a/10_gdb/grade.txt b/10_gdb/grade.txt new file mode 100644 index 0000000..25bb058 --- /dev/null +++ b/10_gdb/grade.txt @@ -0,0 +1,4 @@ +Grading at Mon 29 Nov 2021 02:12:07 AM UTC +Your file matched the expected output + +Overall Grade: PASSED diff --git a/10_gdb/input.txt b/10_gdb/input.txt new file mode 100644 index 0000000..9bbd0ca --- /dev/null +++ b/10_gdb/input.txt @@ -0,0 +1,3 @@ +464384013 +938257400 + |