C++:Problem9

Problem:
This program has the user input a number n and then finds the
mean of the first n positive integers. It uses a while loop
for data validation and a for loop to accumulate the total of the integers from 1 to n.

Display:
This program finds the average of the first n integers.
Please enter a positive integer: -2
Only positive integers are valid. Please re-enter: 6
The mean average of the first 6 positive integers is 3

This program finds the average of the first n integers.
Please enter a positive integer: 15
The mean average of the first 15 positive integers is 8

Code:
#include
using namespace std;
int main()
{
//declaration of variables
int n;
int total = 0;
int number;
float mean;

cout<< "This program finds the average of the first n integers.\n";
cout<< "Please enter a positive integer: ";
cin>> n;

//Using while loop and for loop statment
while (n <= 0)
{
//Prompts user to input
cout<< "Only positive integers are valid. Please re-enter: ";
cin>> n;
}

for (number = 1; number <= n; number++)
{
total = total + number;
}

mean = (total) / n;

cout << "The mean average of the first " << n
<< " positive integers is "<< mean << endl;

return 0;
}

C++:Problem8

Problem:
Write a program that displays a hot beverage menu and prompts the user to make a selection. A switch statement determines which item the user chosen. A do-while loop repeats until the user selects item E from the menu.

Objectives:
• How to use switch statements
• do-while loop
• while loop used for validation of user input
• for loop
• How to use type casting

Display:
***Hot Beverage Menu***


A: Coffee $1.00
B: Tea $ .75
C: Hot Chocolate $1.25
D: Cappuccino $2.50
E: Exit the program
Enter the beverage A,B,C, or D you desire
(or E to exit the program): a
How many cups would you like ? 3
Your Total cost is: $3.00


***Hot Beverage Menu***

A: Coffee $1.00
B: Tea $ .75
C: Hot Chocolate $1.25
D: Cappuccino $2.50
E: Exit the program
Enter the beverage A,B,C, or D you desire
(or E to exit the program): e
Please come again
Goodbye

Code:
#include
#include
using namespace std;

int main()
{
//declaration of variables
int numCups;
float cost;
char beverage;
bool validBeverage;

cout<< fixed << showpoint << setprecision(2);

//Using do while loop and switch statment
do
{
//Prompts the user to input
cout<< endl << endl;
cout<< "***Hot Beverage Menu***" << endl << endl;
cout<< "A: Coffee $1.00" << endl;
cout<< "B: Tea $ .75" << endl;
cout<< "C: Hot Chocolate $1.25" << endl;
cout<< "D: Cappuccino $2.50" << endl;
cout<< "E: Exit the program " << endl;

cout<< "Enter the beverage A,B,C, or D you desire" < << " (or E to exit the program): ";
cin>> beverage;


validBeverage = false;

switch(beverage)
{
case 'a':
case 'A': cost = 1.00;
validBeverage = true;
break;
case 'b':
case 'B': cost = .75;
validBeverage = true;
break;
case 'c':
case 'C': cost = 1.25;
validBeverage = true;
break;
case 'd':
case 'D':cost = 2.50;
validBeverage = true;
break;

case 'e':
case 'E': cout<< "Please come again"<< endl;
break;

default : cout<< "That's not on the menu."< }

//Using if statment to display the output
if (validBeverage)
{
cout<< "How many cups would you like ? ";
cin>> numCups;
cout<<"Your Total cost is: $"
<< cost*numCups < }
}while(beverage != 'E' && beverage != 'e');

//closing
cout << "\nEnd of Program\n\n";
return 0;
}

C++:Problem7

Problem:
Write a program that determines whether a meeting room is in violation of fire law regulations regarding the maximum room capacity. The program will read in the maximum room capacity and the number of people to attend the meeting. If the number of people is less than or equal to the maximum room capacity, the program announces that it is legal to hold the meeting and tells how many additional people may legally attend. If the number of people exceeds the maximum room capacity, the program announces that the meeting cannot be held as planned due to fire regulations and tells how many people must be excluded in order to meet the fire regulations. Write your program so that it allows the calculation to be repeated as often as the user wishes.

Objective:
• If-else statement
• Boolean expression
• Determine the logical data types for variables
• Using a do-while loop

Display:
Enter the room capacity :25
Enter number of people in the meeting :30
Warning! you can't hold the meeting. But if you still want to hold
the meeting you have to exclude: 5 guest(s)
Do you want to run the program again? y/n Y


Enter the room capacity :25
Enter number of people in the meeting :25
You can hold the meeting legelly!
Do you want to run the program again? y/n y


Enter the room capacity :55
Enter number of people in the meeting :25
You can hold the meeting legelly!
Do you want to run the program again? y/n n



End of Program

Code:
#include
using namespace std;

int main ()
{
//declaration of variables
int number;
int roomcapacity;
char choice ;

//Using do while loop and if-else statment
do
{
cout << "Enter the room capacity :";
cin >> roomcapacity;
cout << "Enter number of people in the meeting :";
cin >> number;


if(number <= roomcapacity)
cout << "You can hold the meeting legelly!";
else if (number > roomcapacity)
{
cout <<"Warning! you can't hold the meeting. But if you still want to hold" < <<"the meeting you have to exclude: " << (number - roomcapacity) << " guest(s)";
}

cout << endl << "Do you want to run the program again? y/n ";
cin >> choice;
cout << endl << endl;
}
while ( choice =='y' || choice == 'Y');

//closing
cout << "\nEnd of Program\n\n";
return 0;
}

C++:Problem6

Problem:
Write a program that computes the cost of a long-distance call. The cost of the call is determined according to the following rate schedule:

a. Any call started between 8:00 A.M. and 6:00 P.M., Monday through Friday, is billed at a rate of $0.40 per minute.
b. Any call starting before 8:00 A.M. and after 6:00 P.M., Monday through Friday, is charged at a rate of $0.25 per minute.
c. Any call starting on a Saturday or Sunday is charged at a rate of $0.15 per minute.

The input will consist of the day of the week, the time started, and the length of the call in minutes. The out will be the cost of the call. The time is to be inputted can be in military time as 1300.

Software Design
A. Problem Write a program that computes the cost of a long-distance call. The cost of the call is determined according to the following rate schedule:

d. Any call started between 8:00 A.M. and 6:00 P.M., Monday through Friday, is billed at a rate of $0.40 per minute.
e. Any call starting before 8:00 A.M. and after 6:00 P.M., Monday through Friday, is charged at a rate of $0.25 per minute.
f. Any call starting on a Saturday or Sunday is charged at a rate of $0.15 per minute.

The input will consist of the day of the week, the time started, and the length of the call in minutes. The out will be the cost of the call. The time is to be inputted can be in military time as 1300.
B. Analyze
Given:
- Any call started between 8:00 A.M. and 6:00 P.M., Monday through Friday, is billed at a rate of $0.40 per minute.
- Any call starting before 8:00 A.M. and after 6:00 P.M., Monday through Friday, is charged at a rate of $0.25 per minute.
- Any call starting on a Saturday or Sunday is charged at a rate of $0.15 per minute.
C. Data Types:
1. Input variables
a) int Minutes;
b) int TimeStarted;
c) char choice;
d) string Day;

2. Output variables
a) double cost;
3. Program variable
a) double rate:

D. Formula:
a) cost = Minutes * rate;
E. Algorithm:
1. Introduction
a. Ask the user to input the starting time of the call in military time. (If the user input a wrong time, the program will ask the user again to input the starting time).
b. Ask the user to input the length of the call in minutes.
c. Ask the user to input the day of the week he/she made the call by inputting the first two letters of any day.
d. Ask the user if he/ she wants to calculate their bill again.
3. Calculations:
a) The total cost of the call and the rate per minute.

Display:
Enter the start time of the call in military time
For example, 1:30 = 1330: -34

You entered a non valid Time.
Enter the start time of the call in military time
For example, 1:30 = 1330: 5000

You entered a non valid Time.
Enter the start time of the call in military time
For example, 1:30 = 1330: 1200
Enter the length of the call in minutes: 200
Enter the day of the week on which you called using only the first two letters of the day. (Monday = mo): su

your rate for the call was $0.15 a minute
Your total cost: $30.00

Would you like to calculate your bill again? (y/n): y


Enter the start time of the call in military time
For example, 1:30 = 1330: 850
Enter the length of the call in minutes: 10
Enter the day of the week on which you called using only the first two letters of the day. (Monday = mo): mo

your rate for the call was $0.40 a minute
Your total cost: $4.00


Would you like to calculate your bill again? (y/n): y


Enter the start time of the call in military time
For example, 1:30 = 1330: 2000
Enter the length of the call in minutes: 10
Enter the day of the week on which you called using only the first two letters of the day. (Monday = mo): TH

your rate for the call was $0.25 a minute
Your total cost: $2.50


Would you like to calculate your bill again? (y/n): n



End of Program

Code:
#include
#include
#include
using namespace std;


int main ()
{

//Input variables
int Minutes, TimeStarted;
char choice;
string Day;

//Output variables
double cost, rate;

//Format demical place
cout << fixed << showpoint << setprecision (2);

//Using do while loop to ask the user if he/she wants to calculate his/her bill again
do
{
//Prompt the user to input
cout << "Enter the start time of the call in military time " << endl
<< "For example, 1:30 = 1330: ";
cin >> TimeStarted;
//Using while loop that ask the user to enter the starting time again if the user
//failed in inputting the starting time in the right format.
while(TimeStarted < 0 || TimeStarted >= 2400)
{
cout << "\nYou entered a non valid Time.";
cout << "\nEnter the start time of the call in military time " << endl
<< "For example, 1:30 = 1330: ";
cin >> TimeStarted;
}
cout << "Enter the length of the call in minutes: ";
cin >> Minutes;
cout << "Enter the day of the week on which you called using " <<
"only the first two letters of the day. (Monday = mo): ";
cin >> Day;
//Using if statment to determine the right cost of the call on a certain day.
if(Day == "mo"|| Day == "Mo" || Day == "MO" || Day == "tu" || Day == "Tu" || Day =="TU"
|| Day =="we" || Day == "We" || Day =="WE" || Day =="th" || Day == "Th" || Day =="TH"
|| Day == "fr" || Day =="Fr" || Day == "FR")
{
//Using if statment to determine the right cost of the call on a certain time.
if (TimeStarted >= 800 && TimeStarted <= 1800)
rate = 0.4;
else
rate = 0.25;
//Calculation
cost = Minutes * rate;
cout << "\nyour rate for the call was " << "$" << rate << " a minute"<< endl
<< "Your total cost: " << "$" << cost << endl;
}
else if(Day =="sa" || Day =="Sa" || Day =="SA" || Day =="su" || Day =="Su" || Day =="SU")
{
rate = 0.15;
//Calculation
cost = Minutes * rate;
cout << "\nyour rate for the call was " << "$" << rate << " a minute"<< endl
<< "Your total cost: " << "$" << cost;
}
else
cout <<"You entered a non valid entry. Try again.";

cout << endl << "\nWould you like to calculate your bill again? (y/n): ";
cin >> choice;
cout << endl << endl;

}
while( choice =='Y' || choice == 'y');

//closing
cout << "\nEnd of Program\n\n";
return 0;
}

C++:Problem5

Problem:
Write a C++ program that prompts the user to input the pay rat an hour and the number of working hours each week. The program then outputs the income before and after taxes, the money spend on clothes and other accessories, the money spent on school supplies, the money spent on savings bonds and the money spent on additional bonds.

Software Design:
A. Problem: You have an exciting summer job for five weeks. You want to calculate your gross and net income; expenses, including clothes, accessories and supplies for the next school year. You also want to calculate the amount of money you spend on savings bonds and the amount your parents spend on “matching” savings bonds.

B. Analysis:

GIVEN:
A. Your job is for five weeks.
B. Total income tax rate is 14%.
C. You spend 10% of your net income to buy clothes and accessories for the next school year.
D. You spend 1% of your net income to buy clothes and accessories for the next school year.
E. You spend 25% of your remaining income to buy savings bonds.
F. Parents match every $1.00 you spend on savings bonds with $0.50 to buy additional savings bonds.
G. User enters the hourly pay rate and the total number of hours worked each week

FIND:
• Income before taxes
• Income after taxes
• Money spent on clothes and accessories
• Money spent on school supplies
• Money spent on savings bonds
• Money spent by your parents on additional savings bonds

C. Data types:
1. Input variables
• double hourly_pay_rate
• double hours_worked_per_week
2. Output variables
• double gross_income
• double net_income
• double budget_clothes_accessories
• double budget_school_supplies
• double budget_savings_bonds
• double parents_matching_savings_bonds
3. Program variables
• const int WEEKS_WORKED = 5
• const double TOTAL_TAX_RATE = 0.14
• const double CLOTHES_ACCESSORIES_PERCENT = 0.10
• const double SCHOOL_SUPPLIES_PERCENT = 0.01
• const double SAVINGS_BOND_PERCENT = 0.25
• const double PARENT_MATCHING_RATE = 0.50
• double total_hours_worked
• double remaining_income

D. Formulas
• All equations are simple calculations


E. Algorithm

1. Introduction
2. Ask user for hourly pay rate and average number of hours worked each week
3. Calculations
a. Gross income
b. Net income
c. Total money budgeted for clothes and accessories
d. Total money budgeted for school supplies
e. Remaining income
f. Total money budgeted for savings bonds
g. Total money matched by parents for savings bonds
4. Output results
5. Closing

F. Workspace:

If user inputs pay rate as $15.50 and number of hours worked each week as 20

total hours worked = 5 * 20 = 100

gross income = 100 * $15.50 = $1550.00

net income = (1 - .14) * $1550.00 = $1333.00

total money budgeted for clothes and accessories = .10 * $1333.00 = $133.30

total money budgeted for school supplies = .01 * $1333.00 = $13.33

remaining income = $1333.00 - $133.30 - $ 13.33 = $1186.37
total money budgeted for savings bonds = 0.25 * $1186.37 = $296.59

total money matched by parents for savings bonds = 0.50 * $296.59 = $148.30

Display:
Enter your hourly rate: 15.5
hours week 1: 40
hours week 2: 39
hours week 3: 38.5
hours week 4: 39.5
hours week 5: 37.5

Income before tax = $3014.75
Net income = $2592.68
Clothes and accessories money = $259.27
Supplies money = $25.93
Savings bonds money = $576.87
Additional saving bonds money = $288.00
End of Program

Code:
#include
#include
using namespace std;

int main ()
{

//Input variables
double rateperhour;
double hoursweek1, hoursweek2, hoursweek3, hoursweek4, hoursweek5;

//Output variables
double incomebeforetax, netincome, clothmoney, suppliesmoney, moneyonbonds, additionalbonds, remain;

//Format demical place
cout << fixed << showpoint << setprecision (2);

//Prompt the user for input
cout << "Enter your hourly rate: " << rateperhour;
cin >> rateperhour;
cout << "hours week 1: " << hoursweek1;
cin >> hoursweek1;
cout << "hours week 2: " << hoursweek2;
cin >> hoursweek2;
cout << "hours week 3: " << hoursweek3;
cin >> hoursweek3;
cout << "hours week 4: " << hoursweek4;
cin >> hoursweek4;
cout << "hours week 5: " << hoursweek5;
cin >> hoursweek5;

//Calculation
incomebeforetax = rateperhour * (hoursweek1 + hoursweek2 + hoursweek3 + hoursweek4+
hoursweek5) ;
netincome = incomebeforetax - incomebeforetax * 0.14;
clothmoney = netincome * 0.1;
suppliesmoney = netincome * 0.01;
remain = netincome - clothmoney - suppliesmoney;
moneyonbonds = remain * 0.25;
additionalbonds = static_cast(moneyonbonds) * .50;

//Display output
cout << endl << "Income before tax = $" << incomebeforetax << endl
<< "Net income = $" << netincome << endl << "Clothes and accessories money = $"
<< clothmoney << endl << "Supplies money = $"<< suppliesmoney << endl
<< "Savings bonds money = $" << moneyonbonds << endl
<< "Additional saving bonds money = $" << additionalbonds;


//closing
cout << "\nEnd of Program\n\n";
return 0;
}

C++:Problem4

Problem:
A milk carton can hold 3.78 liters of milk. Each morning, a dairy farm ships cartons of milk to a local grocery store. The cost of producing one liter of milk is $0.38 and the profit of each carton of milk is $0.27. Write a program that does the following:

a. Prompts the user to enter the total amount of milk produced in the morning.
b. Outputs the number of milk cartons needed to hold milk. You must have whole milk cartons. You can not sell a part of a milk carton.
c. Outputs the cost of producing milk.
d. Outputs the profit of producing milk.

Objective:
• Arithmetic calculation
• Formatting decimals
• proper indentation of statements
• proper usage of comment statements
• user-friendly input/out

Display:
Enter the total number of liters: 100
The total number of liters = 100
Amount of cartons = 26.46 Can't sell part of carton!!
Total cartons = 26
Total cost = $38.00

Total profit = $7.02
End of Program

Code:
#include
#include
using namespace std;

int main ()
{

//Input variables
double LitersMilkProduce;

//Output variables
double cartons, totalcost, totalprofit;

//Program variables
const double LITERCOST(0.38), CARTONPROFIT(0.27), LITERS_PER_CARTONS= (3.78);

//Prompt the user for input
cout << "Enter the total number of liters: ";
cin >> LitersMilkProduce;


//Calculation
cartons = LitersMilkProduce / LITERS_PER_CARTONS;
totalcost = LitersMilkProduce * LITERCOST;
totalprofit = static_cast(cartons) * CARTONPROFIT;

//Display output

cout << "The total number of liters = " << LitersMilkProduce << endl;
cout << fixed << showpoint<< setprecision(2) << "Amount of cartons = " << cartons << " Can't sell part of carton!!"
<< endl << "\t Total cartons = "<< static_cast(cartons) << endl
<< "Total cost = $"<< totalcost << endl << endl <<"Total profit = $" << totalprofit;


//closing
cout << "\nEnd of Program\n\n";
return 0;
}

C++:Problem3

Problem:
Write a C++ program that prompts the user to input the elapsed time for an event in hours, minutes, and seconds. The program then outputs the elapsed time in seconds.

Display:
Enter the hours: 2
Enter the minutes: 40
Enter the seconds: 30
The elapsed time in seconds is: 9630.
End of Program

Code:
#include
using namespace std;

int main ()
{
//declaration of variables
int seconds, minutes, hours, elapsedtime;

//Prompt the user for input
cout << "Enter the hours: ";
cin >> hours;
cout << "Enter the minutes: ";
cin >> minutes;
cout << "Enter the seconds: ";
cin >> seconds;

//Calculation
elapsedtime = (hours * 3600) + (minutes * 60) + seconds;

//Display output
cout << "The elapsed time in seconds is: " << elapsedtime << ".";

//closing
cout << "\nEnd of Program\n\n";
return 0;
}