0% found this document useful (0 votes)
21 views

1103 Lab3

The document provides instructions for three programming exercises. The first asks students to write a program that evaluates different mathematical functions. The second requires writing a program to determine which quadrant a point lies in on a graph. The third is a game that randomly selects between Harry, Ron or Hermione characters and determines a winner.

Uploaded by

sahilpurewal1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

1103 Lab3

The document provides instructions for three programming exercises. The first asks students to write a program that evaluates different mathematical functions. The second requires writing a program to determine which quadrant a point lies in on a graph. The third is a game that randomly selects between Harry, Ron or Hermione characters and determines a winner.

Uploaded by

sahilpurewal1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

KWANTLEN POLYTECHNIC UNIVERSITY

CPSC 1103 – Summer 2024

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.

PROGRAM 1: Function Evaluator


Write a program that asks the user to enter an int value to choose from one of three functions (shown below, where ^ means
exponentiation). After the user chooses one of the functions, the program should ask the user for an integer input value (x) for
the function to evaluate. You may assume the user only enters valid input. The program should then calculate and display the
value of the function for the given input. (Remember you can use the pow() function as needed.) Here is what the program
may look like when run (sample user input is highlighted for your reference):

Choose one of the following functions to evaluate:

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

The value of y is: 5

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.

PROGRAM 2: Point Locator


This program should ask the user to enter an x value and a y value (both integers) that represent a point on a graph. The
program should then write out what quadrant the point is in. If it is on an axis or is on the origin, it should write that out (i.e.
“the point is on an axis”) instead of the quadrant number. Remember the quadrants are:
Quadrant I (x > 0, y > 0), quadrant II (x < 0, y > 0), quadrant III (x < 0, y < 0), quadrant IV (x > 0, y < 0);
To do this, you will need to use a series of selection statements. There are many combinations of if and perhaps if...else
statements that could work. Try to come up with a combination that is clear when you read the code, and that is also efficient.
Here are two examples of what the program may do when it is executed (user input is shown highlighted). You can assume
the user enters valid input (continued on next page):

Enter a value for x: 0 Enter a value for x: 3


Enter a value for y: -4 Enter a value for y: -2

The point (0, -4) is on an axis. The point (3, -2) is in Quadrant IV.

Copyright by Kenward Chin. Not for redistribution or use outside of KPU.


CPSC 1103 – Lab 3

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).

PROGRAM 3: Harry, Ron, Hermione


Write a program to play one round of Harry, Ron, Hermione (like Rock, Paper, Scissors) between a human user and the
computer. The user will choose one of the Harry Potter characters Harry, Ron, or Hermione by typing in a string (remember
to #include <string>), and the computer will randomly choose a character as well. The program should then determine
who won the game (or if there was a tie) by comparing the names and then tell the user who won, with a message that
explains who won and the reason why. Here is an example, with user input highlighted:

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:

srand(unsigned(time(NULL))); // use current time as random number seed

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):

num = rand() % 3; // generate a random number between 0 and 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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy