You are on page 1of 8

COS1512/104/1/2014

Assignment 1

Tutorial letter 104/1/2014


Introduction to Programming II

COS1512
School of Computing

Assignment 1

Bar code
COS1512/104/1
Assignment 1

UNIQUE NUMBER: 897640

DUE DATE: 24 February 2014

TUTORIAL MATTER:
 Chapters 4 to 6 and 9 of the Study Guide
 Chapter 4 (section 4.6) of Savitch
 Chapter 5 (section 5.5) of Savitch
 Chapter 6 and 9 (excluding the optional parts of section 9.2) of Savitch

WEIGHT: 20%

EXTENSION: NONE

Answer all the questions. You are required to submit your code electronically via myUnisa
so that we can compile and test it. You must create a zipped file containing only your source
code files. This should not include any executable files, compiled object files or any other
files. The zipped file should not contain any subdirectories within it. After successfully
creating the working programs, you have to create a .zip file consisting of the problems; i.e.
the *.cpp files and/or *.h files. A working program is one which compiles correctly, and
gives the correct output for any valid input. Do not submit programs that do not compile.
Please do not wait until the last day of the due date before submitting your assignment!
Beware that the next assignment is increasingly difficult.

2
COS1512/104/1
Assignment 1

Question 1

Write a program named dentist.cpp that outputs a dentist bill. For members of a dental plan, the
bill consists of the service charge (for the particular procedure performed) and test fees, input to the
program by the user. To non- members the charges consist of the above services plus medicine (also
input by the user). The program first asks if the patient is a member of the dental plan. If the patient
has a dental plan, the program must ask for the service and test charges. If the patient does not have
a dental plan, the program must ask for the service and test charges as well as the medicine cost. The
program uses two overloaded functions to calculate the total bill. Both functions return the total
charge.

Below are two sample runs of the output to be produced by the program dentist.cpp.

Sample Run 1:
Please input a one if you are a member of the dental plan
Input any other number if you are not 1
Please input the service charge 7.89
Please input the test charges 89.56

The total bill is R97.45

Sample Run 2:
Please input a one if you are a member of the dental plan
Input any other number if you are not 2
Please input the service charge 75.84
Please input the test charges 49.78
Please input the medicine charges 40.22

The total bill is R165.84

Question 2

Write a program named netball.cpp that will input the number of wins and losses that a netball
team acquired during a complete season. The wins should be input in a function named getWins
which return the number of wins to the main function. A similar function named getLosses should
do the same thing for the losses. A third function named findPercentWins calculates the
percentage of wins. It receives the wins and losses as parameters and returns the percentage
(double) to the main program which then displays the result. Use the assert function to verify that
the input entered are legitimate, that is between 0 and 80. The percentage should be displayed as a
percentage with two decimal places.

3
COS1512/104/1
Assignment 1

Sample Run:

Please input the number of wins


80
Please input the number of losses
40
The percentage of wins is 66.67%

Question 3

A parking garage charges R2.00 minimum fee to park a car for up to three hours. The garage charges
an additional 50 cents per hour for each hour or part thereof in excess of three hours. The maximum
charge for any given 24-hour period is R10.00. Assume that no car parks for longer than 24 hours at
a time. Write a program named charges.cpp that will calculate and print the parking charges for
each of three customers who parked their cars in this garage yesterday. You should enter the hours
parked for each customer. Your program should print the results and should calculate and print the
total of yesterday’s receipts. The program should use the function calculateCharges to determine
the charge of each customer.

Sample Run:
Please input the name of the customer, followed by the hours parked:
Customer1
1.5
Do you want to enter more information? (Y/N) Y

Please input the name of the customer, followed by the hours parked:
Customer2
4.0
Do you want to enter more information? (Y/N) Y

Please input the name of the customer, followed by the hours parked:
Customer3
24.0
Do you want to enter more information? (Y/N) N

Your output should appear in the following format:

Car Hours Charge


Customer1 1.5 2.00
Customer2 4.0 2.50
Customer3 24.0 10.00

TOTAL 26.50 14.50

4
COS1512/104/1
Assignment 1

Question 4

Write a program named average.cpp that takes its input from a file of numbers of type double and
outputs the average of the numbers in the file to the screen. The file named numbers.dat contains
nothing but the numbers of type double separated by blanks and/or line breaks and can be created
with any editor.

The contents of the file numbers.dat

31.6
50.84
71.76
84.5
19.55
61.4

Sample Run:

The average of the following numbers


31.6
50.84
71.76
84.5
19.55
61.4
Is

Question 5

Write a C++ program named bmi.cpp to determine the body mass index (BMI) of a person. The
BMI of a person measure the human body shape based on an individual's mass and height. BMI is
calculated as

weight/(height * height)

The main program will call a function named createFile(file) where file is declared as
ofstream file; to read the name, length and weight of a person from the keyboard and store this
information (name, length and width) in a text file named bmi_in.dat.

Sample Run:

Enter the following information:


Jack 1.6
Weight: 50

Enter a y if you would like to input more data

5
COS1512/104/1
Assignment 1

Enter the following information:


Name: Themba
Length: 1.76
Weight: 84

Enter a y if you would like to input more data


y

Enter the following information:


Name: Nomsa
Length: 1.55
Weight: 61

Enter a y if you would like to input more data


n

The contents of the file bmi.txt

Jack 1.6 50
Themba 1.76 84
Nomsa 1.55 61

Add another function named calculateBMI(in, out); that reads the file in (bmi_in.dat);
calculates each person’s BMI and write it for file out (bmi_out.dat).

Sample Run:
Name Length Weight BMI
Jack 1.6 50
Themba 1.76 84
Nomsa 1.55 61

Question 6

Computers are playing an increasing role in education. Write a program that will help a primary school
student learn multiplication. The program should accept two integer values and then type a question
such as:
How much is 6 times 7?

The student then types the answer. Your program checks the student’s answer. If it is correct, print
”Very good!” and then ask another multiplication question. If the answer is wrong, print ”No.
Please try again.” And then let the student try the same question repeatedly until the student
finally gets it right.
Sample Run 1:
Enter 2 integer values

6
COS1512/104/1
Assignment 1

6
7
How much is 6 times 7?
36
No. Please try again.
42
Very good!

Sample Run 2:
Enter 2 integer values
8
5
How much is 8 times 5?
40
Very good!

Question 7

The strcmp(string1,string2) function compares string1 to string2. It is a value returning


function that returns a negative integer if string1 < string2, 0 if string1 == string2, and a
positive integer if string1 > string2. Write a program that reads two names (last name first
followed by a comma followed by the first name) and then prints them in alphabetical order. The two
names should be stored in separate character arrays holding a maximum of 25 characters each. Use
the strcmp() function to make the comparison of the two names. Remember that 'a' < 'b',
'b' < 'c', etc. Be sure to include the proper header file to use strcmp().
+
Sample Run 1:
Please input the first name
Brown, George
Please input the second name
Adams, Sally
The names are as follows:
Adams, Sally
Brown, George

Sample Run 2:
Please input the first name
Brown, George
Please input the second name
Brown, George
The names are as follows:
Brown, George

7
COS1512/104/1
Assignment 1

Brown, George
The names are the same

Question 8

Write a program that determines how many consonants are in an entered string of 50 characters or
less. Consonants are all the non-vowel (A, E, I, O, U and sometimes Y) sounds. In the word hat, H and
T are consonants. Output the entered string and the number of consonants in the string.

Sample Run 1:
Please enter a string not more than 50 characters:
Super cali fragi listic expi ali docious
Number of consonants: xx

UNISA
2014

You might also like