COS1511 Assignment 2.pdf - COS1511 Assignment 02 1a#include...

Info icon This preview shows pages 1–6. Sign up to view the full content.

View Full Document Right Arrow Icon
COS1511 Assignment 02 1a. #include <iostream> #include <iomanip> using namespace std; int main() { // Fill in the code to define an integer variable called selection cout << "Please enter the choice of dink " << "(a number from 1 to 4 or 0 to quit) " << endl; cout << "Hot Dog Menu " << endl << endl; cout << "1: Plain hotdog " << endl; cout << "2: Chilli hotdog " << endl; cout << "3: Cheese hotdog " << endl; cout << "4: Russian hotdog " << endl; cout << "0: QUIT " << endl <<endl << endl; cin >> selection ; while(selection > 4 || selection < 0 && selection != 0) { cout << “Invalid choice – Please re-e nter “; cin >> beverage; } cout << “You have selected option number “ << selection; return 0; } 1b. { cout << "You have selected option number " << selection << endl; break; } while(selection = 0) { break; } cout << "How many hotdogs would you like?" << endl; cin >> number; float cost; switch(number) { case 1: cost=number*15.00;
Image of page 1

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
cout << "The total cost is R " << cost << endl; break; case 2: cost=number*12.50; cout << "The total cost is R " << cost << endl; break; case 3: cost=number*17.00; cout << "The total cost is R " << cost << endl; break; 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"; cout << "Try again please" << endl; } return 0; } 2a. int i = 1; while (i <=n) { cout << X ; i<5&&i!=2 i++
Image of page 2
} 2b. The output will be 19 because sum is not initialised to 1, it first adds 1 #include <iostream> using namespace std; int main() { int i = 2, sum = 0; //initialise sum to zero while (i <= 5) { sum +=i; i++; } cout << "The sum of 2 through 5 is " << sum << endl; } 3a. / This program will find the average number of hours a day that a student spent programming over a long weekend. #include <iostream> using namespace std; int main() { int numStudents; int numDays; float numHours, total, average; int student,day = 0; // these are the counters for the loops
Image of page 3

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
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 the number of days in the long weekend" << endl; cin >> numDays; cout << endl; for( student = 1; student <= numStudents; student++) { total = 0; for(day = 1; day <= numDays; day++) { cout << "Please enter the number of hours worked by student " << student <<" on day " << day << "." << endl; cin >> numHours; total = total + numHours; } average = total / numDays; cout << endl; cout << "The average number of hours per day spent programming by " << "student " << student << " is " << average << endl << endl << endl; } return 0; }
Image of page 4
3b. #include <iostream> using namespace std; int main() { int numStudents; int numDays, student, day; float programmingHours, biologyHours; // hours spent studying courses float programmingTotal, biologyTotal; // holds total hours spent on courses float programmingAvg, biologyAvg; // holds average hours spent on courses cout << "This program will find the average number of hours a day" << endl << "each given student spent on programming and on biology over a long weekend\n\n"; cout << "How many students are there ?" << endl << endl; cin >> numStudents; cout << "Enter the number of days in the long weekend" << endl; cin >> numDays; cout << endl;
Image of page 5

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 6
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern