Assignment 3 Solutions
Assignment 3 Solutions
Introduction to Programming I
COS1511
Year:2022
School of Computing
Dear Student
Your programs DO NOT HAVE TO BE IDENTICAL TO OURS, as long as you have followed the guidelines given in the
questions, and got the same output for the given input data. You should have used good programming techniques.
that names of variables and the names of functions should start with lowercase characters and further
words in the names with uppercase characters,
that the names of void functions should be verbs (or contain a verb) and
that the names of value-returning functions should be nouns or adjectives.
Also
use constants where needed
avoid the use of global variables
the correct indenting is extremely important
remember to include comments
Below the mark allocation.
Question 1 20
Question 2 40
Question 3 10
2
COS1511/105/2022
Question 1 20
BMI or Body Mass Index is a method of estimating a person's body fat levels based on a person's
weight and height measurement. The BMI is calculated by dividing the person’s body mass/weight (in
kilograms) by the square of the body height (in metres). The result is universally expressed in units of
kg/m2.
BMI formula: BMI = weight /(height)2
Write a program that calculates a person’s BMI and displays the weight status according to the BMI value,
indicating whether the person is underweight, healthy, overweight or obese. The BMI weight statuses
and related values are listed in the above table.
3
COS1511/105/2022
Answer:
#include<iostream>
//______________MAIN _FUNCTION______________
int main()
{
double weight, height, BMI;
displayFitnessResults(BMI);
4
COS1511/105/2022
return 0;
}
5
COS1511/105/2022
Question 2 40
Suppose we want to compile a student academic report for a high school learner in grade 12 (matric).
A matric learner is enrolled for 6 study units (subjects), namely: English, Mathematics, Life Orientation,
History, Computer literacy, Geography. The learner has to pass at least four subjects, including English,
to complete grade 12. The subject pass mark is 50%.
Write a program that prompts the learner to key in their marks for each subject. The program should
include the following functions:
a. A function studentDetails, that prompts the learner to key in their personal details
nameSurname, and schoolName. (3)
b. A function getMarks, that prompts the learner to key in a mark for each of the six subjects, and
validate the marks. Do not accept marks lower than 0 or higher than 100. (3)
d. A function minMax, to find and return the lowest and the highest of the 6 subject marks
passed to it as the subject with the lowest mark; (6)
e. A function passOrFail, to determine whether the student has passed or failed grade 12. (9)
Use the table below to determine the student symbol and code attained.
CODE SYMBOL MARK
7 A 80 - 100%
6 B 70 - 79%
5 C 60 - 69%
4 D 50 - 59%
3 E 40 - 49%
2 F 30 - 39%
1 FF 0 - 29%
6
COS1511/105/2022
Sample run:
Please key in your name:
John Africa
Please key in the name of your school:
Kings College
Key in your mark for English:
50
Key in your mark for Mathematics:
76
Key in your mark for Life Orientation:
40
Key in your mark for History:
62
Key in your mark for Computer literacy:
56
Key in your mark for Geography/Art:
38
***************************************************
STUDENT ACADEMIC RECORD
This program inputs the learner marks of matric level
subjects and prints the student final report.
***************************************************
Name: John Africa School: Kings College
7
COS1511/105/2022
Outcome: Passed
Answer:
#include<iostream>
//a. A function studentDetails, that prompts the learner to key in their personal details
name, Surname, and schoolName. (3)
//b. A function getMarks, that prompts the learner to key in a mark for each of the six
subjects, and validate the marks. Do not accept marks lower than 0 or higher than 100. (3)
void getMarks(float &getEng, float &getMaths, float &getLifeOr, float &getHistory, float
&compLit, float &getGeo)
{
cout << "Enter your mark for English : "<<endl;
cin >>getEng;
while ( (getEng <0) || (getEng >100) )
{
cout << "invalid mark! Please Enter a mark for English between 0 and 100 : "<<endl;
cin >>getEng;
}
8
COS1511/105/2022
//c. A function averageYearMark, to calculate the average of the 6 Subjects. This function
should be called just once by main, and should be passed the 6 Subject marks. (6)
float calcAverageYearMark(float cEng, float cMaths, float cLifeOr, float cHistory, float
compL, float geo)
{
float total, average =0.0;
// number of subjects
int count =6;
/* calculate the average as the combined total for all the subjects divided by the number
of subjects*/
average = total/count;
//d. A function minMax, to find and return the lowest and highest of the 6 subject marks passed
to it. (6)
void minMax(float cEng, float cMaths, float cLifeOr, float cHistory, float compL, float geo,
double &cMini, double &cMaxi)
{
9
COS1511/105/2022
double calcArr[size];
calcArr[0]=cEng;
calcArr[1]=cMaths;
calcArr[2]=cLifeOr;
calcArr[3]=cHistory;
calcArr[4]=compL;
calcArr[5]=geo;
mini= calcArr[0];
maxi = calcArr[0];
mini = calcArr[i];
maxi = calcArr[i];
}
cMini = mini;
cMaxi = maxi;
return;
}
//e. A function passFail, to determine whether the student has passed or failed grade 12.
The subject pass mark is 50%. Remember that a learner has to pass at least four subjects,
including English, to pass grade 12.
10
COS1511/105/2022
if (marks >=75)
{
distGranted = true;
}
else
{
distGranted = false;
}
return distGranted;
}
//g. A function codeSymbol, to convert each mark to a corresponding grading symbol (A, B, C,
D, E, F) and a code (7,6,5,4,3,2,1). For example, a student with 80% as a mark will have obtained
a grade symbol of A and a code of 7. The symbol and code should be printed next to the mark in
the student report. The same should be calculated and displayed for the average mark. (10)
//h. Students may use a separate function or display in the main function (4)
void displayReport(string name, string surname, string school,float eng, float maths,
float lifeOr,float history,float compLit, float geo )
{
bool passOutcome;
double minimum, maximum, ave;
if (passOutcome)
{
outcome =" Passed";
if ( awardDistinction (ave) )
{
distinction= " with Distinction";
}
}
//Generate the relevant code and symbol according to the subject mark
codeSymbol( eng, engCode, engSymbol);
codeSymbol( maths, mathsCode, mathsSymbol);
codeSymbol( lifeOr, lifeOrCode, lifeOrSymbol);
codeSymbol( history, historyCode, historySymbol);
codeSymbol( compLit, compLitCode, compLitSymbol);
codeSymbol( geo, geoCode, geoSymbol);
cout<<"***************************************************"<<endl;
cout<<" STUDENT ACADEMIC RECORD "<< endl;
cout<<" This program inputs the learner marks of matric level "<<endl;
cout<<" subjects and prints the student final report. "<<endl;
cout<<"***************************************************"<<endl;
cout<<" Name: "<<name <<" "<<surname<<" ";
12
COS1511/105/2022
//______________MAIN _FUNCTION______________
int main()
{
The following could be an alternate solution using arrays. (The above solution may not be the only way
to do it).
In the solution below we use four parallel arrays:
subjects[6] marks[6]
subject[0]=>”English” marks[0]=>marks for English
subject[1]=>”Maths” marks[1]=>marks for Maths
subject[2]=>”Life Orientation” marks[2]=>marks for Life Orinetation
subject[3]=>”History” etc.
subject[4]=>”Coumputer Literacy”
subject[5]=>”Geography”
passCode[6] passCodeSymbol[6]
passcode[0]=>the code indicating the result for Eng passCodeSymbol[0]=>the symbol for the result of Eng
passcode[1]=>the code indicating the result for Maths passCodeSymbol[1]=>the symbol for the result of Maths
etc etc
13
COS1511/105/2022
Note the functions called from within the display(). Since the question did not specify that these functions
should be called from main() we have used this logic so as to avoid passing a lot of values to the display()
function from the main().
#include<iostream>
using namespace std;
}
}
mini= marks[0];
maxi = marks[0];
14
COS1511/105/2022
{
if(marks[i] < mini)
mini = marks[i];
cMaxi = maxi;
return;
}
{
bool passFail = false;
int numPass = 0;
if( marks[0] < 50)/ /if mark for English less than 50
{
passFail= false;
}
else //check if four subjects are passed,English already passed if control gets to this else
{
for(int i = 1; i < 6; i++)
{
if(marks[i] >= 50)
numPass++;
}
if(numPass >= 3)
passFail= true;
}
return passFail;
if (marks >=75)
{
distGranted = true;
}
else
{
distGranted = false;
}
return distGranted;
}
15
COS1511/105/2022
cout<<"***************************************************"<<endl;
cout<<" STUDENT ACADEMIC RECORD "<< endl;
cout<<" This program inputs the learner marks of matric level "<<endl;
16
COS1511/105/2022
codeSymbol( ave, aveCode, aveSymbol); // calling the function here to determine the code
and code symbol on average mark
subPass = passOrFail(marks);
if (subPass)
{
outcome =" Passed";
if ( awardDistinction (ave) ) //calling the function to determine distinction
{
distinction= " with Distinction";
}
}
cout<<"Outcome : the student has "<<outcome<<" "<< distinction<<endl;
return;
//______________MAIN _FUNCTION______________
int main()
{
float ave;
int passCode[NUMSUB]; //to store the pass code for each subject
string passCodeSymbol[NUMSUB]; //to store the pass code symbol for each subject
17
COS1511/105/2022
//Generate the relevant code and symbol according to the subject mark
//call the function six times, on each subject mark
for(int i = 0; i < NUMSUB; i++)
codeSymbol(marks[i], passCode[i], passCodeSymbol[i]);
Question 3 10
Draw variable diagrams for the following program with input values 2 (for variable first) and 3 (for
variable second).
Note : Refer to Lesson 23 of the Study Guide and go through the activities to figure out how the execution
flows in a program like this where the main() calls other functions. Follow the conventions specified in
the Study Guide when you draw the variable diagrams.
For instance:
• A ? shows an uninitialised value for a variable.
• The notation 25 5 means that execution jumps from line 25 to line 5.
• We use square brackets [ ] around the name of a variable to show that it is inaccessible while the
current function is being executed.
18
COS1511/105/2022
#include <iostream>
2 using namespace std;
3
4 void multiplyBy2(int firstP, int secondP)
5 {
6 firstP = firstP * 2;
7 secondP = secondP * 2;
8 }
9
10 void multiplyBy3(int & firstP, int & secondP)
11 {
12
13 firstP = firstP * 3;
14 secondP = secondP * 3;
15 }
16
17 int main()
18 {
19 int first, second;
20 cout << "Enter the first number: "<< endl;
21 cin >> first;
22 cout << "Enter the second number:" << endl;
23 cin >> second;
24 multiplyBy2(first, second);
25 multiplyBy3(first, second);
26 cout << "The first number is "<< first << " now." << endl;
27 cout << "The second number is " << second << " now." << endl;
28
29 return 0;
30 }
19
COS1511/105/2022
Answer:
11 lines
first second
Line 19 ? ?
first second
Line 21
2 ?
first second
Line 23
2 3
[first] [second] firstP secondP
Line 24->4
2 3 2 3
[first] [second] firstP secondP
Line 6
2 3 4 3
[first] [second] firstP secondP
Line 7
2 3 4 6
first second
Line 8->24
2 3
[first]|firstP [second]|secondP
Line 25>10
2 3
[first]|firstP [second]|secondP
Line 13
6 3
[first]|firstP [second]|secondP
Line 14
6 9
first second
Line 15->25
6 9
©
UNISA
2022
20