Garantie de satisfaction à 100% Disponible immédiatement après paiement En ligne et en PDF Tu n'es lié à rien
logo-home
COS1511- assignment 2 solutions with output 2023 12,95 €   Ajouter au panier

Autre

COS1511- assignment 2 solutions with output 2023

  • Cours
  • Établissement

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

Aperçu 3 sur 18  pages

  • 11 mai 2023
  • 18
  • 2022/2023
  • Autre
  • Inconnu
avatar-seller

Questions d'entraînement disponibles

Fiches 10 Fiches
€5,18 0 vendus

Quelques exemples de cette série de questions pratiques

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

Réponse: #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

Réponse: #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

Réponse: #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.

Réponse: #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%;

Réponse: #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; }

Les avantages d'acheter des résumés chez Stuvia:

Qualité garantie par les avis des clients

Qualité garantie par les avis des clients

Les clients de Stuvia ont évalués plus de 700 000 résumés. C'est comme ça que vous savez que vous achetez les meilleurs documents.

L’achat facile et rapide

L’achat facile et rapide

Vous pouvez payer rapidement avec iDeal, carte de crédit ou Stuvia-crédit pour les résumés. Il n'y a pas d'adhésion nécessaire.

Focus sur l’essentiel

Focus sur l’essentiel

Vos camarades écrivent eux-mêmes les notes d’étude, c’est pourquoi les documents sont toujours fiables et à jour. Cela garantit que vous arrivez rapidement au coeur du matériel.

Foire aux questions

Qu'est-ce que j'obtiens en achetant ce document ?

Vous obtenez un PDF, disponible immédiatement après votre achat. Le document acheté est accessible à tout moment, n'importe où et indéfiniment via votre profil.

Garantie de remboursement : comment ça marche ?

Our satisfaction guarantee ensures that you always find a study document that suits you well. You fill out a form, and our customer service team takes care of the rest.

Auprès de qui est-ce que j'achète ce résumé ?

Stuvia est une place de marché. Alors, vous n'achetez donc pas ce document chez nous, mais auprès du vendeur primenovice. Stuvia facilite les paiements au vendeur.

Est-ce que j'aurai un abonnement?

Non, vous n'achetez ce résumé que pour 12,95 €. Vous n'êtes lié à rien après votre achat.

Peut-on faire confiance à Stuvia ?

4.6 étoiles sur Google & Trustpilot (+1000 avis)

97255 résumés ont été vendus ces 30 derniers jours

Fondée en 2010, la référence pour acheter des résumés depuis déjà 14 ans

Commencez à vendre!
12,95 €
  • (0)
  Ajouter