You are on page 1of 13

Course Code: BIT150 Bachelor of ICT with Education (BICT)

Introduction to Programming with C++ Monday, April 20, 2020

BIT150 – INTRODUCTION TO PROGRAMMING WITH C++

ASSIGNMENT 1 – WRITTEN (PRACTICAL) ASSIGNMENT

SUBMISSION: Electronically through the College’s Learning Management System (LMS)


which can be accessed by following this link: https://learn.zictcollege.ac.zm/.

The format of the file to be submitted should be a Microsoft Office Word document (.docx)
and only one file can be uploaded for this Assignment.

Note:
a. No late submission will be accepted for this Assignment. 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.
b. No email submission will be accepted either. If you want to submit the assignment
electronically on the LMS and Moodle (LMS) is off-line during that time, you need
not contact us, because we will be aware of it. Simply submit it as soon as Moodle is
available again.

DUE DATE 23:59hrs on Thursday, 7 May 2020


Extension No Extension will be given
Tutorial Matter Prescribed book: Chapters 1 – 6
Assignment Number 1
Contribution Towards Overall Mark 20%
Reference Material:
1. C++ Programming: An Object-Oriented Approach, 1st ed., 2020.
2. Introduction to Programming with C++, 3rd ed., by D. Y. Liang, 2014.
3. Starting Out with C++ Early Objects, 9th ed., by Behrouz A. Forouzan, 2017.
4. C++ How to Program, 10th ed, by Paul & Harvey Deitel, 2017.
5. Problem Solving with C++, Global Edition, 10th ed., by Walter J. Savitch, 2018.
You can download books from the following web link: http://gen.lib.rus.ec/

Instructor: Mr. K. Chisamu Page 1 of 13


Course Code: BIT150 Bachelor of ICT with Education (BICT)
Introduction to Programming with C++ Monday, April 20, 2020

QUESTION 1: LOOPING

1. Suppose we want to validate the data captured for two variables programsDone and
result in a while loop. The loop should be repeated until the value of result is
greater than or equal to 50 and the value of programsDone is greater than or equal to 5.
The variables result and programsDone are both of type int. Complete the while
loop below. You only have to write down the completed while loop. [5 Marks]

2. Write a program that contains the following for loop. Discuss the output from the
program, by explaining what the purpose of the program is. Execute the program and
submit your program as well as the output. [5 Marks]

3. Include the for loop below in a small program and complete the program. The loop
should be executed 10 times. Do not change the for loop below. Compile and run your
program to see for yourself that it works. You do not have to submit this program and
output.

Instructor: Mr. K. Chisamu Page 2 of 13


Course Code: BIT150 Bachelor of ICT with Education (BICT)
Introduction to Programming with C++ Monday, April 20, 2020

4. Now convert the for loop into a while loop and add any variable initializations that you
think are necessary. Compile and run your program and submit only the program
containing the while loop and its output. [5 Marks]
5. The following code is supposed to display the positive even numbers less than 12. That
is, it will output the numbers 2, 4, 6, 8, and 10. However, there is a logical error in the
code. Explain what the output of the code below will be. Then write a small program
including the code below and make the necessary changes to fix the code so that it
displays what it is intended to display. Ensure that your program works correctly. Only
submit the program, not the output. [5 Marks]

6. The following incomplete program first asks the user to enter the number of items he/she
has eaten today and then to enter the number of calories for each item. It then calculates
the number of calories he/she has eaten for the day and displays the value.

Instructor: Mr. K. Chisamu Page 3 of 13


Course Code: BIT150 Bachelor of ICT with Education (BICT)
Introduction to Programming with C++ Monday, April 20, 2020

You have to complete the code. First complete the code by using a while loop to read in
the calories of all the items. Compile and run your program and submit only the code that
you added and the output. Then change you program to use a for loop to read in the
calories of all the items. Compile and run the program again and submit only the code
that you added and the output. Use the variables that have already been defined in the
given program. Test your program by entering 7 for the number of items and the
following values for the calories: 1 120 60 150 600 1200 300

If your logic is correct, the following will be displayed:


Total calories eaten today = 2431 [10 Marks]

QUESTION 2: NESTED LOOPS

1. Suppose a new member of the city council has to be chosen from three candidates and
suppose there are 4 voting stations. We need a C++ program that will count the votes for
every candidate and display the result. At every voting station the voters have to be asked

Instructor: Mr. K. Chisamu Page 4 of 13


Course Code: BIT150 Bachelor of ICT with Education (BICT)
Introduction to Programming with C++ Monday, April 20, 2020

one after the other for which candidate (indicated by A or B or C) he or she wants to
vote. X is entered when all the voters at the specific voting station have voted.
The program has the following structure:
a. The three totals and the number of spoilt votes are both initialised to 0.
b. Now a for loop follows, going from 1 to the number of voting stations.
c. Inside this loop is a while loop. A prompting message appears on the screen,
asking the voter for which candidate he or she wants to vote. The choice of the
voter is then input.
d. Inside the while loop is a switch statement to increment the correct total. The
default option is used to count the number of spoilt votes.
e. The while loop exits when X is entered for the choice.
f. When the for loop is exited, the three totals and the number of spoilt votes are
displayed.

Instructor: Mr. K. Chisamu Page 5 of 13


Course Code: BIT150 Bachelor of ICT with Education (BICT)
Introduction to Programming with C++ Monday, April 20, 2020

To help you, the following framework is given:

Run your program with the input given below and submit printouts of the
program and output. (The data is written on one line for each voting station, but
you will possibly enter the values on separate lines.) [10 Marks]
B B B A C X
A B C D Y B X
B A C C B X
A B X

Instructor: Mr. K. Chisamu Page 6 of 13


Course Code: BIT150 Bachelor of ICT with Education (BICT)
Introduction to Programming with C++ Monday, April 20, 2020

QUESTION 3: VOID FUNCTIONS

1. The main function of the incomplete program below initialises the variables name,
addr1, addr2 and postalCode and then calls function displayData to display the
name and address as follows:
Mr T.S. Indala
P.O. Box 50741
Sandton
2146

// Program

Your task is to write the void function displayData. Compile and run your program
and submit the program and the output. [5 Marks]

2. Change the program in question 1 above so that the user is asked to input values for the
variables name, addr1, addr2 and postalCode. Compile and run your program with
the following sets of input. Submit your program as well as the output. [5 Marks]

Instructor: Mr. K. Chisamu Page 7 of 13


Course Code: BIT150 Bachelor of ICT with Education (BICT)
Introduction to Programming with C++ Monday, April 20, 2020

QUESTION 4: FUNCTIONS WITH DIFFERENT RETURN TYPES AND ONE OR


MORE VALUE PARAMETERS

1. 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 following values:
65, 43, 78, 67 and 64
The output will be:
After dropping the lowest test score, the test average is 68.50
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.
It should use the following functions:
a. An int function getScore with no parameters, to ask the user for a test score,
validate it and return the score. The function should be called by the main
function once for each of the five scores to be entered. Do not accept test scores
lower than 0 or higher than 100.
b. An int function findLowest that is passed the five test scores and then find and
return the lowest of the five scores passed to it. It is called by function
calcAverage, to determine which of the five scores to drop.
c. A float function calcAverage that is passed the five test scores and then
calculates and returns the average of the four highest scores.
d. A void function displayOutput that is passed the average score; it then
displays the average of the test scores. Display two digits after the decimal point.

Instructor: Mr. K. Chisamu Page 8 of 13


Course Code: BIT150 Bachelor of ICT with Education (BICT)
Introduction to Programming with C++ Monday, April 20, 2020

We give you the main function below. You must submit the four functions you have
developed as well as output using the following data: [20 Marks]

QUESTION 5: FUNCTIONS WITH DIFFERENT RETURN TYPES & PARAMETERS

1. Write a program that will compute the volume of a room. User inputs are the height,
width and length of each 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:
a. one for input;
b. one to do the calculation; and
c. one for output.

The input function has to input the height, width and length into the variables
theHeight, theWidth and theLength. All three variables are of type int. As the

Instructor: Mr. K. Chisamu Page 9 of 13


Course Code: BIT150 Bachelor of ICT with Education (BICT)
Introduction to Programming with C++ Monday, April 20, 2020

values of theHeight, theWidth and theLength will be changed in this function,


reference parameters need to be used.

The calculation function will receive three parameters that represent the height,
width and length, do the calculation and return the volume of the room. As the
parameters are not changed in the function, they should be value parameters. The
function should return an int value which represents the volume.

The output function has to display the height, width and length entered by the user
as well as the volume. You also have 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 includes a for loop that allows the user to repeat the calculation of
the volume for new input values five times. We give the main function below. You must
submit the three functions you have developed as well as output for repeating the loop
five times with the following input data: [20 Marks]

Instructor: Mr. K. Chisamu Page 10 of 13


Course Code: BIT150 Bachelor of ICT with Education (BICT)
Introduction to Programming with C++ Monday, April 20, 2020

QUESTION 6: FUNCTION CALLS

1. Write down short answers only for this question. Look at the following program and then answer the
questions that follow. Study the function header carefully before answering the question. [6 Marks]

a. Is the function call in line 27 a valid function call? Give reasons for your answer.
b. Is the function call in line 30 a valid function call? Give reasons for your answer.
c. Is the function call in line 31 a valid function call? Give reasons for your answer.
d. Consider the following variable definitions and function call: [8 Marks]

Consider the two function headers below. For each header, say if you think the header for function
playGame is a valid header, and give reasons for your answer:

Instructor: Mr. K. Chisamu Page 11 of 13


Course Code: BIT150 Bachelor of ICT with Education (BICT)
Introduction to Programming with C++ Monday, April 20, 2020

QUESTION 7: VARIABLE DIAGRAMS

1. Draw a series of variable diagrams (table format) for the program below. In the table,
indicate using line numbers, the content of each variable from the first line of execution
to the very last

Instructor: Mr. K. Chisamu Page 12 of 13


Course Code: BIT150 Bachelor of ICT with Education (BICT)
Introduction to Programming with C++ Monday, April 20, 2020

Create a table format for your answers. Here is an example (it’s just an example so do
create your own style and format) and start with the first line of code to be executed.

END OF ASSIGNMENT ONE

Instructor: Mr. K. Chisamu Page 13 of 13

You might also like