0% found this document useful (0 votes)
469 views5 pages

Assignment 2 COS1511 PDF

This document contains code for 4 questions that are part of Assignment 2 for the COS1511 course. Question 1 involves coding a menu for beverage options and calculating costs based on user input. Question 2 involves coding loops to print patterns. Question 3 involves coding nested loops to calculate averages of time spent on different subjects over multiple days for multiple students. Question 4 involves coding a function to print number, square, and cube in tabular format for numbers 1-10 using a for loop.

Uploaded by

Silencio
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
469 views5 pages

Assignment 2 COS1511 PDF

This document contains code for 4 questions that are part of Assignment 2 for the COS1511 course. Question 1 involves coding a menu for beverage options and calculating costs based on user input. Question 2 involves coding loops to print patterns. Question 3 involves coding nested loops to calculate averages of time spent on different subjects over multiple days for multiple students. Question 4 involves coding a function to print number, square, and cube in tabular format for numbers 1-10 using a for loop.

Uploaded by

Silencio
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Assignment 2 –COS1511

Question 1a,1b (question1.cpp)

#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
// Fill in the code to define an integer variable called beverage
int beverage;

cout << "Please enter the choice of dink "


<< "(a number from 1 to 4 or 0 to quit) " << endl;
cout << "Hot Beverage Menu" << endl << endl;
cout << "1: Coffee " << endl;
cout << "2: Tea " << endl;
cout << "3: Hot Chocolate " << endl;
cout << "4: Cappuccino " << endl;
cout << "0: QUIT " << endl <<endl << endl;
cin >> beverage ;

while (beverage<0 || beverage>4) //complete the condition


{
cout << "Invalid choice - Please re-enter ";
cin >> beverage;
}
cout << "You have selected option number " << beverage<< endl;

if (beverage !=0)
{
cout << "How many cups would you like?" << endl;
// Fill in the code to read in number
int number;
float cost;
number = 0;
cost = 0;
cin >> number;

// Fill in the code to begin a switch statement


// that is controlled by beverage
switch(beverage)
{
case 1: cost = number * 15.0;
cout << "The total cost is R " << cost << endl;
break;
// Fill in the code to give the case for tea
case 2: cost = number * 12.50;
cout << "The total cost is R " << cost << endl;
break;

// Fill in the code to give the case for hot chocolate


case 3: cost = number * 17.00;
cout << "The total cost is R " << cost << endl;
break;

// Fill in the code to give the case for cappuccino


case 4: cost = number * 22.50;
cout << "The total cost is R " << cost << endl;
break;
case 0: cout << " Please come again" << endl;
break;
default:
cout << "Invalid selection" << endl;
cout << " Try again please" << endl;
}
}
else
cout << "Thank you, come again" ;

return 0;
}

Question 2a (question2a.cpp)

#include <iostream>
using namespace std;

int main()
{
int n,i;
i= 1;
cout<< "enter a number: ";
cin>> n;

while (i <= n)
{
i++;
if (i < 5 && i !=2)
cout << 'X';
}
return 0;
}
Question 2b (question2b.cpp)

The problem is the variable next is incremented before and resulting in incorrect calculation giving a
answer of 360 instead of 120.

#include <iostream>
using namespace std;
int main()
{
int next = 2, product = 1;
while (next <= 5)
{
product = product * next;
next++;
}
cout << "The product of 2 through 5 is " << product << endl;

return 0;
}

Question 3 (question3.cpp)

// This program finds the average time spent programming by a student


// each day over a three day period.
#include <iostream>
using namespace std;
int main()
{
int numStudents;
float numHoursBio, totalBio,numHoursPro, totalPro, averageBio,averagePro;
int student,day = 0; // these are the counters for the loops
int dayAmount = 1;
cout << "This program will find the average number of hours a day"
<< " that a student spent programming over a long weekend\n\n";
cout << "How many students are there ?" << endl << endl;
cin >> numStudents;
cout << "Enter number of days in long weekend " << endl << endl;
cin >> dayAmount;

for( student = 1; student <= numStudents; student++)


{
totalBio = 0;
totalPro = 0;

//Biology
for(day = 1; day <= dayAmount; day++)
{
cout << "Please enter the number of hours student"
<< student <<" studied for biology on day " << day << "." << endl;
cin >> numHoursBio;
totalBio = totalBio + numHoursBio;
}
//Programming
for(day = 1; day <= dayAmount; day++)
{
cout << "Please enter the number of hours student"
<< student <<" studied for programming on day " << day << "." << endl;
cin >> numHoursPro;
totalPro = totalPro + numHoursPro;
}

averageBio = totalBio / dayAmount;


averagePro = totalPro / dayAmount;
cout << endl;
cout << "The average number of hours per day spent programming "
<< "by student " << student << " is " << averagePro << endl<<" and for biology is "<< averageBio<<
endl<< endl;
if (averageBio>averagePro)
cout<< "The student spends more time on biology"<<endl<<endl;
else
cout<< "The student spends more time on Programming"<<endl<<endl;
}
return 0;
}
Question 4

#include <iostream>
#include <cmath>
using namespace std;
void printTabs(int number)
{
int square,cube;
square = pow(number,2);
cube = pow(number,3);
cout<<number << '\t'<< square << '\t'<< cube <<endl;
}

int main()
{
int number;
number=1;
cout<< "Number" << '\t'<< "Square" << '\t'<< "Cube" <<endl;
for (number; number<=10,number++)
{
printTabs(number);
}

return 0;
}

You might also like