0% found this document useful (0 votes)
22 views3 pages

CMPG121 Week2 - Practical2

Uploaded by

Phenyo
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)
22 views3 pages

CMPG121 Week2 - Practical2

Uploaded by

Phenyo
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/ 3

CMPG 121 Structured Programming 1 of 3

Assignment: Practical 2
Week 2 - Control Structures
General requirements
I. Add your name, surname, and student number as a comment line at the top of each of your programs;
II. Use comments to explain each program;
III. Save both C++ projects in a folder called Prac2_Surname. Compress the Prac2_Surname folder,
ensuring both practical tasks are included. Upload the compressed (*.zip) folder to eFundi
Assignments. Save a screenshot of each program’s output, named Prac2-1_Output1.png (adjust
numbering as needed). Finally, paste the code of both tasks in the text area provided (include
appropriate headings). Submit the assignment.

Prac 2-1
Create a program that converts amounts between two currencies (USD to ZAR and ZAR to USD). The
program should allow the user to choose which conversion they want to perform and then enter the
amount to be converted. The program should output the converted amount with proper formatting. Name
your project Prac2-1_studentNumber where ‘studentNumber’ refers to your student number.

1. Display these conversion options to the user:


• USD to ZAR
• ZAR to USD
2. Use a switch statement to handle the user’s choice of conversion.
3. Ask the user to enter the amount to be converted (as a oating-point value).
4. Perform the chosen conversion and display the result:
• Conversion rates (23 July 2024):
• 1 USD = 18.37 ZAR
• 1 ZAR = 0.054 USD
5. Implement input validation to ensure that the user enters a valid number.
6. After displaying the result, ask the user if they want to perform another conversion.
7. Implement a loop so that if the user wants to perform another conversion, the program starts over
without having to run the program again.

Example run

Currency Conversion Tool


1. USD to ZAR
2. ZAR to USD
Choose an option: 1
Enter the amount to be converted: 110.54
The amount in ZAR is 2030.14
Would you like to perform another conversion? (y/n): n
Thank you for using the Currency Conversion Tool!

2024/07/24
fl
CMPG 121 Structured Programming 2 of 3
Prac 2-2
Create an enhanced number guessing game where the computer randomly picks a number between 1
and 50, and the player has to guess it. The player has 5 attempts to guess the number. After each guess,
the program should provide hints such as “cold”, “warm”, and “hot” based on how close the guess is to
the correct number. The program should also validate the input to ensure it’s a number between 1 and 50.
Name your project Prac2-2_studentNumber where ‘studentNumber’ refers to your student number.

1. The computer should pick a random number between 1 and 50 (inclusive).


2. The player has up to 5 attempts to guess the number.
3. After each guess, the program should provide feedback based on the distance from the correct
number:
• “Cold” if the guess is more than 10 away from the number.
• “Warm” if the guess is between 5 and 10 away.
• “Hot” if the guess is between 1 and 4 away.
• “On re” if the guess is the correct number.
4. Handle special cases for borderline guesses:
• Exactly 11 away: “Almost warm!”
• Exactly 5 away: “Almost hot!”
• Exactly 1 away: “Almost there!”
5. Implement input validation to ensure the player’s input is a number between 1 and 50. If the input is
invalid, prompt the user to enter a valid number.
6. If the player guesses the number correctly, congratulate them and display how many tries it took.
7. If the player uses all their attempts without guessing correctly, reveal the number and o er to play
again.
8. Implement a loop so that if the player wants to play again, the game starts over without having to run
the program again.

Example run

Welcome to the Number Guessing Game!


I'm thinking of a number between 1 and 50.
Can you guess what it is in 5 tries or less?

Enter your guess: 15


Cold!

Enter your guess: 25


Warm!

Enter your guess: 28


Warm!
Almost hot!

Enter your guess: 34


Hot!
Almost there!

Enter your guess: 35


On fire! Congratulations! You've guessed the number in 5 tries!

Would you like to play again? (y/n): n


Thank you for playing!

2024/07/24
fi
ff
CMPG 121 Structured Programming 3 of 3
Grading Criteria:

Mark
Item
allocation

De ne constants for the conversion rates /1

Display menu options and handles user choice using switch statement /1

Implement input validation and error handling /3


2-1
Accurate arithmetic operations and conversion formulas /2

Formatted output using appropriate manipulators /2

Loop implementation to allow multiple conversions without restarting the program /2

Implement random number generation /2

Proper use of nested-if-else structures for feedback /4


2-2
Implement input validation and error handling /1

Loop implementation to handle multiple guesses and reply /1

Overall code quality and readability /1

Comments included in programs (penalty 1 per program), le name according to the speci cation
(penalty 2 per program), correct le extension (project zipped) (penalty - not marked), only the
latest version submitted (penalty 1 per additional program submission)

SUB_TOTAL 20

TOTAL 10

2024/07/24
fi
fi
fi
fi

You might also like