1103 Lab3
1103 Lab3
LAB 3
Using Selection and Calculation
OBJECTIVES
Upon successful completion of this lab, the student will be able to:
• perform simple arithmetic calculations with integer values
• use basic selection structures such as if and if...else
• use pseudorandom number generation in a program
Write and test each of the following programs. Remember to create a separate Visual Studio project for each program.
1) y = 4x + 7
2) y = (x - 2)^2 + 1
3) y = x^3 - 2x^2 + 3x - 1
Enter choice: 2
Enter x value: 4
Write the program and test it for each function. You should test at least two values of x for each function; try to test with
values that you can verify without resorting to a calculator (for example, x = 12352 is probably a test you can’t verify in your
head!). Submit the listing of your code (using good programming style), and screenshots showing the results of your six tests.
The point (0, -4) is on an axis. The point (3, -2) is in Quadrant IV.
Obtain the program listing when done and sufficient test output to prove that your program works. (Hint: this means you will
have to test at least five points, one for each possible outcome).
The rules of the game are that Harry impresses Ron, Ron kisses
Hermione, and Hermione punches Harry. Thus, Harry beats Ron
Choose Harry, Ron or Hermione: Ron
and loses to Hermione; Ron beats Hermione and loses to Harry;
The computer chooses Harry.
Hermione beats Harry and loses to Ron. (If you want to for fun
I win, because Harry impresses Ron!
you can substitute other action words, but “who beats whom”
should stay the same.)
There are two different issues for you to tackle here. The first is how to get the computer to randomly choose its move, like
the computer chose a random number in Lab 1. In C++, the rand() function lets the program generate random numbers.
When you #include <cstdlib> and do the following with an int variable called num:
num = rand();
C++ assigns a “random” number to num. As explained in the notes, this number is actually pseudorandom. Unless you “seed”
the random number, the program will choose the same number each time you use the program. To seed the number generator
so it won’t pick the same number each time, #include <ctime> and use the following at the start of your main program:
The random number the computer chooses will be between 0 and 32767. To reduce the choice of random numbers to just
three options, use the following to divide the large number by 3 and use the modulus operator (%) to compute the remainder
(which will be either 0, 1 or 2):
You can use the resulting random number to represent math, chemistry or physics depending on whether the number is 0, 1
or 2. Again, look at Lab 1 for an example of how random number generation works.
The second issue is how to determine who has won the round. You will have to check what the user chose and what the
computer chose, and determine what winning, losing or tying message should be displayed. This will require a (longish)
series of if...else if instructions.
Write the program and test it. Submit your code and four screenshots that show that the user can win with each of the
three choices, as well as one tie result. (You’ll have to play multiple times in order to collect the necessary screenshots.)
WHAT TO HAND IN
Complete all the above programs, and obtain the required code and screenshots. You should hand in:
• a word-processed cover page, showing your name, the course name, your section (i.e. S10), the lab number and the
date of submission, all centered horizontally on the page
• the code and test runs for each of the exercises, in the order given above
DUE DATE
Friday, June 7, 2024.
Copyright by Kenward Chin. Not for redistribution or use outside of KPU. Page 2