Assignment 2
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;
//ask the user for input.
cout << "Enter age: " ;
cin >> age;
cout << "Enter final mark for exam: " ;
//ask for marks.
cin >> finalmark;//read mark from keyboard.
//start validation.
while(age >= 30 || finalmark <= 65)
{
cout << "Either the age is greater than 30 or finalmark is less 65.student does not
qualify" <<endl;
//ask the user for input again.
cout << "Please enter age again: " ;
cin >>age;
cout << "Please enter final again: " ;
//ask for marks.
cin>> finalmark;//read mark from keyboard.
}
//display results.
{
cout << "Conditions meet,student qualifies!!" <<endl;
cout << "Age is: " <<age<< endl;
cout << "finalmark is: " <<finalmark <<endl;
}
}
Question 1b
CODE OUTPUT DESCRIPTION
example for (int i = 0; i < 10; 0123456789 The loop runs 10
i++) cout << i; cout times, each
<< endl; iteration has an
increment of 1.
A. for (int i = 1; i <= 1; * The loop executes
i++) cout << “*”; once, increments
cout << endl; once.
B. for (int i = 2; i >= 2; Runs This is an infinity
i++) cout << “*”; infinitely loop, starting from 2
cout << endl; to 2 then increment
forever.
C. for (int i = 1; i <= 1; Runs This is an infinity
i--) cout << “*”; cout infinitely loop, it starts at 1
<< endl; then decrement
forever.
D. for (int = 12; i >= 9; Error, identifier “i” is Gives an error
i--) cout << “*”; cout not defined because we do not
<< endl; have i. If it were
there it would
execute 4 times,
starting at 12
decrementing to 9.
it runs as long as i
is greater than 9
E. for (int i = 0; i <= 5; ***** The loop runs 5
i++) cout << “*”; times
cout << endl;
F. for (int i = 0; i <= 5; Error, identifier “i” is Gives an error
i++) cout << “*”; i = i not because we do not
+ 1; have i. If there were
brackets and i=i+1
was
cout << endl; defined inside with
cout<<”*” , it would
have printed *****
Question 1c.
//including the for loop.
#include <iostream>
using namespace std;
int main()
{
// a for loop that execute 10 times.
// int n=9;
// for (int i =0; i <= n; i++)
// if (i < 5 && i !=2)
// cout << 'x' ;
//changing the for loop into a while loop
int n = 9;
int i = 0;
while (i <= n)
{
if (i < 5 && i != 2){
cout << 'x';
}
i++;
}
}
Question 1d.
#include <iostream>
using namespace std;
const int LIMIT = 10;
int main()
{
float counter;
int number;
//start count from 0
int zeros = 0;
int odds = 0;
int evens = 0;
cout <<"Please enter "<< LIMIT << " integers, " //undefined identifier it should be
LIMIT
<< "positive, negative, or zeros." << endl;
cout << "The numbers you entered are:" << endl;
for (counter = 1; counter <= LIMIT; counter++)
{
cin >> number; // wrong operator , should be >>
switch (number % 2) //check evens and odds
{
case 0:
evens++;
if (number == 0) // compare number to zero
zeros++;
break;
//break so that it does not continue to another case
case 1:
case -1:
odds++;
}
}
cout << endl;
cout << "There are " << evens << " evens, "
<< "which includes " << zeros << " zeros."
<< endl;
cout << "The number of odd numbers is: " << odds
<< endl;
return 0;
}
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;
cout << "Please enter the following:" << endl;
cout << "cost per room: ";
cin >> cost_per_room;
cout << "sales tax per room: ";
cin >> sales_tax;
cout << "the number of rooms: ";
cin >> num_rooms;
cout << "number of days: ";
cin >> num_days;
//calculate the discounts given number of rooms
if (num_rooms >= 30) {
discount = 0.3;
} else if (num_rooms >= 20) {
discount = 0.2;
} else if (num_rooms >= 10) {
discount = 0.1;
} else {
discount = 0.0;
}
//additional discount if number of days is greater than 3
if (num_days >= 3) {
discount += 0.05;
}
// major calcutations
total_cost_per_room = cost_per_room * (1 - discount);
total_cost_of_rooms = total_cost_per_room * num_rooms * num_days;
total_cost_per_booking = total_cost_of_rooms * (1 + sales_tax/100);
//The display on the screen
cout << fixed << setprecision(2);
cout << "The total cost for one room is R" << cost_per_room << endl;
cout << "The discount per room is " << discount*100 << "%\n";
cout << "The number of rooms booked: " << num_rooms << endl;
cout << "The total cost of the rooms are R: " << total_cost_of_rooms
<< endl;
cout << "The sales tax paid is: " << sales_tax << "%\n";
cout << "The total cost per booking is R" << total_cost_per_booking
<< endl;
return 0;
}
Question 2b
#include <iostream>
using namespace std;
int main()
{
//declare variables
float total=0, result, average;
int experiment = 0;
//loop through the table
for (experiment; experiment < 4; experiment++)
{
cout << "Enter experiment " << experiment + 1 << " results: "
<< endl;
for (int i = 0; i < 5; i++)
{
cout << i + 1 << ") ";
cin >> result;
total += result;
}
average = total/5;
cout.setf(ios::fixed);
cout.precision(2);
cout << "Average for experiment " << experiment + 1 << ": "
<< average << endl << endl;
}
}
Question 2c
#include <iostream>
using namespace std;
int main()
{
int watts, lifeExp;
cout<<"Enter wattage for the bulb: "<<endl;
cin>>watts;
switch (watts)
{
case 25:
lifeExp = 25000;
break;
case 40:
case 60:
lifeExp = 1000;
break;
case 75:
case 100:
lifeExp = 750;
break;
default:
lifeExp = 0;
break;
}
cout<<"A "<<watts<<" watt bulb has a life expectancy of "<<lifeExp
<<" hours"<<endl;
}
Question 3 and 4a mixed.
#include <iostream>
using namespace std;
const float PAYABLE = 12.50;
void printHeading(){
for(int i = 0;i<55;i++){
cout<<"*";
}
cout<<endl;
cout<<" GOLDEN SALES COMPANY"<<endl;
cout<<"This program inputs the number of items sold by a \nSalesperson and prints the
amount of pay due."<<endl;
for(int i = 0;i<55;i++){
cout<<"*";
}
cout<<endl;
}
void calculatePay(int itemsSold){
float totalPayment;
totalPayment = itemsSold*PAYABLE;
cout.setf(ios::fixed);
cout.precision(2);
cout<<"The amount pay due is R "<<totalPayment<<endl;
}
int main(){
int sold;
float payDue;
printHeading();
cout<<"Please input the number of items sold"<<endl;
cin>>sold;
calculatePay(sold);
return 0;
}
Question 4b.
#include <iostream>
using namespace std;
const float PAYABLE = 12.50;
void printHeading(){
for(int i = 0;i<55;i++){
cout<<"*";
}
cout<<endl;
cout<<" GOLDEN SALES COMPANY"<<endl;
cout<<"This program inputs the number of items sold by a \nSalesperson and prints the
amount of pay due."<<endl;
for(int i = 0;i<55;i++){
cout<<"*";
}
cout<<endl;
}
float calculatePay(int itemsSold){
float totalPayment;
totalPayment = itemsSold*PAYABLE;
cout.setf(ios::fixed);
cout.precision(2);
return totalPayment;
}
int main(){
int sold;
float payDue;
printHeading();
cout<<"Please input the number of items sold"<<endl;
cin>>sold;
payDue = calculatePay(sold);
cout<<"The amount pay due is R "<<payDue<<endl;
return 0;
}
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;
}