Prog 1: Guess Number
Change Log:
1/12 Output was changed to count the number of values that have duplicate digits, as announced 1/11 in class. Output now also shows multiple runs of the program. YouTube video was replaced to reflect the above change.
1/18 Program description changed to specify that reversing a number need not be in a function. Shown in blue below.
1/19 Fixed further language that specified functions. Shown in green below.
1/23 The instructions ask you to test all possible values between 1..1000. Assume this does *not* include 1000. My sample output below incorrectly starts at 0 rather than at 1, as it should. For this reason we will accept a count value of either 279 (when starting at 1) or 280 (when starting at 0).
Write a program that interacts with the user, and predicts their final number! (This idea came from this YouTube video.)
Running your program should look like the following, where user input is shown in bold:
Author: Dale Reed Program: #1, Guess Number TA: Karla Templar, Tues 10 Jan 8, 2017 Welcome to the number guessing game! If you concentrate, sometimes you can connect to the electrons in the computer! Let's try it. Think of a three digit number. (To make it harder, make the digits all different from each other). Type in your number: 532 I'll help you with the math. Lets randomize those digits by reversing them, and do a subtraction: 532 (The original number) - 235 (The reversed digits) ===== 297 Press 'Y' to continue or 'X' to exit... y Now lets again scramble the numbers by reversing them, and adding them this time: 297 + 792 ===== ? Before you continue, take a look at my number guess written down on paper. Press 'D' to display the answer or 'X' to exit... d Answer is 1089. Press 'T' to test all possible combinations or 'X' to exit... x
Running the program again for a different input gives the following:
Author: Dale Reed Program: #1, Guess Number TA: Karla Templar, Tues 10 Jan 8, 2017 Welcome to the number guessing game! If you concentrate, sometimes you can connect to the electrons in the computer! Let's try it. Think of a three digit number. (To make it harder, make the digits all different from each other). Type in your number: 275 I'll help you with the math. Lets randomize those digits by reversing them, and do a subtraction: 572 (The reversed digits) - 275 (The original number) ===== 297 Press 'Y' to continue or 'X' to exit... y Now lets again scramble the numbers by reversing them, and adding them this time: 297 + 792 ===== ? Before you continue, take a look at my number guess written down on paper. Press 'D' to display the answer or 'X' to exit... d Answer is 1089. Press 'T' to test all possible combinations or 'X' to exit... x
Running the program again for input with duplicate digits gives the following:
Author: Dale Reed Program: #1, Guess Number TA: Karla Templar, Tues 10 Jan 8, 2017 Welcome to the number guessing game! If you concentrate, sometimes you can connect to the electrons in the computer! Let's try it. Think of a three digit number. (To make it harder, make the digits all different from each other). Type in your number: 424 *** All three digits are not different. Exiting program... Program ended with exit code: 0
Running the program again with a single digit input value again fails, since a single digit implicitly has two preceding zeros, which are duplicate digits:
Author: Dale Reed Program: #1, Guess Number TA: Karla Templar, Tues 10 Jan 8, 2017 Welcome to the number guessing game! If you concentrate, sometimes you can connect to the electrons in the computer! Let's try it. Think of a three digit number. (To make it harder, make the digits all different from each other). Type in your number: 9 *** All three digits are not different. Exiting program... Program ended with exit code: 0
Running the program again and this time going all the way through gives the following output. Note that you can display the large table of output numbers just be printing the numbers and letting them wrap around the screen. The width of your screen may result in the line wraps being different than what is shown below:
Author: Dale Reed Program: #1, Guess Number TA: Karla Templar, Tues 10 Jan 8, 2017 Welcome to the number guessing game! If you concentrate, sometimes you can connect to the electrons in the computer! Let's try it. Think of a three digit number. (To make it harder, make the digits all different from each other). Type in your number: 35 I'll help you with the math. Lets randomize those digits by reversing them, and do a subtraction: 530 (The reversed digits) - 35 (The original number) ===== 495 Press 'Y' to continue or 'X' to exit... Y Now lets again scramble the numbers by reversing them, and adding them this time: 495 + 594 ===== ? Before you continue, take a look at my number guess written down on paper. Press 'D' to display the answer or 'X' to exit... D Answer is 1089. Press 'T' to test all possible combinations or 'X' to exit... t For all numbers between 1..1000, the values that do not work are: 0 1 2 3 4 5 6 7 8 9 10 11 20 22 30 33 40 44 50 55 60 66 70 77 80 88 90 99 100 101 110 111 112 113 114 115 116 117 118 119 121 122 131 133 141 144 151 155 161 166 171 177 181 188 191 199 200 202 211 212 220 221 222 223 224 225 226 227 228 229 232 233 242 244 252 255 262 266 272 277 282 288 292 299 300 303 311 313 322 323 330 331 332 333 334 335 336 337 338 339 343 344 353 355 363 366 373 377 383 388 393 399 400 404 411 414 422 424 433 434 440 441 442 443 444 445 446 447 448 449 454 455 464 466 474 477 484 488 494 499 500 505 511 515 522 525 533 535 544 545 550 551 552 553 554 555 556 557 558 559 565 566 575 577 585 588 595 599 600 606 611 616 622 626 633 636 644 646 655 656 660 661 662 663 664 665 666 667 668 669 676 677 686 688 696 699 700 707 711 717 722 727 733 737 744 747 755 757 766 767 770 771 772 773 774 775 776 777 778 779 787 788 797 799 800 808 811 818 822 828 833 838 844 848 855 858 866 868 877 878 880 881 882 883 884 885 886 887 888 889 898 899 900 909 911 919 922 929 933 939 944 949 955 959 966 969 977 979 988 989 990 991 992 993 994 995 996 997 998 999
There are 280 values that do not work. Thanks for playing!
In the first step where you do the subtraction be sure to always put the largest number on the top. You can watch a ~three-minute YouTube video of this program running. Your program should handle any input between 1 and 999 and must do the calculations at the end to count how many values do not work (i.e. how many don't have three distinct digits, or that after the computation don't end up equaling 1089).
You need to know how to:
Compile and run a program
Display output using printf
Read user input into an int variable using scanf
Peel off individual digits on the int variable using the modulo operator % that gives the remainder
Combine digits back together into a single number
Compare the three digits to verify they are different from each other
Use a loop to test all possible values
It is also helpful - though not essential - for you to break up your program using functions. I suggest you write the program using the following steps, for the various amounts of credit shown:
(0 points) Display the identifying information and the instructions. Each of these should be a separate function, with no parameters and no return value.
(5 points) Prompt for and read in user input. This should be a separate function that returns an int.
(10 points) Write reverseNumber() function code to separate out the individual digits, make it into a new reversed number, and display it. This should be a separate function that "catches" the original number and returns the reversed number.
(5 points) Do the subtraction and store the answer in a variable. Be sure to display the largest number first when doing the subtraction. You will need to use an "if" statement for this. Display the result.
(5 points) Prompt to continue and get user input. This should be a function that returns a char or code that gets that character.
(5 points) Again reverse the number and this time do the addition. Again use the reverseNumber() function that you wrote above or write equivalent code to reverse the number.
(25 points) Test all possible numbers between 1..1000. Display which values do not work, and the total of how many of those numbers do not work for this trick, either because they have duplicate digits or because the result of the computation does not equal 1089. You will need to use a loop for this. Your program must compute these values, so you can't just print out the results from the program shown above.
Be sure to read the grading criteria on the course syllabus before turning in your program. Turn in your program using Blackboard. Multiple submissions are allowed, but only the most recent submission will be graded.
The name of the program you will turn in should be guessnum followed by your netid and the .c file extension. In other words, if your netid is reed then your program would be called guessnumreed.c
You must also zip up the file you turn in, which typically is done by right-clicking on it and choosing the "compress" option. After zipping your program (e.g. guessnumreed.c), you will likely end up with a file called something like guessnumreed.zip. Only turn in this single file, turning it in on Blackboard into the assignment Program 1. Please do not put it into a folder before zipping it, rather zip the one file by itself.
Failing to follow these naming conventions and failure to turn in a zip file will result in a 5 point deduction, even if everything else is perfect in your program.