- Write C++ programs
- Compile C++ programs
- Implement programs that use loops
Instructions
Answer the programming problems sequentially (i.e., answer prob01 before prob02). If you have questions let your instructor or the lab assistant know. You can also consult your classmates.
When you answer two programming problems correctly, let your instructor know and wait for further instruction.
Lab exercise guide
Here’s a link to the Lab exercise guide in case you need to review the lab exercise objectives, grading scheme, or evaluation process.
Sales report
Create a program to generate a monthly sales report. The program should ask the user to provide the monthly sales for the year and it will display the total sales and the average monthly sales for the year. Please see the sample output below to guide the design of your program.
Sample Input/Output
Annual sales report===================Please provide the monthly sales for the year.Month 1: $300.25Month 2: $400.75Month 3: $350.21Month 4: $601.28Month 5: $702.45Month 6: $265.45Month 7: $365.89Month 8: $291.18Month 9: $500.37Month 10: $333.68Month 11: $450.92Month 12: $600.01Total sales: $5162.44Average monthly sale: $430.20
Submission checklist
- Compiled and ran the driver (
main
). - Manually checked for compilation and logical errors.
- Ensured no errors on the unit test (
make test
). - Followed advice from the stylechecker (
make stylecheck
). - Followed advice from the formatchecker to improve code readbility (
make formatcheck
).
Code evaluation
Open the terminal and navigate to the folder that contains this exercise. Assuming you have pulled the code inside of /home/student/labex02-tuffy
and you are currently in /home/student
you can issue the following commands
cd labex02-tuffy
You also need to navigate into the problem you want to answer. To access the files needed to answer problem 1, for example, you need to issue the following command.
cd prob01
When you want to answer another problem, you need to go back up to the parent folder and navigate into the next problem. Assuming you are currently in prob01
, you can issue the following commands to go to the parent folder then go into another problem you want to answer; prob02
for example.
cd ..cd prob02
Use the clang++
command to compile your code and the ./
command to run it. The sample code below shows how you would compile code save in main.cpp
and into the executable file main
. Make sure you use the correct filenames required in this problem. Take note that if you make any changes to your code, you will need to compile it first before you see changes when running it.
clang++ -std=c++17 main.cpp -o main./main
You can run one, two, or all the commands below to test
your code, stylecheck
your code’s design, or formatcheck
your work. Kindly make sure that you have compiled and executed your code before issuing any of the commands below to avoid errors.
make testmake stylecheckmake formatcheck
A faster way of running all these tests uses the all
parameter.
make all
Game discount
Your favorite video game shop is having a special deal. The deal states that if you buy a game that is priced at $60.00 or more, your total cost will be 20% off. Write a program that computes the total cost of the game(s) that you decide to buy.
The program should ask the user to input the number of games they wish to purchase and ask them to provide the price for each of those games. The program will display the total cost of the games with the discount if it is applicable. It should also show the amount saved after the discount. Take note that different user input results in a different output.
The system will display an error message if the customer inputs an invalid number of games (i.e., 0 or less).
Please see the sample output below to guide the design of your program.
Sample output
Please input the number of game(s) you want to purchase: 3Enter the price of the game 1: $70.00Enter the price of the game 2: $30.00Enter the price of the game 3: $24.00The total cost of 3 game(s) is $99.20You saved $24.80
Sample invalid input and its output
Please input the number of game(s): 0You need to purchase at least 1 game. Please rerun the program.
Submission checklist
- Compiled and ran the driver (
main
). - Manually checked for compilation and logical errors.
- Ensured no errors on the unit test (
make test
). - Followed advice from the stylechecker (
make stylecheck
). - Followed advice from the formatchecker to improve code readbility (
make formatcheck
).
Code evaluation
Open the terminal and navigate to the folder that contains this exercise. Assuming you have pulled the code inside of /home/student/labex02-tuffy
and you are currently in /home/student
you can issue the following commands
cd labex02-tuffy
You also need to navigate into the problem you want to answer. To access the files needed to answer problem 1, for example, you need to issue the following command.
cd prob01
When you want to answer another problem, you need to go back up to the parent folder and navigate into the next problem. Assuming you are currently in prob01
, you can issue the following commands to go to the parent folder then go into another problem you want to answer; prob02
for example.
cd ..cd prob02
Use the clang++
command to compile your code and the ./
command to run it. The sample code below shows how you would compile code save in main.cpp
and into the executable file main
. Make sure you use the correct filenames required in this problem. Take note that if you make any changes to your code, you will need to compile it first before you see changes when running it.
clang++ -std=c++17 main.cpp -o main./main
You can run one, two, or all the commands below to test
your code, stylecheck
your code’s design, or formatcheck
your work. Kindly make sure that you have compiled and executed your code before issuing any of the commands below to avoid errors.
make testmake stylecheckmake formatcheck
A faster way of running all these tests uses the all
parameter.
make all
Operations
Write a program that will ask the user to perform an operation on two numbers given a user’s input.
The program should ask the user to provide the type of operation, where ‘+’ refers to addition, ‘-‘ refers to subtraction, ‘*’ refers to multiplication, and ‘/’ refers to division. They can also input ‘x’ to exit the program. If the user provides a valid operation then the programs asks the user to input two numbers that will be used in performing the operation. The program should display an error message if the user provides an invalid operation character.
When the program receives a valid operation and two input values, perform the given operation on the inputs and display the results on the screen. Take note that different user input results in a different output. Please see the sample output below to guide the design of your program.
Sample output
Please enter the operation (+, -, *, /) or x to exit the program: +Please input the first number: 5Please input the second number: 6You chose to add: 5 + 6 = 11Please enter the operation (+, -, *, /) or x to exit the program: x
Please enter the operation (+, -, *, /) or x to exit the program: /Please input the first number: 8Please input the second number: 3You chose to divide: 8 / 3 = 3Please enter the operation (+, -, *, /) or x to exit the program: -Please input the first number: 7Please input the second number: 5You chose to subtract: 7 - 5 = 2Please enter the operation (+, -, *, /) or x to exit the program: x
Please enter the operation (+, -, *, /) or x to exit the program: @Invalid operationPlease enter the operation (+, -, *, /) or x to exit the program: x
Please enter the operation (+, -, *, /) or x to exit the program: x
Submission checklist
- Compiled and ran the driver (
main
). - Manually checked for compilation and logical errors.
- Ensured no errors on the unit test (
make test
). - Followed advice from the stylechecker (
make stylecheck
). - Followed advice from the formatchecker to improve code readbility (
make formatcheck
).
Code evaluation
Open the terminal and navigate to the folder that contains this exercise. Assuming you have pulled the code inside of /home/student/labex02-tuffy
and you are currently in /home/student
you can issue the following commands
cd labex02-tuffy
You also need to navigate into the problem you want to answer. To access the files needed to answer problem 1, for example, you need to issue the following command.
cd prob01
When you want to answer another problem, you need to go back up to the parent folder and navigate into the next problem. Assuming you are currently in prob01
, you can issue the following commands to go to the parent folder then go into another problem you want to answer; prob02
for example.
cd ..cd prob02
Use the clang++
command to compile your code and the ./
command to run it. The sample code below shows how you would compile code save in main.cpp
and into the executable file main
. Make sure you use the correct filenames required in this problem. Take note that if you make any changes to your code, you will need to compile it first before you see changes when running it.
clang++ -std=c++17 main.cpp -o main./main
You can run one, two, or all the commands below to test
your code, stylecheck
your code’s design, or formatcheck
your work. Kindly make sure that you have compiled and executed your code before issuing any of the commands below to avoid errors.
make testmake stylecheckmake formatcheck
A faster way of running all these tests uses the all
parameter.
make all
RepeatedAddition
Use a loop to add a given number a n
number of times.
The program should ask the user to provide the number and the number of times it is added to compute the total. The repetitions should be a non-negative value.
Note: This problem can be solved by using multiplication. However, the goal is to practice using loops so you only get full points if you use them.
Please see the sample output below to guide the design of your program.
Sample Input/Output
Please enter a number: 5Number of times to be added: 10The sum is 50
Please enter a number: 3Number of times to be added: 30The sum is 90
Please enter a number: 5Number of times to be added: -3Repetitions can't be negative
Submission checklist
- Compiled and ran the driver (
main
). - Manually checked for compilation and logical errors.
- Ensured no errors on the unit test (
make test
). - Followed advice from the stylechecker (
make stylecheck
). - Followed advice from the formatchecker to improve code readbility (
make formatcheck
).
Code evaluation
Open the terminal and navigate to the folder that contains this exercise. Assuming you have pulled the code inside of /home/student/labex02-tuffy
and you are currently in /home/student
you can issue the following commands
cd labex02-tuffy
You also need to navigate into the problem you want to answer. To access the files needed to answer problem 1, for example, you need to issue the following command.
cd prob01
When you want to answer another problem, you need to go back up to the parent folder and navigate into the next problem. Assuming you are currently in prob01
, you can issue the following commands to go to the parent folder then go into another problem you want to answer; prob02
for example.
cd ..cd prob02
Use the clang++
command to compile your code and the ./
command to run it. The sample code below shows how you would compile code save in main.cpp
and into the executable file main
. Make sure you use the correct filenames required in this problem. Take note that if you make any changes to your code, you will need to compile it first before you see changes when running it.
clang++ -std=c++17 main.cpp -o main./main
You can run one, two, or all the commands below to test
your code, stylecheck
your code’s design, or formatcheck
your work. Kindly make sure that you have compiled and executed your code before issuing any of the commands below to avoid errors.
make testmake stylecheckmake formatcheck
A faster way of running all these tests uses the all
parameter.
make all
Power
Write a program that asks the user for a base number and a power, then use loops to raise the base number to that power.
Please see the sample output below to guide the design of your program.
Sample Output:
Please enter the base number: 5Please enter the power: 35 ^ 3 = 125
Please enter the base number: 3Please enter the power: -3Negative powers are currently unsupported.
Submission checklist
- Compiled and ran the driver (
main
). - Manually checked for compilation and logical errors.
- Ensured no errors on the unit test (
make test
). - Followed advice from the stylechecker (
make stylecheck
). - Followed advice from the formatchecker to improve code readbility (
make formatcheck
).
Code evaluation
Open the terminal and navigate to the folder that contains this exercise. Assuming you have pulled the code inside of /home/student/labex02-tuffy
and you are currently in /home/student
you can issue the following commands
cd labex02-tuffy
You also need to navigate into the problem you want to answer. To access the files needed to answer problem 1, for example, you need to issue the following command.
cd prob01
When you want to answer another problem, you need to go back up to the parent folder and navigate into the next problem. Assuming you are currently in prob01
, you can issue the following commands to go to the parent folder then go into another problem you want to answer; prob02
for example.
cd ..cd prob02
Use the clang++
command to compile your code and the ./
command to run it. The sample code below shows how you would compile code save in main.cpp
and into the executable file main
. Make sure you use the correct filenames required in this problem. Take note that if you make any changes to your code, you will need to compile it first before you see changes when running it.
clang++ -std=c++17 main.cpp -o main./main
You can run one, two, or all the commands below to test
your code, stylecheck
your code’s design, or formatcheck
your work. Kindly make sure that you have compiled and executed your code before issuing any of the commands below to avoid errors.
make testmake stylecheckmake formatcheck
A faster way of running all these tests uses the all
parameter.
make all
Reviews
There are no reviews yet.