Other
COS1511- assignment 2 solutions with output 2023
- Course
- Institution
This document contains the solutions to assignment 2 for COS With all the codes and comments in them, functions and outputs to every question
[Show more]Preview 3 out of 18 pages
Some examples from this set of practice questions
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
Answer: #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
Answer: #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
Answer: #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.
Answer: #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%;
Answer: #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; }
Stuvia customers have reviewed more than 700,000 summaries. This how you know that you are buying the best documents.
You can quickly pay through credit card or Stuvia-credit for the summaries. There is no membership needed.
Your fellow students write the study notes themselves, which is why the documents are always reliable and up-to-date. This ensures you quickly get to the core!
You get a PDF, available immediately after your purchase. The purchased document is accessible anytime, anywhere and indefinitely through your profile.
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.
Stuvia is a marketplace, so you are not buying this document from us, but from seller primenovice. Stuvia facilitates payment to the seller.
No, you only buy these notes for $14.95. You're not tied to anything after your purchase.
4.6 stars on Google & Trustpilot (+1000 reviews)
77254 documents were sold in the last 30 days
Founded in 2010, the go-to place to buy study notes for 14 years now