Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

QUESTION 1

#include <iostream>
using namespace std;

int main() {
int a, b, input1, input2, output1;
float c, output2;
a = 4;
b = 9;
c = 3.0;

cout << "Enter your two inputs >> ";


cin >> input1 >> input2;

output1 = a * b / input1 + input2 % 7;


output2 = a + c/input1 - (b + input2) % 5;

cout << "The results are as follow:" << endl;


cout << "Output1: " << output1 << endl;
cout << "Output2: " << output2 << endl;

}
Figure Q1

a) Analyze the code presented in Figure Q1 and write the complete output that will
be displayed on the screen based on the following input from the user:
• 4 and 9

Additionally, show your calculations that led to the expected output.

(5 marks)

b) Re-analyze the code presented in Figure Q1 and determine the output that will
be displayed on the screen for the following user inputs:
• 6 and 1

Additionally, show your calculations that led to the expected output.

(5 marks)

1
QUESTION 2
a) Analyze the code in Figure Q2a, and reconstruct this code by replacing the
“if, else-if, else” (on lines 13-21) with the usage of a switch
construct. Ensure that the generated output is the same. Write the new code to
replace only the code on lines 13-21.

1 #include <iostream>
2 using namespace std;
3
4 int main() {
5 char choice;
6 cout << "Menu:\n";
7 cout << "N - New Game\n";
8 cout << "L - Load Game\n";
9 cout << "Q - Quit Game\n";
10 cout << "Enter your choice: ";
11 cin >> choice;
12
13 if (choice == 'N') {
14 cout << "Starting a new game..." << endl;
15 } else if (choice == 'L') {
16 cout << "Loading a saved game..." << endl;
17 } else if (choice == 'Q') {
18 cout << "Quitting the game..." << endl;
19 } else {
20 cout << "Invalid choice." << endl;
21 }
22
23 return 0;
24 }
Figure Q2a

b) Analyze the code presented in Figure Q2b and modify it by replacing the use
of "if-else if-else" statements with "switch" statements, without changing the
resulting output. Write the new code to replace only the code on lines 9-19.

(4 marks)

2
1 #include <iostream>
2 using namespace std;
3
4 int main() {
5 int temp;
6 cout << "Provide the temperature in Celsius >> ";
7 cin >> temp;
8
9 if (temp < 0)
10 cout << "It's freezing outside!" << endl;
11
12 else if (temp <= 10)
13 cout << "It's chilly outside" << endl;
14
15 else if (temp <= 20)
16 cout << "It's pleasant outside" << endl;
17
18 else
19 cout << "It's warm outside!" << endl;
20
21
22 return 0;
23 }
Figure Q2b
(6 marks)

3
QUESTION 3

1 #include <iostream>
2
3 using namespace std;
4
5 int main()
6 {
7 int n = 10, i = 1;
8
9 do {
10 cout << (i+2) << " ";
11 i++;
12 } while (i <= n);
13
14 cout << endl;
15
16 return 0;
17 }
Figure Q3

a) Analyze the code presented in Figure Q3 and write the expected output that will
be displayed on the screen.
(3 marks)

b) Modify the code in Figure Q3 by replacing the existing do … while loop with a for
loop, while ensuring that the resulting output remains unchanged. Only the code
on lines 7-12 should be updated.
(6 marks)

c) Modify the code that you wrote for part (b) so that the output only displays even
numbers. To accomplish this, incorporate the "continue" statement into your
code.

Also, write the output that is produced by the updated code.


(6 marks)

4
QUESTION 4

Student Mark Marks Grade


Student 1 82 80-100 A
Student 2 55 60-79 B
Student 3 70 50-59 C
Student 4 88 40-49 D
Student 5 44 < 40 F
Student 6 65
Student 7 76 Table Q4b
Student 8 92
Student 9 30
Student 10 71
Student 11 86
Student 12 59

Table Q4a

Referring to the data in Table Q4a above, design a C++ program that can accomplish
the following tasks:
• Prompt the user to enter the marks of each student one by one (without limiting
the number of students).
• To terminate the mark entry process, the user may input -1.
• Once the user has finished entering all the marks, the program will calculate
and display the average mark for all students. The program will also display the
corresponding grade, which can be found in Table Q4b.
• For this question, you do not need to use array variables.
Your program should generate output as shown below when the user inputs marks for
these 12 students.
Enter the marks for each student (or -1 to stop):
82
55
70
88
44
65
76
92
30
71
86
59
-1
The average mark for all students is: 68.1667
The corresponding grade is: B

(10 marks)

5
QUESTION 5

a) You are tasked with analyzing sales data for a small business. You have been
provided with an array of monthly sales figures (in dollars) for the past year. The
array contains 12 elements, each representing the sales for a different month.

Write a C++ program to analyze the provided sales data as follows:

• Initialize an array with the following monthly sales values for the past year:

[1200, 1500, 1800, 900, 2000, 1700, 2100, 2500, 1400, 1900, 1600, 2300]

• Using a loop to iterate over the values, calculate the following statistics and
then display them:

➢ Total annual sales.


➢ Average monthly sales.
➢ The month with the highest sales.
➢ The month with the lowest sales.

(5 marks)

b) In your C++ program, how would you declare and initialize a multi-dimensional
string array to store the following data (excluding the header)?

Lab Person In Equipment


Charged
Lab 1 John Microscope
Lab 2 Maria Incubator
Lab 3 David Glassware
Lab 4 Sarah Fume Hood
(5 marks)

c) Based on your answer in (b), how would you retrieve and show the following
values from the array:
• Lab4
• John
• Glassware

(5 marks)

You might also like