Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <chrono> // steady_clock
- #include <cstdlib> // rand(), srand()
- #include <ctime> // time()
- using namespace std;
- // Time how fast you can multiply two numbers.
- int main()
- {
- srand(time(NULL)); // Prepare random number generator.
- int first_random_number = 0; // first number to multiply.
- int second_random_number = 0; // first number to multiply.
- // Explain the program.
- cout << "You will be given two numbers to multiply together.\n"
- "\nTime yourself to see how fast you can do it.\n";
- cout << "\nPress enter to see the numbers.\n"
- "Press enter again when you have the answer.\n"
- "You will see the correct answer and your time.\n";
- cout << "\nPress ENTER when ready.\n";
- cin.ignore(numeric_limits<streamsize>::max(), '\n'); // Wait till ready.
- first_random_number = (rand() % 89) + 11; // Generate first number to multiply.
- second_random_number = (rand() % 9) + 1; // Generate second number to multiply.
- cout << first_random_number << " * " << second_random_number << '\n'; // Display the problem.
- chrono::steady_clock::time_point start = std::chrono::steady_clock::now(); // Start timing.
- cin.ignore(numeric_limits<streamsize>::max(), '\n'); // Wait till user is done.
- chrono::steady_clock::time_point stop = std::chrono::steady_clock::now(); // Stop timing.
- float elapsed_time = std::chrono::duration_cast<std::chrono::milliseconds>(stop - start).count(); // Calculate elapsed time.
- float elapsed_time_seconds_decimal = elapsed_time / 1000; // Convert milliseconds to seconds.
- cout << "The answer is " << first_random_number * second_random_number << '\n'; // Display the answer.
- cout << "\nYour time: " << elapsed_time_seconds_decimal << " seconds\n"; // Display how long it took.
- return 0; // End the program.
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement