C++:Problem16

Problem:
A company manager wants to determine the gross pay for each of his employees in a particular division. Also, the total payroll

Ask the company manager how many employees in a division?
Ask what each employee’s ID is?
Ask how many hours each employee worked per week?
Ask the amount hourly pay each employee earns?

Find the gross pay for each employees.
Find the total payroll for the division


Display the output below

Objective:
multi-parallel arrays

Display:
How many employee in the division: 3

What is the ID of employee 1 :3333
What is the hours worked in a week :55
What is the employee's hourly earnings: $10

What is the ID of employee 2 :3343
What is the hours worked in a week :33
What is the employee's hourly earnings: $13

What is the ID of employee 3 :3232
What is the hours worked in a week :40
What is the employee's hourly earnings: $16

Division: Automobile Wheels
Number of employees: 3
Total payroll for division: $1619

Employee ID Gross Pay

3333 $550
3343 $429
3232 $640

Code:
#include
#include
using namespace std;


void asking(int ID[],int hour[], int rate[], int size);
//purpose: To store the id, hours, rate in array to pass them to the display function.
//precondition:size of the division and the id, hours and the rate for each employee.
//postcondition: none
void display(int ID[], int hour[], int rate[], int size);
//purpose: to find the sum and the pay gross.
//precondition: none.
//postcondition: none.

int main ()
{
int const Max = 20;
int ID[Max];
int hour[Max];
int rate[Max];
int number;

//decimal format
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);

cout << "How many employee in the division: ";
cin >> number;
cout << endl;
asking(ID,hour,rate,number);
display(ID,hour,rate,number);

return 0;
}

void asking(int ID[],int hour[], int rate[], int size)
{
int i;
for (i=0; i< size; i++)
{
cout << "What is the ID of employee "< cin >> ID[i];
cout << "What is the hours worked in a week :";
cin >> hour[i];
cout << "What is the employee's hourly earnings: $";
cin >> rate[i];
cout << endl;
}
}


void display(int ID[], int hour[], int rate[], int size)
{
int j;
int sum=0;
for (j=0; j sum = sum + hour[j] * rate[j];

cout << "Division: Automobile Wheels"< cout << "Number of employees: " << size << endl;
cout << "Total payroll for division: $"<< sum << endl << endl;
cout << "Employee ID"<< setw(15) << "Gross Pay" << endl << endl;

for (j=0; jcout << ID[j] << setw(16) << "$"<< hour[j] * rate[j] << endl;

}

C++:Problem15

int findLowest ();
//purpose: to find the minimum value.
//preconditon: none
//postcondtion: to return the minimum.

float calcAverage (int m);
//purpose: to calculate the average the numbers.
//preconditon: none
//postcondtion: return the average.

//intializing an Array
int scores[10];


int main()
{
int minimum;

getValues();
minimum = findLowest();
cout<
//decmial format
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);

cout<< "The average of your highest nine scores is "
< return 0;
}

void getValues()
{

//for-loop to get the scores of the ten exams
for(int test= 0; test<10;test++)
{
if((scores[test]>= 0) && (scores[test]<=100))
cout<< "Enter your score on test #"<< test+1<< " ";
cin>> scores[test];
}
}

C++:Problem14

Problem:
A coffee shop sells expresso coffee for $1.25 a cup, bagels for $1.50 and Danish pastries for $2.25. Write a cash register program to compute a customer's bill. Using functions (Therefore, you must have at least four functions)
• To obtain the quantity of each item ordered (Hint: use call-by-reference variables)
• Calculate the subtotal of the bill
• Calculate the total cost of the bill, including a 7 percent sales tax
• Display an itemized bill for example as follows:

Item Quantity Price

Coffee 3 $3.75
Bagels 2 $3.00
Danish 1 $2.25

Sub total $9.00
Sales Tax $ .63

Total $9.63

Objective:
• Using functions with call by value and call by reference
• Write comments for functions: purpose, precondition and postcondition
• No Global Variable (only when necessary)
• Using formatting comments
• Using good style for source program

Display:
Enter the amount of coffee: 1

Enter the amount of bagels: 1

Enter the amount of danish pastries: 1
Item Quantity Price

Coffee 1 $1.25
Bagels 1 $1.50
Danish 1 $2.25

Sub total $5.00
Sales Tax $0.35

Total $5.35


Would you like to try again?(y/n) n


End of program

Code:
//function declaration
void display(float cprc,float bprc,float dprc, int cqnt, int bqnt,int dqnt, float sbttl,float ttl);
//purpose:display the design of the bill
//precondition: none
//Postcondition: returns the display function

//function declaration
float coffee(int& quantity);
//purpose: To find price of coffee
//precondition: input quantity of cups
//postcondition: returns the price of coffee

//function declaration
float bagel(int& quantity);
//purpose: To find price of bagel
//precondition: input quantity of bagels
//postcondition: returns the price of bagels

//function declaration
float danish(int& quantity);
//purpose: To find price of danish pasries
//precondition: input quantity of danish pastries
//postcondition: returns the price of danish pastries

//function declaration
float cal_subTtl(float p1, float p2, float p3);
//purpose: To find price of coffee, bagels and danish pastries
//precondition: none
//postcondition: returns the price1, price2 and price3

//function declaration
float cal_Total(float st);
//purpose: To find total of subtotal and tax
//precondition: none
//postcondition: returns the total of the bill


#include
#include
#include
using namespace std;

int main()
{
//declaration of variables
float cprice, bprice, dprice, subtotal, total;
int cquant, bquant, dquant;
char choice;

//decimal format
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);

do
{
cout < cprice = coffee(cquant);
bprice = bagel(bquant);
dprice = danish(dquant);
subtotal = cal_subTtl(cprice, bprice, dprice);
total = cal_Total(subtotal);
display(cprice,bprice,dprice,cquant,bquant,dquant,subtotal,total);

cout <<"\n\nWould you like to try again?(y/n) ";
cin >> choice;
}
while(toupper(choice) =='y');

cout << "\n\nEnd of program"< return 0;
}

//function definition
void display(float cprc,float bprc,float dprc, int cqnt, int bqnt,int dqnt, float sbttl,float ttl)
{

cout<< "Item\t\t"<<"Quantity\t\t"<<"Price"< cout<< "Coffee"<< setw(8) << cqnt<< setw(11) <<"$"< cout<< "Bagels"<< setw(8) << bqnt<< setw(11) << "$"< cout<< "Danish"<< setw(8)<< dqnt<< setw(11) << "$"< cout<< "Sub total"<< setw(16) << "$" << sbttl< cout<< "Sales Tax"<< setw(16) << "$"<< .07*sbttl< cout<< "Total"<< setw(20) << "$" <
}


//function definition
float coffee(int& quantity)
{

cout<< "Enter the amount of coffee: ";
cin>> quantity;
return quantity * 1.25 ;
}

//function definition
float bagel(int& quantity)
{

cout< cin>> quantity;
return quantity * 1.50 ;
}

//function definition
float danish(int& quantity)
{
cout< cin>> quantity;
return quantity * 2.25 ;
}

//function definition
float cal_subTtl(float p1, float p2, float p3)
{
return p1 + p2 + p3;
}

//function definition
float cal_Total(float st)
{
return (.07 * st) + st;
}

C++:Problem13

Problem:
Write a C++ program that will prompt for and input the side of a square and output a picture of the square, the area of the square and the perimeter of the square. A sample run is below.

Enter a number between 0 and 20: -3
Sorry, the number must be between 0 and 20.
Enter a number between 0 and 20: 4
Here is a square with side 4:
* * * *
* * * *
* * * *
* * * *
Its area is 16 and its perimeter is 16.
Required: Your program must have 4 functions besides the function main(). It must have a function that prompts for inputs the side of the square, a function that calculates the area of the square, a function that calculates the perimeter of the square, and a function that outputs the picture of the square.

Objective:
• To practice writing programs with functions.
• call-by-value
• call-by-reference

Display:
Enter a number between 0 and 20: -1
Sorry, the number must be between 0 and 20.
Enter a number between 0 and 20: 22
Sorry, the number must be between 0 and 20.
Enter a number between 0 and 20: 5
Here is a square with side 5
*****
*****
*****
*****
*****
Its area is 25 and its perimeter is 20

Code:
#include
using namespace std;
//function declaration
void input_side(int& lenght );
//purpose: none
//preconditon: input side of a square
//postcondtion: none

void display_square(int lenght);
//purpose: to display the picture of the square
//preconditon: input side of the square
//postcondtion: return the display of the square

int area ( int lenght);
//purpose: to calculate the area of the square
//preconditon: input side s of the square
//postcondtion: return the area of the square

int perimeter( int lenght);
//purpose: to calculate the perimeter of the square
//preconditon: input side s of the square
//postcondtion: return the perimeter of the square

int main()
{
//Input variables
int side

//Prompt the user to input
cout << "Enter a number between 0 and 20: ";
cin >> side;

//Using while loop that ask the user to enter the input again if the user
//failed in inputting it.
while (side > 20 || side <= 0)
{
cout << "Sorry, the number must be between 0 and 20.";
cout << "\nEnter a number between 0 and 20: ";
cin >> side;
}
input_side(side);
display_square(side);
cout << "Its area is "<< area (side)
<< " and its perimeter is "
<< perimeter(side) << endl;

return 0;
}
// Function Definition
void input_side(int& lenght)
{
cout << "Here is a square with side " << lenght << endl;
}
// Function Definition
void display_square(int lenght)
{
int i=0, j=0;
for(j=0;j{
for(i=0;icout << "*";
cout << endl;
}
}
// Function Definition
int area (int lenght)
{
return lenght*lenght;
}
// Function Definition
int perimeter(int lenght)
{
return lenght*4;
}

C++:Problem12

Problem:
Write a program that asks the user’s height, weight, and age and then computes clothing sizes according to the formulas:

• Hat size = weight in pounds divided by height in inches and all that multiplied by 2.9.
• Jacket size (chest in inches) = height times weight divided by 288 and then adjusted by 1/8 of an inch for each 10 years over age 30. (Note that the adjustment only takes after a full ten years. So, there is no adjustment for ages 30 through 39, but 1/8 of an inch is added for age 40.)
Waist in inches = weight divided by 5.7 and then adjusted by adding 1/10 of an inch for each 2 years over age 28. (Note that the adjustment only takes place after a full 2 years. So, there is no adjustment for age 29, but 1/10 of an inch is added for age 30.)

Objective:
• Software Design
• Writing functions
• Function prototypes with comments: purpose, precondition and postconditon
• Call to functions
• Function definitions
• Using logical data types for variables
• Using good format for source program

Software Design:
Problem: Write a program that asks the user’s height, weight, and age and then computes clothing sizes according to the formulas:

• Hat size = weight in pounds divided by height in inches and all that multiplied by 2.9.
• Jacket size (chest in inches) = height times weight divided by 288 and then adjusted by 1/8 of an inch for each 10 years over age 30. (Note that the adjustment only takes after a full ten years. So, there is no adjustment for ages 30 through 39, but 1/8 of an inch is added for age 40.)
• Waist in inches = weight divided by 5.7 and then adjusted by adding 1/10 of an inch for each 2 years over age 28. (Note that the adjustment only takes place after a full 2 years. So, there is no adjustment for age 29, but 1/10 of an inch is added for age 30.)


A. Analyze:

GIVEN:
- (User) enters his/her height.
- (User) enters his/her weight.
- (User) (User) enters his/her age.

FIND:
- Hate size in inches.
- Jacket size (chest in inches.)
- Waist in inches.

B. Data Types:
1. Functions
a. double hat(double weight ,double height)

b. double jacket(double height,double weight,int age)
Local variables:
double size;
int j;

c. double waist(double height,double weight,int age)
Local variables:
double size2;
int k;


2. Input variables
double height;
double weight;
int age;
char answer;

C. Formulas:
For the hat size ((weight/height) * 2.9);
For age smaller than or equal to 30
size = (height * weight) / 288);
For age bigger than 30
size =((height * weight) / 288)+((1.0/8)*j); where j= (age-30)/10;
For age smaller than or equal to 28
size2 = weight/(5.7);
For age bigger than 28
size2 = (weight/(5.7))+( (1.0/10)*k); where k = (age-28)/2;
D. Algorithm:
1. Introduction
2. Ask user for his/her height.
3. Ask user for his/her weight.
4. Ask user for his/her age.
5. Calculations
a. The size of the hate.
b. The size of the jacket.
c. The size of the waist.
6. Output Results
7. Closing

Display:
This program will calculate a customers size for hat
jacket and waist based on inputs of age weight and height.

Enter the customer's height in inches: 67
Enter the customer's weight in pounds: 240
Enter the customer's age: 45


Your Hat size: 10.39
Your Jacket size: 55.96
Your Waist size: 42.91


Would you like to continue (y/n)? y
Enter the customer's height in inches: 55
Enter the customer's weight in pounds: 120
Enter the customer's age: 23


Your Hat size: 6.33
Your Jacket size: 22.92
Your Waist size: 21.05


Would you like to continue (y/n)? n
End of program

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

//function declaration
double hat(double,double);
//purpose: find size of hat
//preconditon: input weight in pounds & height in inches
//postcondtion: return size of hat

//function declaration
double jacket(double,double,int);
//purpose: find size of jacket
//preconditon: input height in inches & weight in pounds & age
//postcondtion: return size of jacket

//function declaration
double waist(double,double,int);
//purpose: find size of waist
//preconditon: input weight in pounds & age
//postcondtion: return size of waist

int main ()
{
//variable declaration
double height, weight;
int age;
char answer;

//format decimal
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);

cout << "\t\t This program will calculate a customers size for hat\n"
<< "\t\t jacket and waist based on inputs of age weight and height.\n\n";

do
{
cout<< "Enter the customer's height in inches: ";
cin>>height;
cout<< "Enter the customer's weight in pounds: ";
cin>>weight;
cout<< "Enter the customer's age: ";
cin>>age;
cout< cout<< "\tYour Hat size: "< cout<< "\tYour Jacket size: "< cout<< "\tYour Waist size: "< cout<< "Would you like to continue (y/n)? ";
cin>>answer;
}while(toupper(answer) == 'Y');
cout<< "End of program"< return 0;
}

//function definition
double hat(double weight ,double height)
{
return ((weight/height) * 2.9);
}

//function definition
double jacket(double height,double weight,int age)
{
double size;
int j;
if (age>=30)
{
if((age % 10) !=0)
age = age-(age%10);
j= (age-30)/10;
size =((height * weight) / 288)+((1.0/8)*j);
}
else
size =((height * weight) / 288);
return size;
}

//function definition
double waist(double height,double weight,int age)
{
double size2;
int k;
if(age >= 28)
{
if((age % 2) !=0)
age = age-(age%2);
k = (age-28)/2;
size2 = (weight/(5.7))+( (1.0/10)*k);
}
else
size2 = weight / (5.7);
return size2;
}

C++:Problem11

Problem:
Write a program that asks the user to enter the width and the length of the rectangle that the user wants to create.

Display:
Enter a length between 3 and 20: 21



You entered a non valid input. Try again
Enter a length between 3 and 20: 4

Enter a width between 3 and 20: 0


You entered a non valid input. Try again
Enter a width between 3 and 20: 5

*****
* *
* *
*****
Do you want to try again?(y/n) :y
Enter a length between 3 and 20: 7

Enter a width between 3 and 20: 12
************
* *
* *
* *
* *
* *
************
Do you want to try again?(y/n) :n

Code:
#include
using namespace std;

int main ()

{
//declaration of variables
int len, wid;
char choice;

//Using do while loop to ask the user again if he or she wants to try the program again
do
{
cout<< "Enter a length between 3 and 20: ";
cin >> len;
cout << endl;
while(len < 3 || len > 20 )
{
cout <<"\n\nYou entered a non valid input. Try again";
cout<< "\nEnter a length between 3 and 20: ";
cin >> len;
cout << endl;
}
cout << "Enter a width between 3 and 20: ";
cin >> wid;
while(wid < 3 || wid > 20)
{
cout <<"\n\nYou entered a non valid input. Try again";
cout<< "\nEnter a width between 3 and 20: ";
cin >> wid;
cout << endl;
}
int i=1,j=1,k=1,m=1,l=1;
for(i=1;i<=wid;i++)
cout<<"*";
for(j=1;j<=len-2;j++)
{
cout< for(k=1;k<=wid-2;k++)
cout <<" ";
for(l=1;l<=wid-k;l++)
cout<<"*";
}
coutcout<<"*";
cout <<"\nDo you want to try again?(y/n) :";
cin >> choice;
}
while(choice == 'y' || choice =='Y');

return 0;
}

C++:Problem10

Problem:
Write a C++ program that performs the following tasks:
1. Prompts the user to enter a positive integer that is less than 20.
2. Inputs an integer and validates that it is positive and less than 20.
3. Outputs four triangles with the following properties:
All four triangles are right triangles
Each triangle has a different orientation.
If n is the input number, the last row of the triangles has n *'s in the largest row.
For example, if the number input is 4, the program should output the following triangles

a. *
**
***
****

b. ****
***
**
*

c. ****
***
**
*

d. *
**
***
****

4, Outputs the same four triangles right next to each other.
For example, if the input number is 5, the following pattern should be output:
* ********** *
** **** **** **
*** *** *** ***
**** ** ** ****
****** ******
5. Allows the user to repeat the program as many times as they desire with different input.

Objective:
To gain practice in programming loops

Display:
Enter a postive integer that is less than 20: 0



You entered a non valid input. Try again
Enter a postive integer: 20



You entered a non valid input. Try again
Enter a postive integer: 7

*
**
***
****
*****
******
*******

*******
******
*****
****
***
**
*

*******
******
*****
****
***
**
*

*
**
***
****
*****
******
*******

* ************** *
** ****** ****** **
*** ***** ***** ***
**** **** **** ****
***** *** *** *****
****** ** ** ******
******** ********

Would you like to try again (y/n): n

End of Program

Code:
#include
using namespace std;

int main ()

{

//declaration of variables
int row, num = 1, num2 = 1, num3 = 1, num4 = 1,num5 = 1, num6 = row - num4, num7 = row - num4 + 1, num8 = 1;
char choice;

do
{
cout <<"\nEnter a postive integer that is less than 20: ";
cin >> row;
cout << endl;
while(row <= 0 || row >= 20)
{
cout <<"\n\nYou entered a non valid input. Try again";
cout <<"\nEnter a postive integer: ";
cin >> row;
cout << endl;
}



for(num=1;num<=row;num++)
{
for(num2 = 1; num2 <= num; num2++)
{
cout << "*";
}
cout << endl;
}
cout << endl;



for(num = row; num >= 1;num--)
{
for(num2 = 1; num2 <= num ; num2++)
{
cout << "*";
}
cout << endl;
}
cout << endl;



for(num=1;num<=row;num++)
{
for(num2 = num; num2 >=1; num2--)
{
for(num2 = num; num2 >1 ; num2--)
{
cout << " ";
}
for(num3 = num; num3 <= row; num3++)
{
cout << "*";
}
}
cout << endl;
}
cout << endl;



for(num=1;num<=row;num++)
{
for(num2 = num; num2 >=1; num2--)
{
for(num3 = num; num3 < row; num3++)
{
cout << " ";
}
for(num2 = num; num2 >=1 ; num2--)
{
cout << "*";
}
}
cout << endl;
}
cout << endl;



for(num4=1; num4<= row; num4++)
{
for(num5=1;num5<=num4;num5++)
cout<<"*";
for(num6=row-num4;num6>=1;num6--)
cout<<" ";
for(num7=row-num4+1;num7>=1;num7--)
cout<< "*";
for(num8=1;num8 cout<<" ";
for(num5=1;num5 cout<<" ";
for(num7=row-num4+1;num7>=1;num7--)
cout<< "*";
for(num7=row-num4+1;num7>1;num7--)
cout<< " ";
for(num8=1;num8<=num4;num8++)
cout<<"*";
cout << endl;
}


cout<<"\nWould you like to try again (y/n): ";
cin>>choice;
}
while(choice =='y' || choice =='Y');

cout << "\nEnd of Program\n\n";

return 0;
}

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

C++:Problem2

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

Objectives:
• Using arithmetic calculations
• Using division and mod
• Formatting source program
• Putting comments

Display:
Enter the elapsed time in seconds: 9630
02:40:30.
End of Program

Code:
#include
using namespace std;

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

//Prompt the user for input
cout << "Enter the elapsed time in seconds: ";
cin >> elapsedtime;

//Calculation
hours = elapsedtime / 3600;
remain = elapsedtime % 3600;
minutes = remain / 60;
seconds = remain % 60;

//Display output
if (hours < 10)
cout << "0" << hours <<":";
else
cout << hours << ":";

if (minutes < 10)
cout << "0" << minutes << ":";
else
cout << minutes << ":";

if (seconds < 10)
cout << "0" << seconds <<".";
else
cout << seconds <<".";

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

C++:Problem1

Problem:
Write a C++ program that asks the user for two integers and then outputs their sum, difference (both ways) and product Use the template for understanding proper indentation of statements.

Objective:
• cin and cout
• calculations
• proper indentation of statements
• proper usage of comment statements
• user-friendly input/out

Display:
Please input number 1: 5
Please input number 2: 10
The numbers you input are 5 and 10
The sum of 5 and 10 is: 15
The difference of 5 and 10 is: -5
The difference of 10 and 5 is: 5
The product of 5 and 10 is: 50

End of Program

Code:
#include
using namespace std;

int main ()
{
//declaration of variables
int firstnumber, secondnumber, sum, subtract1, subtract2, multiply;

//Prompt the user for input
cout << "Please input number 1: ";
cin >> firstnumber;
cout << "Please input number 2: ";
cin >> secondnumber;
cout << "The numbers you input are "<< firstnumber<<" and "<< secondnumber << endl;

//Calculation
sum = firstnumber + secondnumber;
subtract1 = firstnumber - secondnumber;
subtract2 = secondnumber - firstnumber;
multiply = firstnumber * secondnumber;

//Display output
cout<<"The sum of "<< firstnum ber << " and "<< secondnumber << " is: "<< sum << endl;
cout <<"The difference of " << firstnumber <<" and " << secondnumber << " is: " << subtract1 << endl;
cout <<"The difference of " << secondnumber <<" and " << firstnumber << " is: " << subtract2 << endl;
cout <<"The product of " << firstnumber << " and " << secondnumber << " is: " << multiply << endl;

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