Written by students who passed Immediately available after payment Read online or as PDF Wrong document? Swap it for free 4.6 TrustPilot
logo-home
Document preview thumbnail
Preview 3 out of 17 pages
Exam (elaborations)

INP1501 ASSIGNMENT 2 MEMO

Document preview thumbnail
Preview 3 out of 17 pages

INP1501 ASSINGMENT 2 MEMO

Content preview

INP1501 ASSIMENT 2 MEMO

1. question1.cpp:
//Vending machine menu
#include <iostream>

#include <iomanip> using

namespace std;


int main()
{
int beverage, number; //define an integer variable
const float COFFEE = 15.00, TEA = 12.50, HOT_CHOCOLATE = 17.00, CAPPUCCINO = 22.50; float cost;
cout << "Please enter the choice of drink "
<< "(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 > 4 || beverage < 0)
{
cout << "Invalid choice - Please re-enter "; cin
>> beverage;
}
cout << "You have selected option number " << beverage << endl;
cout << "How many cups would you like? "; cin >> number;
cout.setf(ios::fixed); cout.precision(2);
switch(beverage)
{
case 1: cost = COFFEE * number;
cout << "The total cost is R" << cost << endl; break;
case 2: cost = TEA * number;
cout << "The total cost is R" << cost << endl; break;
case 3: cost = HOT_CHOCOLATE * number; cout
<< "The total cost is R" << cost << endl; break;
case 4: cost = CAPPUCCINO * number; cout
<< "The total cost is R" << cost << endl; break;
case 0: cout << "Please come again" << endl;
break; default:
cout << "Invalid selection"
<< "Try again please" << endl;
}
return 0;
}

,2. Question 2:
a. question2a.cpp:
//Convert for loop to while loop
//Loop must execute 10 times
//Add any variable initializitions that are deemed necessary
#include <iostream> using

namespace std;



int main()
{ int i, n; n =
10; i = 1;
while (i <= n)
{
cout << i; //Displays the amount of times the loop executes if (i
< 5 && i != 2)
cout << 'X'; //Displays X for everytime i is less than 5 and also not equal to 2 cout
<< endl;
i++;
}

return 0;
}
b. Question 2b:

, b.i. Explain what the output of the code will be: next = 2 , which initiates loop - while (next
<=5) { next++; product = product * next;}
1st while loop (next = 2): count is initialized (next++) next = 2 + 1 = 3, product = 1 * 3 =
3
2nd while loop (next = 3): count is initialized (next++) next =3 + 1 = 4, product = 3 * 4 =
12
3rd while loop (next = 4): count is initialized (next++) next = 4 + 1 = 5, product = 12 * 5 = 60
4th while loop (next = 5): count is initialized (next++) next = 5 + 1 =6, product = 60 * 6 =
360
5th while loop (Next = 6): loop terminates because condition is not met

What is the logical error?:
Program is written in such a way that it’s calculating the product of numbers 3 through
6
instead of 2 through 5.

b.ii. Make necessary changes to to fix the code so that it displays what it is intended to display:
Option A:
If you wish to keep next that it initializes count before product is calculated then you need
to:
- change next before the loop such that next = 1
- change the condition of the loop to while (next < 5)

question2b.cpp:
//Calculate the product of numbers 2 through 5
#include <iostream> using

namespace std; int main()

{
int next = 1, product = 1; while
(next < 5)
{

Document information

Uploaded on
February 12, 2023
Number of pages
17
Written in
2022/2023
Type
Exam (elaborations)
Contains
Questions & answers
$10.26

Wrong document? Swap it for free Within 14 days of purchase and before downloading, you can choose a different document. You can simply spend the amount again.
Written by students who passed
Immediately available after payment
Read online or as PDF

Seller avatar
Reputation scores are based on the amount of documents a seller has sold for a fee and the reviews they have received for those documents. There are three levels: Bronze, Silver and Gold. The better the reputation, the more your can rely on the quality of the sellers work.
KRS2020
3.8
(4)
Sold
13
Followers
6
Items
14
Last sold
11 months ago



Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Working on your references?

Create accurate citations in APA, MLA and Harvard with our free citation generator.

Working on your references?

Frequently asked questions