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