EECS168:Lab3
Navigation |
---|
Home |
Information |
Classwork |
Contents
Due Date
- This lab is due June 26th by 11:59pm
Code Comments
Comments are descriptions that the creator of the code uses to describe what the code is doing. It does not affect the code and is only meant for those who are are looking at the source code. In C++ there are a few different ways of creating comments. To make a single line comment you only need to type // in front of your comment.
// This is a single line comment
To make a multiple line comment you must place a /* or /** at the front of your comment and end it with a */ for either way.
/* This is a multiple
* line comment
*/
/** Notice that we have to put an asterisk
* at the front of each line
*/
- Note: The multiple line comments can also be used as single line comments with the */ appearing at the end of the line
Objectives
- Understand the while loop and do-while loop.
- Learn and use random number generator
- Start using the debugger
while Loops Syntax
//optional initialization while( looping condition ) { //code that repeats //optional progression }
do-while Loops
A do-while statement is similar to the while statement except that the loop body is always executed at least once. After one iteration the do-while behaves exactly like the while statement.
do { Statement 1 Statement 2 . . Statement n } while(Boolean expression);
Example of do-while loop:
int count_down = 0;
std::cout << "How many greetings do you want?: ";
std::cin >> count_down;
do
{
std::cout << "Hello" << std::endl;
count_down = count_down-1;
}while (count_down > 0);
std::cout << "Thats all!" << std::endl;
Sample output
How many greetings do you want?
0
Hello
Thats all!
Random Numbers
C++ offers many ways to generate random numbers. Today we'll cover an older way, or C-style way, of generating a random number.
Here's a sample dice program:
#include <iostream> #include <stdlib.h> #include <time.h> int main() { int dice=0; srand( time(NULL) ); //Roll the dice 100 times for(int i=0; i<99; i++) { dice = rand() % 6 + 1; //Random number between 1 and 6; std::cout << "you rolled: " << dice << '\n' ; } }
Notes:
- You only need to seed the random number generator (RNG) once
- Once the RNG is seeded you can call rand() to get back a random positive integer
- The NULL is a keyword from C that is meant to represent nothing, but is actually just a compiler macro for zero
Exercise 1: Character Count
Create a program, CharacterCount that will obtain a string (assume no whitespace) from the user. Then ask the user for a single character. Finally print the count of occurences of that that character in the string.
Only use the at and length methods
Sample run:
Enter a string: aabajdndkiAaaAa Enter a character to count: a In the string aabajdndkiAaaAa the character 'a' occurs 6 time(s)
Exercise 2: Guess a Number Game
Create a program, NumberGuessingGame that will make the user guess a random number between 1 and 100 (inclusive). The game won't end until the user guesses the correct number, but along the way you will help the user.
Until the user guesses the secret number...
- Obtain a guess
- If it's higher than the number, tell the user
- If it's lower than the number, tell the user
- If they guess the number, the game stops and they are prompted to either play again or exit
After they win...
- Congratulate them
- Tell them how many guesses it took
- Tell what their closest incorrect guess was
- If multiple guesses are equally close, display the most recent one
Use do while loops in your program.
Sample run (where the secret number is 73):
Welcome to the number guessing game.
Guess a number: 45
Sorry, you guess is too low.
Guess a number: 99
Sorry, your guess is too high.
Guess a number: 74
Sorry, your guess is too high.
Guess a number: 72
Sorry, your guess is too low.
Guess a number: 73
You win!
You guessed the secret number after 5 guess(es).
Your closest guess was 72.
Would you like to play again? (y/n): n
Exercise 3: ASCII
Let's make an ASCII program! We will allow the user to see the ASCII representation of a specific number they want or to see all the ASCII conversions from 33 to 126.
Recall that an int can be casted to a char by doing the following:
//Print the int x as a char int x = 97; //C-style cast std::cout << (char)x << '\n'; //C++ cast std::cout << static_cast<char>(x) << '\n';
Present the user with the following menu:
1) Select a specific ASCII character provided an int 2) Display visible ASCII values (33 to 126) 3) Exit
Requirements:
- If the user chooses a specific value, it must be from 33 to 126 inclusively
- When displaying the conversion, always do so in the following format:
- <int value> = <char value>
- Example:
- 97 = a
- The program does not end until the user wants to exit
Sample run:
1) Select a specific number 2) Display visible ASCII values (33 to 126) 3) Exit Choice: 1 Enter value: 97 97 = a 1) Select a specific number 2) Display visible ASCII values (33 to 126) 3) Exit Choice: 2 33 = ! 34 = " 35 = # 36 = $ 37 = % 38 = & 39 = ' 40 = ( 41 = ) 42 = * 43 = + 44 = , 45 = - 46 = . 47 = / 48 = 0 49 = 1 50 = 2 51 = 3 52 = 4 53 = 5 54 = 6 55 = 7 56 = 8 57 = 9 58 = : 59 = ; 60 = < 61 = = 62 = > 63 = ? 64 = @ 65 = A 66 = B 67 = C 68 = D 69 = E 70 = F 71 = G 72 = H 73 = I 74 = J 75 = K 76 = L 77 = M 78 = N 79 = O 80 = P 81 = Q 82 = R 83 = S 84 = T 85 = U 86 = V 87 = W 88 = X 89 = Y 90 = Z 91 = [ 92 = \ 93 = ] 94 = ^ 95 = _ 96 = ` 97 = a 98 = b 99 = c 100 = d 101 = e 102 = f 103 = g 104 = h 105 = i 106 = j 107 = k 108 = l 109 = m 110 = n 111 = o 112 = p 113 = q 114 = r 115 = s 116 = t 117 = u 118 = v 119 = w 120 = x 121 = y 122 = z 123 = { 124 = | 125 = } 126 = ~ 1) Select a specific number 2) Display visible ASCII values (33 to 126) 3) Exit Choice: 3
Display an error message if they give an invalid choice, but you can assume they will input a number.
Exercise 4: Outbreak!
Flu season is upon us and the number of people getting sick is growing.
- On day 1, there was only 1 person with the flu.
- On day 2, it jumped to 5.
- On day 3, there were 17
- Every day since, the number of people who have the flu is equal to the last 3 days combined
You will make a program that will ask the user for what day they want a count of people with the flu for. Then display the amount.
Sample runs:
OUTBREAK! What day do you want a sick count for?: 1 Total people with flu: 1
OUTBREAK! What day do you want a sick count for?: 2 Total people with flu: 5
OUTBREAK! What day do you want a sick count for?: 3 Total people with flu: 17
OUTBREAK! What day do you want a sick count for?: 4 Total people with flu: 23
OUTBREAK! What day do you want a sick count for?: 5 Total people with flu: 45
OUTBREAK! What day do you want a sick count for?: 0 Invalid day
Lab Submission
- Your tarball and the directory inside it should be named like Smith-123456-Lab-4
- DO NOT put your .o or executable files in your package.
- Ask your TA if they use Blackboard or email
168 Rubric
- [2pts] File name of tarball correct
- [3pts] Comments and formatting
- [10pts] Clean output very closely matches sample output
- [10pts] Exercise 1: Character count
- Correct character count calculated
- [10pts] Exercise 2: Number Guessing Game
- [2pts]do while loops used
- [2pts]Number is random
- [2pts]User given correct feedback
- [2pts]Game only ends when the user guess correctly without any extra displays
- [2pts]User can choose to play again
- [20pts] Exercise 3: ASCII
- [5pts] Checks for valid selections
- [10pts] Print range correctly
- [5pts] Prints single value correctly
- [45pts] Exercise 4: Outbreak!
- [5pts] Displays error for invalid days
- [10pts] Base cases (days 1 - 3) print correct values
- [30pts] Days greater than 3 display correct values