100% de satisfacción garantizada Inmediatamente disponible después del pago Tanto en línea como en PDF No estas atado a nada 4.2 TrustPilot
logo-home
Examen

INP1501 ASSIGNMENT 2 MEMO

Puntuación
-
Vendido
-
Páginas
17
Grado
A+
Subido en
12-02-2023
Escrito en
2022/2023

INP1501 ASSINGMENT 2 MEMO

Institución
Grado










Ups! No podemos cargar tu documento ahora. Inténtalo de nuevo o contacta con soporte.

Escuela, estudio y materia

Institución
Grado

Información del documento

Subido en
12 de febrero de 2023
Número de páginas
17
Escrito en
2022/2023
Tipo
Examen
Contiene
Preguntas y respuestas

Temas

Vista previa del contenido

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)
{
$9.80
Accede al documento completo:

100% de satisfacción garantizada
Inmediatamente disponible después del pago
Tanto en línea como en PDF
No estas atado a nada

Conoce al vendedor

Seller avatar
Los indicadores de reputación están sujetos a la cantidad de artículos vendidos por una tarifa y las reseñas que ha recibido por esos documentos. Hay tres niveles: Bronce, Plata y Oro. Cuanto mayor reputación, más podrás confiar en la calidad del trabajo del vendedor.
KRS2020 UNISA
Seguir Necesitas iniciar sesión para seguir a otros usuarios o asignaturas
Vendido
13
Miembro desde
4 año
Número de seguidores
6
Documentos
14
Última venta
2 meses hace
KRS Tutorials

Ebooks

3.8

4 reseñas

5
1
4
1
3
2
2
0
1
0

Recientemente visto por ti

Por qué los estudiantes eligen Stuvia

Creado por compañeros estudiantes, verificado por reseñas

Calidad en la que puedes confiar: escrito por estudiantes que aprobaron y evaluado por otros que han usado estos resúmenes.

¿No estás satisfecho? Elige otro documento

¡No te preocupes! Puedes elegir directamente otro documento que se ajuste mejor a lo que buscas.

Paga como quieras, empieza a estudiar al instante

Sin suscripción, sin compromisos. Paga como estés acostumbrado con tarjeta de crédito y descarga tu documento PDF inmediatamente.

Student with book image

“Comprado, descargado y aprobado. Así de fácil puede ser.”

Alisha Student

Preguntas frecuentes