100% satisfaction guarantee Immediately available after payment Both online and in PDF No strings attached 4.2 TrustPilot
logo-home
Exam (elaborations)

INP1501 ASSIGNMENT 2 MEMO

Rating
-
Sold
-
Pages
17
Grade
A+
Uploaded on
12-02-2023
Written in
2022/2023

INP1501 ASSINGMENT 2 MEMO

Institution
Course










Whoops! We can’t load your doc right now. Try again or contact support.

Written for

Institution
Course

Document information

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

Subjects

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)
{

Get to know the seller

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 UNISA
Follow You need to be logged in order to follow users or courses
Sold
13
Member since
4 year
Number of followers
6
Documents
14
Last sold
2 months ago
KRS Tutorials

Ebooks

3.8

4 reviews

5
1
4
1
3
2
2
0
1
0

Recently viewed by you

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

Frequently asked questions