100% tevredenheidsgarantie Direct beschikbaar na betaling Zowel online als in PDF Je zit nergens aan vast
logo-home
COS1511- assignment 2 solutions with output 2023 $14.05   In winkelwagen

Overig

COS1511- assignment 2 solutions with output 2023

  • Vak
  • Instelling

This document contains the solutions to assignment 2 for COS With all the codes and comments in them, functions and outputs to every question

Voorbeeld 3 van de 18  pagina's

  • 11 mei 2023
  • 18
  • 2022/2023
  • Overig
  • Onbekend
avatar-seller

Beschikbare oefenvragen

Oefenvragen 10 Oefenvragen
$5.62 0 verkocht

Enkele voorbeelden uit deze set oefenvragen

1.

Write a function named integerPower() that accepts two integer numbers (base and exponent) as formal parameters and returns the value of base exponent. For example, integerPower(3,4) = 3 * 3 * 3 * 3 = 81

Antwoord: #include <iostream> using namespace std; int integerPower(int base, int expo) { int answer=1; for(int i=1;i<=expo;i++) answer*=base; cout<<endl<<endl; cout<<base<<\" base , exponent \"<<expo; cout<<\"ANSWER is: \"<<answer<<endl; return answer; } int main() { int mainBase,mainExpo; cout<<\"Enter the base and exponent: \"<<endl; cin>>mainBase>>mainExpo; integerPower(mainBase,mainExpo); return 0; }

2.

Change the program so that the pay amount is displayed in the main program instead of function calculatePay. Submit a printout of the program and the output

Antwoord: #include <iostream> using namespace std; void printHeading() { for(int i=1;i<=52;i++)//using a for loop to output a number of (*)s cout<<\"*\"; cout<<endl; cout<<\" GOLDEN SALES COMPANY\"<<endl; cout<<\"This program inputs the number of items sold by a \"<<endl; cout<<\" Salesperson and prints the amount of pay due\"<<endl; for(int n=1;n<=52;n++)//using a for loop to output a number of (*)s cout<<\"*\"; cout<<endl; } float calculatePay(float x)//a function to return a calculation as a float { x*=12.5; cout.setf(ios::fixed); cout.precision(2); cout<<endl; return x; } int main() { int items; printHeading(); cout<<\"please input the number of items sold: \"<<endl; cin>>items; cout<<\"The amount pay due is R\"<<calculatePay(items)<<endl; return 0; }

3.

In this program, you have to make use of the switch statement. The average life expectancy (in hours) of a lightbulb based on the bulb’s wattage is listed in the table below

Antwoord: #include <iostream> using namespace std; int main() { int watts, life; cout<<\"Enter the watts of a bulb: \"; cin>>watts; switch(watts) { case 25: life=25000; break; case 40: case 60: life=1000; break; case 75: case 100: life=750; break; } if(watts==25||watts==40||watts==60||watts==75||watts==100) cout<<\"The life expectancy of a light bulb with \"<<watts<<\" watts is \"<<life<<\" hours.\"<<endl; else cout<<\"the life expectancy in hours is not defined in the range for the watts entered.\"<<endl; return 0; }

4.

Four experiments are performed, each consisting of five test results. The results for each experiment are given in the following list. Write a program using a nested loop to compute and display the average of the test results for each experiment. Display the average with a precision of two digits after the decimal point.

Antwoord: #include <iostream> using namespace std; int main() { float result, total, average; for (int e=1;e<=4;e++) { total=0; average=0; cout<<\"EXPERIMENT NO.\"<<e<<endl; for (int i=1;i<=5;i++) { cout<<\"ENTER THE RESULTS: \"; cin>>result; total+=result; } average=total/5.0; cout<<\"The average for experiment no.\"<<e<<\" is \"<<average<<endl<<endl; } return 0; }

5.

The cost of renting a romm at a hotel is R900 per night. For special occasions, such as a wedding or conference, the hotel offers a special discount as follows: • if the number of rooms booked is at least 10, the discount is 10%; • if the number of rooms booked is at least 20, the discount is 20%; • if the number of rooms booked is greater or equal 30, the discount is 30%;

Antwoord: #include <iostream> using namespace std; int main () { float discount, price, rooms, days, total, salesTax, discountTotal, taxedTotal; cout<<\"Please enter the following: \"<<endl; cout<<\" cost per room: \"; cin>>price; cout<<\" Sales tax per room: \"; cin>>salesTax; cout<<\" the number of rooms: \"; cin>>rooms; cout<<\" number of days: \"; cin>>days; cout<<endl<<endl; cout<<\"The total cost for one room is R\"<<price<<endl; if (10<=rooms && rooms<20) //testing the number of rooms booked and assigning a value to discount. discount=10; else if (20<=rooms && rooms<30) discount=20; else discount=30; if (days>=3) //testing the days and incriminating discount with five if days are 3 or greater. discount+=5; cout<<\"The discount per room is \"<<discount<<\"%\"<<endl; cout<<\"The number of rooms booked: \"<<rooms<<endl; //calculating the total price of the booking. total=price*rooms*days; //subtracting the discount from the total accordingly. discountTotal=total-total*(discount/100); cout<<\"The total cost of the rooms over \"<<days<<\" days is R\"<<discountTotal<<endl; cout<<\"The sales tax paid is: \"<<salesTax<<\"%\"<<endl; //adding sales tax to the total discounted price. taxedTotal=discountTotal+discountTotal*(salesTax/100); cout<<\"The total cost per booking is R\"<<taxedTotal<<endl; return 0; }

Voordelen van het kopen van samenvattingen bij Stuvia op een rij:

Verzekerd van kwaliteit door reviews

Verzekerd van kwaliteit door reviews

Stuvia-klanten hebben meer dan 700.000 samenvattingen beoordeeld. Zo weet je zeker dat je de beste documenten koopt!

Snel en makkelijk kopen

Snel en makkelijk kopen

Je betaalt supersnel en eenmalig met iDeal, creditcard of Stuvia-tegoed voor de samenvatting. Zonder lidmaatschap.

Focus op de essentie

Focus op de essentie

Samenvattingen worden geschreven voor en door anderen. Daarom zijn de samenvattingen altijd betrouwbaar en actueel. Zo kom je snel tot de kern!

Veelgestelde vragen

Wat krijg ik als ik dit document koop?

Je krijgt een PDF, die direct beschikbaar is na je aankoop. Het gekochte document is altijd, overal en oneindig toegankelijk via je profiel.

Tevredenheidsgarantie: hoe werkt dat?

Onze tevredenheidsgarantie zorgt ervoor dat je altijd een studiedocument vindt dat goed bij je past. Je vult een formulier in en onze klantenservice regelt de rest.

Van wie koop ik deze samenvatting?

Stuvia is een marktplaats, je koop dit document dus niet van ons, maar van verkoper primenovice. Stuvia faciliteert de betaling aan de verkoper.

Zit ik meteen vast aan een abonnement?

Nee, je koopt alleen deze samenvatting voor $14.05. Je zit daarna nergens aan vast.

Is Stuvia te vertrouwen?

4,6 sterren op Google & Trustpilot (+1000 reviews)

Afgelopen 30 dagen zijn er 85244 samenvattingen verkocht

Opgericht in 2010, al 14 jaar dé plek om samenvattingen te kopen

Start met verkopen

Laatst bekeken door jou


$14.05
  • (0)
  Kopen