100% satisfaction guarantee Immediately available after payment Both online and in PDF No strings attached
logo-home
COS1511- assignment 2 solutions with output 2023 R250,00   Add to cart

Other

COS1511- assignment 2 solutions with output 2023

 26 views  0 purchase

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

Preview 3 out of 18  pages

  • May 11, 2023
  • 18
  • 2022/2023
  • Other
  • Unknown
All documents for this subject (83)
avatar-seller
primenovice

Available practice questions

Flashcards 10 Flashcards
R100,00 0 sales

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; }

ASSIGNMENT 02 COS1511 2023
QUESTION 1a.
while(age>30||finalMark<65)

{

cout<<”Candidate not suitable.”<<endl;

cout<<”Enter age: “;

cin>>age;

cout<<”Enter final mark: “;

cin>>finalMark;

}



QUESTION 1b.
CODE OUTPUT
a. for(int i=1;i<=1;i++) *
cout<<”*”;
cout<<endl;
b. for(int i=2;i>=2;i++) Endless loop of *
cout<<”*”;
cout<<endl;
c. for(int i=1;i<=1;i--) Endless loop of *
cout<<”*”;
cout<<endl;
d. for(int i=12;i>=9;1--) ****
cout<<”*”;
cout<<endl;
e. for(int i=0;i<=5;i++) ******
cout<<”*”;
cout<<endl;
f. for(int i=1;i<=5;i++) Syntax error.
cout<<”*”; “i” is inaccessible after the for loop.
i=i+1;
cout<<endl;


QUESTION 1c.

,PROGRAM.
#include <iostream>

using namespace std;

int main()

{

int i=0;

while(i<=10)

{

if (i<5 && i!=2)

cout<<"X";

i++;

}

return 0;

}

Output.




QUESTION 1d.

, PROGRAM.
#include <iostream>

using namespace std;

const int LIMIT =10;

int main()

{



float counter;

//The declaration of variables can be on the same line separated by a comma.

int number, zeros, odds, evens;

//The counter variables are inirialised to assure that they don’t acquire an unknown initial value.

zeros=0;

odds=0;

evens=0;



cout<<"Please enter "<<LIMIT<<" integers, positive,negative or zeros."<<endl;

cout<<"The number you entered are "<<endl;

for(counter=1;counter<=LIMIT; counter++)

{

cin>>number;



switch(number%2) //The switch statement is working on a remainder and (%) is used.

{

case 0:

evens++;

if (number==0) //correcting the boolean operator for number is equal to zero…(==) not (=)

zeros++;

break;

The benefits of buying summaries with Stuvia:

Guaranteed quality through customer reviews

Guaranteed quality through customer reviews

Stuvia customers have reviewed more than 700,000 summaries. This how you know that you are buying the best documents.

Quick and easy check-out

Quick and easy check-out

You can quickly pay through EFT, credit card or Stuvia-credit for the summaries. There is no membership needed.

Focus on what matters

Focus on what matters

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!

Frequently asked questions

What do I get when I buy this document?

You get a PDF, available immediately after your purchase. The purchased document is accessible anytime, anywhere and indefinitely through your profile.

Satisfaction guarantee: how does it work?

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.

Who am I buying this summary from?

Stuvia is a marketplace, so you are not buying this document from us, but from seller primenovice. Stuvia facilitates payment to the seller.

Will I be stuck with a subscription?

No, you only buy this summary for R250,00. You're not tied to anything after your purchase.

Can Stuvia be trusted?

4.6 stars on Google & Trustpilot (+1000 reviews)

72042 documents were sold in the last 30 days

Founded in 2010, the go-to place to buy summaries for 14 years now

Start selling
R250,00
  • (0)
  Buy now