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

Lab Assignment 4

Uploaded by

jacob.dubon21
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Lab Assignment 4

Uploaded by

jacob.dubon21
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

COSC 1436

Lab Assignment 4 – Selection Structures (If & Switch)


Write the following C++ programs; then build and run them. When they build and run correctly,
copy the code into a separate text file for each program, with the same name as that program.
Submit the text files.

NOTE: All programs that you write must have comments at the top with the program name, your
name, and a description of what the program will do.

1. Create a program called Lab4A that will calculate an employee’s pay for the week.
Ask the user for the employee’s pay rate and the total hours that the employee worked during
the week. (I recommend making the variables for these doubles.)
Follow the logic steps given below.

 If totalHours is greater than 40 then


o Set regularHours = 40
o Calculate how many overtime hours they worked (hours over 40),
o Calculate regular pay as 40 * pay rate
o Calculate overtime pay as overtime hours * pay rate * 1.5
o totalPay is overtime pay + regular pay
o Print the output as follows:
regHours regular and overtimeHours overtime at payRate = totalPay

 else do the following


o Set regularHours = totalHours
o Calculate total pay as total hours * pay rate
o Print the output as follows:
regHours regular at payRate = totalPay

2. Write a program named Lab4B that will do the following.


o Input 3 integers from a text file named Input4B.txt and print the numbers with
labels.
o If two of the numbers can be multiplied to get the third, print a statement to say
that.
o Otherwise, print a statement to say that it cannot be done.
o HINT: you will need an if – else if structure because you do not know which of the 3
numbers will be the largest.
(Once you have finished and tested the program, and it runs correctly, change the
file to Input4B2.txt to test it. Then test it two more times with Input4B3.txt and
Input7C4.txt. Only write the program to input from 1 text file, but when you are
testing it, change the name of the text file each time you run it, to make sure it
works correctly for all the sets of numbers.)

3. Write a program named Lab4C that will determine if a student is in elementary, middle, or high
school based on his grade.
 Ask the user to input an integer grade level (1-12).
 Add an if statement that checks for input failure when the user enters the grade. If
there’s an error, it should print an error message and exit the program. (This would be
for something like a letter being typed instead of a number.)
COSC 1436
 Use if statements to determine which school the student will attend – elementary,
middle, or high school. Grades 1-5 will be in elementary, 6-8 will be in middle school
and 9-12 will be in high school.
 Print your results in the following format:
Grade studentGrade goes to studentSchool
So if the user types 8, the output would be
Grade 8 goes to middle school
If they enter 10, it would be
Grade 10 goes to high school
And so on….

4. Write a program named Lab4D that will act as a calculator, allowing the user to type in an
equation and calculating the result.
a. Ask the user to enter 2 integers with a character between them, all separated by spaces.
The character should be +, -, *, /, or % (So you should have 2 int variables and 1 char
variable for these.)
b. Input the 2 numbers and the character operator.
c. Write a switch structure based on the char variable, that will calculate the result of the
mathematical operation on the numbers.
d. Print the whole equation with the result in the following format:
If the user types in 40 / 5, then the output should be: 40 / 5 = 8
(You have worked with output statements enough that you should be able to do this.)

Sample input/output below (These are just examples of what the user could type in.)
Input Output
27 + 15 27 + 15 = 42
46 / 5 46 / 5 = 9
10 - 2 10 - 2 = 8
55 % 2 55 % 2 = 1

Hint – You input 2 integers (perhaps num1 & num2) from the user, and you also get a char
value for the operator (perhaps we can call it op). Set up a switch statement based on op.
 If op is ‘+’, then the answer is num1 + num2
 If op is ‘-‘, then the answer is num1 – num2,
 And so on…
After your switch statement print both numbers, the character operator and the answer in the
format shown.

(And if you still run into issues with it, you could always ask your professor for help.)

You might also like