Time for us to start writing computer games! This first one will be a bit simple, but it's a start. Our task is to write a program that implements a guessing game. Our program generates a random number between 0 and 100. The player must guess the secret number. The program provides hints like "that's too high" or "that's too low" until the player finally guesses the secret number.
We will work on this game in three steps.
// random.cpp. Maggie Johnson
// Description: Illustrates how to generate a random number in Visual C++
#include <iostream>
#include <time.h>
using namespace std;
int main () {
int random_number;
// Initialize random seed.
srand (time(NULL));
// Generate random number between 1 and 100
random_number = rand() % 100 + 1;
cout << "Your random number:" << random_number << endl;
system("pause");
}
For step 2, we need to receive an integer input from the player (with appropriate error-checking on cin), and check it against the secret number. Try and write this part of the game yourself before checking our solution.
Finally, we need to add a loop that keeps collecting guesses from the player until they finally guess the secret number. After completing this part of the program, you can check our solution.
Decomposition is one of the most important skills for a programmer to learn. Being able to break a task down into manageable pieces, and then complete one at a time is critical, no matter how big or how small the project. Here are some other opportunities for you to practice decomposition.
We will work on this game in three steps.
- Figure out how to generate a random number within a given range of values.
- Create a main function that processes one guess from the player, and provides hints.
- Add what we need to allow for multiple guesses until the player guesses the number.
// random.cpp. Maggie Johnson
// Description: Illustrates how to generate a random number in Visual C++
#include <iostream>
#include <time.h>
using namespace std;
int main () {
int random_number;
// Initialize random seed.
srand (time(NULL));
// Generate random number between 1 and 100
random_number = rand() % 100 + 1;
cout << "Your random number:" << random_number << endl;
system("pause");
}
For step 2, we need to receive an integer input from the player (with appropriate error-checking on cin), and check it against the secret number. Try and write this part of the game yourself before checking our solution.
// guess.cpp. Maggie Johnson // Description: A guessing game where the player guesses // the secret number. #include <iostream> #include <time.h> using namespace std; int main () { int random_number, guess; // Initialize random seed. srand (time(NULL)); // Generate random number between 1 and 100 random_number = rand() % 100 + 1; cout << "Guess our number (1 to 100) "; if (!(cin >> guess)) { cout << "Please enter only numbers" << endl; } else { if (random_number < guess) cout << "The secret number is lower than " << guess << endl; else if (random_number > guess) cout << "The secret number is higher than" << guess << endl; else cout << "You guessed it: " << random_number << endl; } cout << "random: " << random_number << endl; return 0; }
Finally, we need to add a loop that keeps collecting guesses from the player until they finally guess the secret number. After completing this part of the program, you can check our solution.
// guess.cpp. Maggie Johnson // Description: A guessing game where the player guesses // the secret number. #include <iostream> #include <time.h> #include <cstdlib.h> using namespace std; int main () { int random_number, guess; // Initialize random seed. srand (time(NULL)); // Generate random number between 1 and 100 random_number = rand() % 100 + 1; cout << "Guess our number (1 to 100) "; do { if (!(cin >> guess)) { cout << "Please enter only numbers" << endl; } else { if (random_number < guess) cout << "The secret number is lower than " << guess << endl; else if (random_number > guess) cout << "The secret number is higher than " << guess << endl; } } while (random_number != guess); cout << "Congratulations!" << endl; return 0; }
Decomposition is one of the most important skills for a programmer to learn. Being able to break a task down into manageable pieces, and then complete one at a time is critical, no matter how big or how small the project. Here are some other opportunities for you to practice decomposition.
- Many everyday tasks provide opportunities to build skills in decomposition. It might surprise you to discover that your mind just works this way! For example, if I have to clean up a really messy kitchen (perhaps some little girls just baked a cake), I break the tasks down to provide a plan, and to make sure I don't have to re-do work later. I don't want to wash the floor first if there's all kinds of stuff on the counters to clean up - I'll probably have to do the floor again later. So, maybe I put all the ingredients away first; move all the dishes into the sink; clean off the counters; wash the dishes; and finally, clean up the floor. There are other ways to perform this task, but by thinking it through first, it helps me define a plan that will not require re-work.
Use everyday tasks as examples for developing your decomposition skills. Who knows - it might even make you more efficient around the house! - The greatest common divisor of two integers is the largest number that divides them both evenly. For example, gcd(12, 18) = 6, gcd(−4, 14) = 2. The most efficient way to compute gcd is with the Euclidean algorithm. Write a program with a function to compute gcd for two integers. Try doing the function without recursion first - it will help you understand how the algorithm works. Explicitly define your sub-tasks, for example, find the code for the Euclidean algorithm; create a non-recursive function for the algorithm; etc. Here is our solution.
// gcd.cpp, Maggie Johnson // Description: An implementation of Euclid's algorithm for computing gcd. // Fundamental idea of Euclid's algorithm (one of the oldest known algorithms) // for finding the greatest common divisor of two integers: // gcd(a, 0) = a // gcd(a, b) = gcd(b, a % b) // % means modulo, that is, the remainder of a/b. // For example, gcd(6, 4) = 2; gcd(12, 18) = 6. #include <iostream> using namespace std; // A non-recursive version of Euclid's algorithm int gcd (int a, int b) { int temp; while (b != 0) { temp = a % b; a = b; b = temp; } return(a); } int main () { int x, y; cout << "Enter two integers: "; if (!(cin >> x >> y)) { cout << "Please enter only integers" << endl; } else { cout << "gcd(" << x << ", " << y << ") = " << gcd(x,y) << endl; } return(0); }
Comments
Post a Comment