INP1501 - 102 - 0 - 2024 Assignment 2
INP1501 - 102 - 0 - 2024 Assignment 2
INP1501 - 102 - 0 - 2024 Assignment 2
Introduction to Programming I
INP1501
Year:2024
School of Computing
BARCODE
Open Rubric
INP1501/102/0/2024
Please note that we automatically give five days extension for this assignment. It will be to
your own advantage to check after a few days whether the assignment has been registered on
the system. If you have not completed the assignment by the extension date, submit whatever
you have completed – you will get marks for everything that you have done.
CONTRIBUTION WEIGHT TO
YEAR MARK
QUESTIONS Practical exercises
If myUnisa is off-line when you want to submit the assignment, you need not contact us,
because we will be aware of it. Simply submit it as soon as myUnisa is available again.
Question 1
Write function headers for the functions described below:
(i) The function check has two parameters. The first parameter should be an integer number and the
second parameter a floating point number. The function returns no value.
(ii) The function mult has two floating point numbers as parameters and returns the result of
multiplying them.
(iii) The function time inputs seconds, minutes and hours and returns them as parameters to its
calling function.
(iv) The function countChar returns the number of occurrences of a character in a string, both provided as
parameters.
Question 2
Find the error(s) in each of the following program segments and explain how the error(s) can be
corrected:
int result;
result = x +
y;
}
if (n == 0)
return 0;
else
n * computeProd(n – 1);
float a;
cout << a << endl;
}
int
a;
int
b;
int
c;
int result;
Question 3
Write the functions as described below. You need not submit programs that use these functions.
(i) Write a function that returns the cube of the integer passed to it. For example cube(2) will
return 8 and
cube(3) will return 27.
(ii) Write a void function that receives four int parameters: the first two by value and the last two by
reference. Name the formal parameters n1, n2, sum and diff. The function should calculate the sum of
the two parameters passed by value and then store the result in the first variable passed by reference. It
should calculate the difference between the two parameters passed by value and then store the result in
the second parameter passed by reference. When calculating the difference, subtract the larger value from
the smaller value. Name the function calcSumAndDiff.
(iii) Write a function void rectangle(int w, int h) to print an open rectangle of asterisks (*). The
parameters w and h are the width and the height of the rectangle, expressed in number of asterisks.
(iv) Write a function, computePrice that receives two parameters: the first one is a character variable
indicating the size of the pizza (S, M or L) and the second an integer variable indicating the number of
toppings on the pizza. It then computes the cost of the pizza and returns the cost as a floating point
number according to the rules:
Question 4
The post office needs a program that reads in postal address data and then displays the data in a
neat format.
Question 5
Write a program that calculates the average of a group of test scores, where the lowest score
in the group is dropped. For example: if the user enters the values 65, 43, 78, 67 and 64, the output
will be:
INP1501/102/0/2024
This average is calculated by dropping the lowest score which is 43 and dividing the sum of the
remaining four values 65, 78, 67 and 64, which is 274, by 4.
You must submit the four functions you have developed as well as output using the following 5 sets of
input data:
65 24 80 73 51
66 38 84 69 59
72 52 81 23 53
65 28 72 63 65
65 55 75 68 62
Question 3
Write a program that will compute the volume of a room. User inputs are the height, width and
length of a room. For example, if the user inputs the height as 3, the width as 4 and the length as 5
the volume will be 3 * 4 * 5 = 60. You have to declare three functions: one for input; one to do the
calculation; and one for output.
• The input function getData has to input the height, width and length into the variables theHeight,
theWidth and theLength.
• The calculation function, calculateVolume will receive three parameters that represent the height,
width and length, do the calculation and return the volume of the room.
• The output function displayOutput has to display the height, width and length entered by the user as
well as the volume.
• The program also has to indicate the size of the room as small, medium or large. If the volume is less
than 100, the size is small, between 100 and 500 the size is medium and greater than 500 the size is
large. For example:
• The main function should include a for loop that allows the user to repeat the calculation of the volume
for new input values five times.
5
INP1501/102/0/2024
You must submit the three functions and the main function you have developed, as well as output
produced by the following input data:
345
10 14 12
877
12 14 8
15 8 12
Question 7
Draw a series of variable diagrams for the program below. Use the conventions of the Study Guide.