100% Zufriedenheitsgarantie Sofort verfügbar nach Zahlung Sowohl online als auch als PDF Du bist an nichts gebunden
logo-home
COS1511- assignment 2 solutions with output 2023 12,89 €   In den Einkaufswagen

Andere

COS1511- assignment 2 solutions with output 2023

  • Kurs
  • Hochschule

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

vorschau 3 aus 18   Seiten

  • 11. mai 2023
  • 18
  • 2022/2023
  • Andere
  • Unbekannt
avatar-seller

Verfügbare Übungsfragen

Karteikarten 10 Karteikarten
€5,15 0 verkauft

Einige Beispiele aus dieser Reihe der Übungsfragen

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

Antwort: #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

Antwort: #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

Antwort: #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.

Antwort: #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%;

Antwort: #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; }

Alle Vorteile der Zusammenfassungen von Stuvia auf einen Blick:

Garantiert gute Qualität durch Reviews

Garantiert gute Qualität durch Reviews

Stuvia Verkäufer haben mehr als 700.000 Zusammenfassungen beurteilt. Deshalb weißt du dass du das beste Dokument kaufst.

Schnell und einfach kaufen

Schnell und einfach kaufen

Man bezahlt schnell und einfach mit iDeal, Kreditkarte oder Stuvia-Kredit für die Zusammenfassungen. Man braucht keine Mitgliedschaft.

Konzentration auf den Kern der Sache

Konzentration auf den Kern der Sache

Deine Mitstudenten schreiben die Zusammenfassungen. Deshalb enthalten die Zusammenfassungen immer aktuelle, zuverlässige und up-to-date Informationen. Damit kommst du schnell zum Kern der Sache.

Häufig gestellte Fragen

Was bekomme ich, wenn ich dieses Dokument kaufe?

Du erhältst eine PDF-Datei, die sofort nach dem Kauf verfügbar ist. Das gekaufte Dokument ist jederzeit, überall und unbegrenzt über dein Profil zugänglich.

Zufriedenheitsgarantie: Wie funktioniert das?

Unsere Zufriedenheitsgarantie sorgt dafür, dass du immer eine Lernunterlage findest, die zu dir passt. Du füllst ein Formular aus und unser Kundendienstteam kümmert sich um den Rest.

Wem kaufe ich diese Zusammenfassung ab?

Stuvia ist ein Marktplatz, du kaufst dieses Dokument also nicht von uns, sondern vom Verkäufer primenovice. Stuvia erleichtert die Zahlung an den Verkäufer.

Werde ich an ein Abonnement gebunden sein?

Nein, du kaufst diese Zusammenfassung nur für 12,89 €. Du bist nach deinem Kauf an nichts gebunden.

Kann man Stuvia trauen?

4.6 Sterne auf Google & Trustpilot (+1000 reviews)

45.681 Zusammenfassungen wurden in den letzten 30 Tagen verkauft

Gegründet 2010, seit 14 Jahren die erste Adresse für Zusammenfassungen

Starte mit dem Verkauf
12,89 €
  • (0)
  Kaufen