Based on the images provided, here is the verbatim transcription of the content on the slides from Universiti Malaya.
Slide 1: Revision (Part B) - 2
You are helping a bookstore create a simple C++ program to calculate customer bills. The store offers a 10% discount for purchases over RM100. To keep the program modular and organized, the store wants to use subfunctions:
- A non-void function to calculate the discount amount
- A void function to display the final bill Task: Write a complete C++ program that does the following:
- Ask the user to enter:
- Customer name
- Total amount of purchase (before discount)
- Use a non-void function named calculateDiscount(double amount) that:
- Returns 10% of the amount if it's more than RM100
- Returns 0 otherwise
- Use a void function named displayBill(string name, double total, double discount) that:
- Displays the customer name
- Displays the original amount
- Displays the discount and final amount after discount
- All values are passed by value
Slide 2: Logic and Flowchart Task
Highlights:
- calculateDiscount() is a non-void function that returns a double value.
- displayBill() is a void function that takes all data by value and formats the output.
- This program clearly separates processing and output logic for better modularity. Draw the flowchart for the program.
- Start
- Input data
- Check condition (amount > 100)
- Calculate discount
- Calculate final amount
- Display output
- End
Slide 3: Code Implementation (Functions)
#include <iostream>
#include <iomanip> // for fixed and setprecision
using namespace std;
// Non-void function: calculates discount
double calculateDiscount(double amount) {
if (amount > 100)
return amount * 0.10; // 10% discount
else
return 0.0;
}
// Void function: displays the bill
void displayBill(string name, double total, double discount) {
double finalAmount = total - discount;
cout << "\nCustomer Name: " << name << endl;
cout << fixed << setprecision(2);
cout << "Original Amount: RM" << total << endl;
cout << "Discount: RM" << discount << endl;
cout << "Final Amount: RM" << finalAmount << endl;
}
Slide 4: Code Implementation (Main)
int main() {
string customerName;
double totalAmount;
// Input
cout << "Enter customer name: ";
getline(cin, customerName);
cout << "Enter total purchase amount: ";
cin >> totalAmount;
// Process
double discount = calculateDiscount(totalAmount);
// Output
displayBill(customerName, totalAmount, discount);
return 0;
}
Slide 5: Revision (Part B) - 3
You are developing a simple program for a mathematics learning system. The program calculates the factorial of a number entered by the user using recursion. To keep the program modular, the system uses two subfunctions:
- A non-void recursive function to calculate the factorial
- A void function to display the result Task:
- Write a complete C++ program that:
- Asks the user to enter a positive integer
- Uses a recursive function named calculateFactorial(int n) to:
- Return the factorial of the number
- Uses a function named displayResult(int n, int result) to:
- Display the number entered
- Display the factorial result Enter a positive integer: 10 Factorial of 10 = 1 x 2 x 3 x 4 x 5 x 6 x 7 x 8 x 9 x 10 = 3628800
Slide 6: Recursive Code Implementation
#include <iostream>
using namespace std;
// Subfunction 1: Recursive calculation
int calculateFactorial(int n) {
if (n == 0 || n == 1)
return 1;
return n * calculateFactorial(n - 1);
}
// Subfunction 2: Display function
void displayResult(int n, int result) {
cout << "Factorial of " << n << " = ";
for (int i = 1; i <= n; i++) {
cout << i;
if (i < n)
cout << " x ";
}
cout << " = " << result << endl;
}
int main() {
int num;
cout << "Enter a positive integer: ";
cin >> num;
int factorial = calculateFactorial(num);
displayResult(num, factorial);
return 0;
}
Slide 7: Revision (Part B) - Students' Test Scores
A university department maintains records of students' test scores across several subjects. Write a C++ program that processes the student scores according to the following requirements: Requirements:
- The program shall first prompt the user to enter:
- The number of students
- The number of subjects
- Based on these values, dynamically allocate memory to store the scores using a two-dimensional array implemented with pointers.
- The program shall then read the scores (in marks) for each student across all subjects.
- Important: All data must be stored and accessed strictly using pointer notation only.
- Subsequently, the program shall compute, for each student:
- The total score
- The average score
- Each student shall be classified based on their average score: | Average Score | Category | |---|---| | \ge 80 | Excellent | | 50 - 79 | Pass | | < 50 | Fail |
Slide 8: Example Output (Students)
Example Output: Student 1: 70 80 90 | Total = 240 | Average = 80 | Excellent Student 2: 40 50 60 | Total = 150 | Average = 50 | Pass
Generate these type of questions, use grill-me