Assignment 2 C0S1511 introduction to programming
Assignment 2 C0S1511 introduction to programming
COS1511
Introduction to programming
Bsc in computing
UNISA
Mr TP GOVENDER
Question 1
Quastion 1a
//input and validate of the age of students that qualify for an internship, also their final
mark obtained for the examination.
#include<iostream>
using namespace std;
int main()
{
// reveal variables.
int age;
double finalmark;
Question 1c.
//including the for loop.
#include <iostream>
using namespace std;
int main()
{
Question 2
Question 2a
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
//declare variables
double cost_per_room, sales_tax, discount, total_cost_per_room,
total_cost_of_rooms, total_cost_per_booking;
int num_rooms, num_days;
Question 5
Question 5a.
#include <iostream>
using namespace std;
int integerPower(int base, int exponent) {
int result = 1;
for (int i = 0; i < exponent; i++) {
result *= base;
}
return result;
}
int main() {
int base, exponent;
cout << "Enter the base number: ";
cin >> base;
cout << "Enter the exponent: ";
cin >> exponent;
int power = integerPower(base, exponent);
cout << base << " raised to the power of " << exponent << " is: "
<< power << endl;
return 0;
}
Question 5b.
#include <iostream>
using namespace std;
bool isEqual(char char1, char char2) {
if (tolower(char1) == tolower(char2)) {
return true;
}
else {
return false;
}
}
int main() {
char char1, char2;
cout << "Enter the first character: ";
cin >> char1;
cout << "Enter the second character: ";
cin >> char2;
bool result = isEqual(char1, char2);
if (result) {
cout<<char1<<" and "<<char2 << " The characters are equal."
<< endl;
}
else {
cout<<char1<<" and "<<char2 << " The characters are not equal."
<< endl;
}
return 0;
}
Question 5c.
#include <iostream>
using namespace std;
bool isEqual(char char1, char char2) {
if (tolower(char1) == tolower(char2)) {
return true;
}
else {
return false;
}
}
int main() {
char char1, char2;
cout << "Enter the first character: ";
cin >> char1;
cout << "Enter the second character: ";
cin >> char2;
bool result = isEqual(char1, char2);
if (result) {
cout<<char1<<" and "<<char2 << " The characters are equal."
<< endl;
}
else {
cout<<char1<<" and "<<char2 << " The characters are not equal."
<< endl;
}
return 0;
}
Question 6
#include <iostream>
using namespace std;
// the judges score
void getJudgeData(double &score)
{
do {
cout << "Enter a judge's score (0-10): ";
cin >> score;
} while (score < 0 || score > 10);
}
double findLowest(double score1, double score2, double score3, double
score4, double score5)
{
double lowest = score1;
if (score2 < lowest) lowest = score2;
if (score3 < lowest) lowest = score3;
if (score4 < lowest) lowest = score4;
if (score5 < lowest) lowest = score5;
return lowest;
}
double findHighest(double score1, double score2, double score3, double
score4, double score5)
{
double highest = score1;
if (score2 > highest) highest = score2;
if (score3 > highest) highest = score3;
if (score4 > highest) highest = score4;
if (score5 > highest) highest = score5;
return highest;
}
double calcScore(double score1, double score2, double score3, double score4,
double score5)
{
double lowest = findLowest(score1, score2, score3, score4, score5);
double highest = findHighest(score1, score2, score3, score4, score5);
return(score1+ score2+ score3+ score4 + score5 - lowest -highest)/3.0;
}
void displayOutput(double finalScore)
{
cout.precision(2);
cout.setf(ios::fixed);
cout << "The performer's final score is " << finalScore << endl;
}
int main()
{
double score1, score2, score3, score4, score5;
double finalScore;
// scores from judges
getJudgeData(score1);
getJudgeData(score2);
getJudgeData(score3);
getJudgeData(score4);
getJudgeData(score5);
// final score
finalScore = calcScore(score1, score2, score3, score4, score5);
// output
displayOutput(finalScore);
return 0;
}