Final SCSJ1013-201620171
Final SCSJ1013-201620171
Final SCSJ1013-201620171
SEMESTER I 2016/2017
Name
I/C No.
Year/Course
Section
Lecturer’s Name
Question 1 (9 Marks)
a) Declare and initialize an array of strings named students to store the following names:
Amir Ali, Rania, Syamim, Thomas Ang, and Aidura. (2 marks)
c) Write a function named changeName to change the students' names in the array by user
input. The function takes the array of strings as argument and does not return a value.
(4 marks)
1
Sem. I 2014/2015
Question 2 (4 Marks)
int c = a + b;
a = a+1;
b = b+1;
return c;
int f(int a)
return a*a;
Determine the values of x and y after the execution of each of the following function calls.
Note that, each question is independent. Assume the original values of the variables for each
question are x=2 and y=2.
Answers:
Answers
x y
(a). f(x, y);
(b). y = f(x, 2);
(c). y = f(x);
(d). y = f (x, f(2) );
2
Sem. I 2014/2015
A program will be developed to calculate the product of all the integer numbers from s to e,
where s and e are positive numbers specified by the user. You have to complete user defined
functions described in (a) and main function as described in (b).
(a). You have to write the code for three user defined functions named readPositive,
swap and multiply. Table 1 gives the prototypes of the functions as well as their
descriptions. Figure 1 shows some example runs of the program.
Table 1
Function prototypes Description
int readPositive(void); This function is used to obtain a positive integer
from the keyboard. If the user enters a non-positive
integer, he or she will be asked to enter another
integer number until it is positive. The return value
from this function is the positive integer.
(2 marks)
(2 marks)
int multiply(int, int); This function is used to multiply all the integer
numbers from the starting number (the first
parameter) to the ending number (the second
parameter). The starting number must be less than
or equal to the ending number, so that the
multiplication can be done ascendingly.
(3 marks)
Run 1
The starting number:
Enter an integer => 3
3
Sem. I 2014/2015
Run 2
The starting number:
Enter an integer => 5
Run 3
The starting number:
Enter an integer => 0
Enter an integer => -1
Enter an integer => -2
Enter an integer => -3
Enter an integer => 3
4
Sem. I 2014/2015
(b). Using the functions defined in (a), fill in the blank lines in the main function of the
program below with appropriate function calls according to the tasks stated. Figure 1
shows some example runs of the program.
int main()
{
int s; // The starting integer number.
int e; // The ending integer number.
int p; // The product of the integers from s to e.
5
Sem. I 2014/2015
_____________________________________________
return 0;
}
6
Sem. I 2014/2015
Question 4 (8 Marks)
Based on the output generated in Output column in Table 3, complete the blank space with
correct cmath predefined functions and additional C++ statement (if necessary). Table 2
shows list of predefined functions.
Table 2
Predefined functions:
abs(x) abs(x) log(x)
ceil(x) fabs(x) log10(x)
exp(x) floor(x) pow(x,y)
sqrt(x) sin(x) tan(x)
Table 3
Line C++ Statements Output
1 #include <iostream> -
2 #include <iomanip> -
3 #include <cmath> -
4 using namespace std; -
5 int main() { -
6 float num1 = -6.25; -
7 cout << showpoint << fixed; -
7
Sem. I 2014/2015
Given three functions named f, g and h, and three arrays, named p, q and r, in the program
segment given in Figure 2.
8
Sem. I 2014/2015
(a). Determine the output for each function call below: (6 marks)
Answers:
i. f(p,2);
ii. g(p,2);
iii. h(p,2);
(b). Using the same arrays from the program segment in Figure 2 and appropriate loops, write
the code segment to accomplish each of the following tasks:
i. print the sum of all the elements of array p. (5 marks)
ii. copy all the elements of p to q accordingly. (3 marks)
iii. find the total of elements for each column in array p and put the result into array r
accordingly. (4 marks)
Answers:
i.
ii.
iii.
9
Sem. I 2014/2015
The management of a shopping complex keeps track of its shop-lot tenants to assist them in
the collection of monthly rentals. Among records kept are: lot number, lot status, tenant’s
name, telephone number, lot size: length and width (in meters), and rental rate per square
meter. From these records the monthly rent will be calculated. (5 Marks)
(a). Create a structure declaration called Tenants that holds the information stated above.
lotNumber: an integer value.
lotStatus: a character (O-ordinary, P-prime).
tenantName: string.
phoneNum: string.
lotLength: a double value.
lotWidth: a double value.
rentalRate: a double value.
mthlyRent: a double value.
Answers:
10
Sem. I 2014/2015
(b). Assuming that it is a newly opened complex and only has 3 tenants; 2 prime and 1
ordinary. Write a C++ statement to create a variable tenant of Tenants type to hold
the information of these 3 tenants. Initialize the the first and second tenant with
the information given in Table 3. (3 marks)
Table 3
Tenant Lot Lot Tenant’s Telephone Lot Lot
number status name number length width
1st 2317 prime Shafie 0173323477 12 meters 15
tenant Afdal
2nd 1224 ordinary Linda 0113211212 5 meters 12
tenant Malek
Answers:
11
Sem. I 2014/2015
(c). The third tenant’s information came late and was added later. Write C++ statements to
read values of the last tenant from the keyboard. (3 marks)
(d). The monthly rental rate for an ordinary shop-lot is RM100 per square meter. However,
the rental rate for a prime lot is doubled that of an ordinary lot. Using a loop write C++
statements to identify the lot status and assign the monthly rent of each shoplot.
(5 marks)
12
Sem. I 2014/2015
(e). Using a loop write C++ statements to calculate the monthly rentals for all tenants based
on the shop-lot’s size. (3 marks)
13
Sem. I 2014/2015
A pet shop named OwnAPet wants to have a computerized system to calculate and print
payroll information for its 5 employees.
INSTRUCTIONS:
Write a C++ payroll program for the pet shop owner. Your program should be able to do the
following tasks:
– A 2D array which consists of four columns. The first column is used to store the list
of hours worked, second column for storing the list of rates of pay, third column is for
storing the list of regular pays, the fourth column for storing the list of overtime pays
and the fifth column is for storing the list of total pays.
(b) The program will read the data of the employees consisting of the employee IDs, hours
worked and rates of pay from the keyboard.
(c) The program will display the payroll information of the employees onto the screen.
(d) Besides function main(), the program needs to define four (4) other functions as
described in Table 4. You should use appropriate arguments (based on Table 4) for
each function.
(e) Figure 3 shows an example run of the program with the keyboard inputs whereas Table
5 shows the assessment criteria.
Write your full program in the blank space provided in page 18.
14
Sem. I 2014/2015
Function Description
displayLine() To display lines using 105 characters of ‘-’ onto the screen using
a loop.
getData() To get the data of the employees from the keyboard. For each
employee, the user needs to enter the ID, hours worked and the
rate of pay of the employee. The function should accept a 1D
array of empID and 2D array empRecord as its arguments.
calculatePayroll() To calculate the regular pays, overtime pays and total pays of
the employees. For each employee, the calculation is done as
follows:
i) Regular pay = hour x rate
15
Sem. I 2014/2015
Payroll Program
ID: 1
Hours worked:45
Rate of Pay (RM per hour):3
ID: 2
Hours worked:40
Rate of Pay (RM per hour):2
ID: 3
Hours worked:20
Rate of Pay (RM per hour):1
ID: 4
Hours worked:55
Rate of Pay (RM per hour):3
ID: 5
Hours worked:41
Rate of Pay (RM per hour):2
--------------------------------
Press any key to continue . . .
16
Sem. I 2014/2015
17
Sem. I 2014/2015
18