ACCIS Online Portfolio

Beginning C++ Assignment 1

Name: Janine Bouyssounouse
Student ID: 4782181
E-mail: jcbouyss@yahoo.com
Course Number: CS110
Course Code: 03A2
Assignment Number: 1
Compiler Version: CodeWarrior 9
Version of Word: 2002

This assignment graded by V. Anton Spraul on 19 July 2004.
Score: (25.2/26) 97%

Although it wasn't a problem for this assignment, you should also include the source code files for your programs in the .zip, in addition to copying the programs into your Word document, as you have done. Having them here in the document allows us to make comments easily, but we also need the source code to make life easy if we need to run the code.

Excellent work. Good use of functions, although that's not covered in this assignment. One thing I would caution against is short variables names. Use meaningful names, like wages instead of w. Never sacrifice readability for keystrokes.

1.1 Explain in your own words the following terms: structured programming, software, objectoriented programming.

Structured programming is an organized way to write programs that make it easier for programmers to read, maintain, and debugtheir programs.

Software is a set of instructions to tell the hardware of a computer what to do. Programs are software.

Object oriented programming uses nouns used to describe a program as smaller portions of code to represent the real world. For instance a car could be an object and it would have different characteristics and behaviors to describe what the car would look like and do. The idea is that the object car could be used by many different programs, but it only has to be written once.

1.2 Evaluate the following expressions as C++ would, giving the result and the type of the result
(example, for 10 / 5 the answer would be <2, int>):
a. 13 % 5 <3, int>
b. 13 / 5 <2, int>
c. 12 / 5 <2, int>
d. 5 / 12 <0, int>
e. 5 % 12 <5, int>

1.3 Write a program that displays the sum of five input values.

Not a problem, but the intention is that you stick to techniques shown to this point in the course.

// -------------------------------------------------------------
// Program CS110ques1_3
//
// Purpose: Adds five integers.
//
// Author : Janine Bouyssounouse
// Date : July 17, 2004
// -------------------------------------------------------------

#include

using std::cout;
using std::cin;
using std::endl;

void showPurpose();
int getInteger();
int computeSum(int a, int b, int c, int d, int e);
void showResults(int s);

void showPurpose()
// -------------------------------------------------------------
// Preconditions : none.
// Postconditions: The purpose of this program has been written
// to standard output.
// -------------------------------------------------------------
{
cout << "This program will ask for five integers and then ";
cout << "show their sum.\n";
}

int getInteger()
// ------------------------------------------------------------
// Precondition : none.
// Postcondition: An integer is returned.
// ------------------------------------------------------------
{
int i;

cout << "Enter an integer: "; // prompt
cin >> i; // read an integer

return i;
}

int computeSum(int a, int b, int c, int d, int e)
// ------------------------------------------------------------
// Preconditions : a, b, c, d, and e are set to integers to
// be added together
// Postconditions: The sum is returned.
// ------------------------------------------------------------
{
int s;

s = a + b + c + d + e;

return s;
}

void showResults(int s)
// -----------------------------------------------------------
// Preconditions : s is set to the sum
// Postconditions: Displays the sum.
// -----------------------------------------------------------
{
cout << "The sum is " << s << endl;
}

// function main begins program execution
int main()
{
int one; // first number to be input by user
int two; // second number to be input by user
int three;// third number to be input by user
int four; // fourth number to be input by user
int five; // fifth number to be input by user
int sum; // variable in which sum will be stored

showPurpose();

one = getInteger();

two = getInteger();

three = getInteger();

four = getInteger();

five = getInteger();

sum = computeSum(one, two, three, four, five);

showResults(sum);

return 0; // indicate that program ended successfully

} // end function main

output:

This program will ask for five integers and then show their sum.
Enter an integer: 5
Enter an integer: 4
Enter an integer: 3
Enter an integer: 2
Enter an integer: 1
The sum is 15

2.1 As in 1.2, evaluate the following expressions as C++ would, giving the result and the type of the
result:
a. 20 / 5 <4, int>
b. 20 / 5.0 <4, double>
c. 14 / 5 <2, int>
d. 14.0 / 5 <2.8, double>

2.1 (-0.3) Write a program to compute the mass of a block of aluminum. The program should input the
dimensions of the block (i.e., length, width, and height) in centimeters. The density of aluminum
is 2.7 grams per cubic centimeter.

// -------------------------------------------------------------
// Program CS110ques2_1
//
// Purpose: Computes the mass of a cube of aluminum in grams
// based on user input of length, width, and height
// of the cube in centimeters and displays the
// information.
//
// Author : Janine Bouyssounouse
// Date : July 17, 2004
// -------------------------------------------------------------

#include

using std::cout;
using std::cin;
using std::endl;

void showPurpose();
double getLength();
double getWidth();
double getHeight();
double computeVolume(double length, double width, double height);
double computeMass(double d, double v);
void showResults(double m);

void showPurpose()
// -------------------------------------------------------------
// Preconditions : none.
// Postconditions: The purpose of this program has been written
// to standard output.
// -------------------------------------------------------------
{
cout << "This program will ask for the length, width, and ";
cout << "height of a cube of\naluminum in centimeters, ";
cout << "compute the mass of the cube in grams and\ndisplay ";
cout << "this information.\n";
}

double getLength()
// ------------------------------------------------------------
// Preconditions : none.
// Postconditions: The length is returned.
// ------------------------------------------------------------
{
double l;

cout << "Enter the length in centimeters: "; // prompt
cin >> l; // read the length

return l;
}

double getWidth()
// ------------------------------------------------------------
// Preconditions : none.
// Postconditions: The width is returned.
// ------------------------------------------------------------
{
double w;

cout << "Enter the width in centimeters: "; // prompt
cin >> w; // read the width

return w;
}

double getHeight()
// ------------------------------------------------------------
// Preconditions : none.
// Postconditions: The height is returned.
// ------------------------------------------------------------
{
double h;

cout << "Enter the height in centimeters: "; // prompt
cin >> h; // read the height

return h;
}

double computeVolume(double l, double w, double h)
// ------------------------------------------------------------
// Preconditions : l is set to length, w is set to width
// h is set to height
// Postconditions: The volume is returned.
// ------------------------------------------------------------
{
double v;

v = l * w * h;

return v;
}

double computeMass(double d, double v)
// ------------------------------------------------------------
// Preconditions : d is set to density, v is set to volume
// Postconditions: The mass is returned.
// ------------------------------------------------------------
{
double m;

m = d / v; mass is density times the volume.

return m;
}

void showResults(double m)
// -----------------------------------------------------------
// Preconditions : m is set to the mass
// Postconditions: Displays the mass.
// -----------------------------------------------------------
{
cout << "The mass of the cube of aluminum is ";
cout << m << " grams." << endl;
}

// function main begins program execution
int main()
{
double length; // length of cube input by user
double width; // width of cube input by user
double height; // height of cube input by user
double volume; // variable in which the volume will be stored
double density; // the density used in the calculation
double mass; // variable in which the mass will be stored

density = 2.7; // density of aluminum in grams per cubic cm

showPurpose();

length = getLength();

width = getWidth();

height = getHeight();

volume = computeVolume(length, width, height);

mass = computeMass(density, volume);

showResults(mass);

return 0; // indicate that program ended successfully

} // end function main

output:

This program will ask for the length, width, and height of a cube of
aluminum in centimeters, compute the mass of the cube in grams and
display this information.
Enter the length in centimeters: 2
Enter the width in centimeters: 3
Enter the height in centimeters: 9
The mass of the cube of aluminum is 0.05 grams.

2.2 Write a program to compute a water and sewage bill. The input is the number of gallons
consumed. The bill is computed as follows:
water costs .21 dollars per gallon of water consumed
sewage service .01 dollars per gallon of water consumed

// -------------------------------------------------------------
// Program CS110ques2_2
//
// Purpose: Computes the water and sewage bill for a month
// based on user input of gallons of water consumed
// in a month.
//
// Author : Janine Bouyssounouse
// Date : July 17, 2004
// -------------------------------------------------------------

#include

using std::cout;
using std::cin;
using std::endl;

#include

using std::setprecision;
using std::fixed;

void showPurpose();
double getGallons();
double computeWaterBill(double g, double wc);
double computeSewageBill(double g, double sc);
void showResults(double g, double wc, double sc, double wb, double sb);


void showPurpose()
// -------------------------------------------------------------
// Preconditions : none.
// Postconditions: The purpose of this program has been written
// to standard output.
// -------------------------------------------------------------
{
cout << "This program will ask for the gallons of water ";
cout << "consumed in a month,\ncompute the water and ";
cout << "sewage bill for that month, and display the costs.\n";
}

double getGallons()
// ------------------------------------------------------------
// Preconditions : none.
// Postconditions: The gallons of water used in a month is
// returned.
// ------------------------------------------------------------
{
double g;

cout << "\nEnter the gallons of water used in a month: ";
cin >> g; // read the gallons
return g;
}

double computeWaterBill(double g, double wc)
// ------------------------------------------------------------
// Preconditions : g is set to gallons of water used in a month,
// wc is set to the water cost per month
// Postconditions: The water bill amount is returned.
// ------------------------------------------------------------
{
double w;

w = g * wc;

return w;
}

double computeSewageBill(double g, double sc)
// ------------------------------------------------------------
// Preconditions : g is set to gallons of water used in a month,
// sc is set to the sewage cost per month
// Postconditions: The sewage bill amount is returned.
// ------------------------------------------------------------
{
double s;

s = g * sc;

return s;
}

void showResults(double g, double wc, double sc, double wb, double sb)
// -----------------------------------------------------------
// Preconditions : g is set to the gallons of water consumed,
// wb is set to the water bill amount,
// sb is set to the sewage bill amount
// Postconditions: Displays the water and sewage bill.
// -----------------------------------------------------------
{
cout << "\n";
cout << g <<" gallons of water were consumed for the month.\n";
cout << "\nThe cost per gallon for the water bill is ";
cout << setprecision(2) << fixed << wc << " dollars.";
cout << "\nThe water bill for the month is ";
cout << setprecision(2) << fixed << wb << " dollars.\n";
cout << "\nThe cost per gallon for the sewage bill is ";
cout << setprecision(2) << fixed << sc << " dollars.";
cout << "\nThe sewage bill for the month is ";
cout << setprecision(2) << fixed << sb << " dollars.\n";
cout << "\n\nThe total bill for the month is ";
cout << setprecision(2) << fixed << wb + sb << " dollars." << endl;
}

// function main begins program execution
int main()
{
double gallons; // gallons of water consumed in a month
double water_cost; // water cost per gallon of water
double sewage_cost;// sewage cost per gallon of water
double water_bill; // variable in which the water bill will be stored
double sewage_bill; // variable in which the sewage bill will be stored

water_cost = .21; // water cost per gallon of water in dollars
sewage_cost = .01; // sewage cost per gallon of water in dollars

showPurpose();

gallons = getGallons();

water_bill = computeWaterBill(gallons, water_cost);

sewage_bill = computeSewageBill(gallons, sewage_cost);

showResults(gallons, water_cost, sewage_cost, water_bill, sewage_bill);

return 0; // indicate that program ended successfully

} // end function main

output:

This program will ask for the gallons of water consumed in a month,
compute the water and sewage bill for that month, and display the costs.

Enter the gallons of water used in a month: 250

250 gallons of water were consumed for the month.

The cost per gallon for the water bill is 0.21 dollars.
The water bill for the month is 52.50 dollars.

The cost per gallon for the sewage bill is 0.01 dollars.
The sewage bill for the month is 2.50 dollars.


The total bill for the month is 55.00 dollars.


2.3 Write a program that reads in three numbers from the user. The program then displays the
highest number of the three and the average of the three.

// -------------------------------------------------------------
// Program CS110ques2_3
//
// Purpose: Finds the highest value and the average of three
// numbers.
//
// Author : Janine Bouyssounouse
// Date : July 18, 2004
// -------------------------------------------------------------

#include

using std::cout;
using std::cin;
using std::endl;

#include

using std::setprecision;
using std::fixed;

void showPurpose();
int getInteger();
int determineHighest(int a, int b, int c);
double determineAverage(int a, int b, int c);
void showResults(int h, double ave);

void showPurpose()
// -------------------------------------------------------------
// Preconditions : none.
// Postconditions: The purpose of this program has been written
// to standard output.
// -------------------------------------------------------------
{
cout << "This program will ask for three integers and then\n";
cout << "determine the highest value and show their average.\n";
}

int getInteger()
// ------------------------------------------------------------
// Precondition : none.
// Postcondition: An integer is returned.
// ------------------------------------------------------------
{
int i;

cout << "Enter an integer: "; // prompt
cin >> i; // read an integer

return i;
}

int determineHighest(int a, int b, int c)
// ------------------------------------------------------------
// Preconditions : a, b, and c are set to integers to
// find the highest of the three
// Postconditions: The highest is returned.
// ------------------------------------------------------------
{
int h;

h = a;

if(b > h)
h = b;

if(c > h)
h = c;

return h;
}

double determineAverage(int a, int b, int c)
// ------------------------------------------------------------
// Preconditions : a, b, and c are set to integers to
// find the highest of the three
// Postconditions: The average is returned.
// ------------------------------------------------------------
{
double ave;
int sum;

sum = a + b + c;

ave = static_cast(sum) / 3; note that you could also divide by 3.0, avoiding the explicit cast

return ave;
}

void showResults(int h, double ave)
// -----------------------------------------------------------
// Preconditions : s is set to the sum
// Postconditions: Displays the sum.
// -----------------------------------------------------------
{
cout << "The highest number is " << h << "\n";
cout << "The average of the three numbers is ";
cout << setprecision(2) << fixed << ave << endl;
}

// function main begins program execution
int main()
{
int one; // first number to be input by user
int two; // second number to be input by user
int three;// third number to be input by user
int highest; // variable in which the highest integer will be stored
double average; // variable in which the average will be stored

showPurpose();

one = getInteger();

two = getInteger();

three = getInteger();

highest = determineHighest(one, two, three);

average = determineAverage(one,two, three);

showResults(highest, average);

return 0; // indicate that program ended successfully

} // end function main

output:

This program will ask for three integers and then
determine the highest value and show their average.
Enter an integer: 10
Enter an integer: 20
Enter an integer: 30
The highest number is 30
The average of the three numbers is 20.00

------

This program will ask for three integers and then
determine the highest value and show their average.
Enter an integer: 11
Enter an integer: 17
Enter an integer: 10
The highest number is 17
The average of the three numbers is 12.67

2.4 Write a program that reads a number from the user and then displays a message indicating
whether the number is odd or even. (Note: the definition of an even number is one that is
divisible by 2 without a remainder.) Use an if-else statement.

// -------------------------------------------------------------
// Program CS110ques2_4
//
// Purpose: Determines if a number is odd or even.
//
// Author : Janine Bouyssounouse
// Date : July 18, 2004
// -------------------------------------------------------------

#include

using std::cout;
using std::cin;
using std::endl;

void showPurpose();
int getInteger();
void oddEven(int a);

void showPurpose()
// -------------------------------------------------------------
// Preconditions : none.
// Postconditions: The purpose of this program has been written
// to standard output.
// -------------------------------------------------------------
{
cout << "This program will ask for an integer and then\n";
cout << "determine if it is odd or even.\n";
}

int getInteger()
// ------------------------------------------------------------
// Precondition : none.
// Postcondition: An integer is returned.
// ------------------------------------------------------------
{
int i;

cout << "Enter an integer: "; // prompt
cin >> i; // read an integer

return i;
}

void oddEven(int a)
// ------------------------------------------------------------
// Preconditions : a is set to an integers to determine if it
// is odd or even.
// Postconditions: The result is printed.
// ------------------------------------------------------------
{
if(a % 2 == 0)
cout << a << " is an even number." << endl;
else
cout << a << " is an odd number." << endl;
}

// function main begins program execution
int main()
{
int one; // first number to be input by user

showPurpose();

one = getInteger();

oddEven(one);

return 0; // indicate that program ended successfully

} // end function main

output:

This program will ask for an integer and then
determine if it is odd or even.
Enter an integer: 7
7 is an odd number.

-----------------

This program will ask for an integer and then
determine if it is odd or even.
Enter an integer: 10
10 is an even number.


2.5 (-0.5) Write a program that reads an integer, displays its value doubled, and continues reading
and displaying until the user enters 0. Use a while loop.

// -------------------------------------------------------------
// Program CS110ques2_5
//
// Purpose: Reads an integer from the user, displays its double
// and then continues reading integers until a zero
// is entered.
//
// Author : Janine Bouyssounouse
// Date : July 18, 2004
// -------------------------------------------------------------

#include

using std::cout;
using std::cin;
using std::endl;

void showPurpose();
int getInteger();
void displayDouble();

void showPurpose()
// -------------------------------------------------------------
// Preconditions : none.
// Postconditions: The purpose of this program has been written
// to standard output.
// -------------------------------------------------------------
{
cout << "This program will ask for integers and display\n";
cout << "their doubles until a zero is entered.\n\n";
}

int getInteger()
// ------------------------------------------------------------
// Precondition : none.
// Postcondition: An integer is returned.
// ------------------------------------------------------------
{
int i;

cout << "Enter integer to be doubled, enter 0 to end: "; // prompt
cin >> i; // read an integer

return i;
}

void displayDouble()
// ------------------------------------------------------------
// Preconditions : none.
// Postconditions: Integers are doubled until a zero is entered.
// ------------------------------------------------------------
{
int i;

i = getInteger();

while(i != 0)
{
cout << i << " doubled is " << i * i << "\n\n"; doubling means the number should be multiplied by 2. You are raising to the second power.

i = getInteger();
}
}

// function main begins program execution
int main()
{
showPurpose();

displayDouble();

return 0; // indicate that program ended successfully

} // end function main

output:

This program will ask for integers and display
their doubles until a zero is entered.

Enter integer to be doubled, enter 0 to end: 2
2 doubled is 4

Enter integer to be doubled, enter 0 to end: 6
6 doubled is 36

Enter integer to be doubled, enter 0 to end: 9
9 doubled is 81

Enter integer to be doubled, enter 0 to end: 0


2.6 Imagine that 10 int values are labeled by position: 1, 2, 3, etc. Write a program that reads
10 integers and tracks how many of them have the same value as their position. That is, if
the first number read is 1, or the third number is 3, that counts as a match, and the output
would be the number of matches Use a single if statement and a loop.

// -------------------------------------------------------------
// Program CS110ques2_6
//
// Purpose: Reads ten integers from the user, counts how many of
// the integers are the same as the ordered they were
// entered.
//
// Author : Janine Bouyssounouse
// Date : July 18, 2004
// -------------------------------------------------------------

#include

using std::cout;
using std::cin;
using std::endl;

void showPurpose();
int getInteger();
int countOrderMatches();
void showResults(int m);

void showPurpose()
// -------------------------------------------------------------
// Preconditions : none.
// Postconditions: The purpose of this program has been written
// to standard output.
// -------------------------------------------------------------
{
cout << "This program will ask for ten integers and count\n";
cout << "how many times the number entered is the same as\n";
cout << "the order it was entered.\n\n";
}

int getInteger()
// ------------------------------------------------------------
// Precondition : none.
// Postcondition: An integer is returned.
// ------------------------------------------------------------
{
int i;

cout << "Enter an integer: "; // prompt
cin >> i; // read an integer

return i;
}

int countOrderMatches()
// ------------------------------------------------------------
// Preconditions : none.
// Postconditions: Integers are matched against the order they
// are entered and that numbr is counted
// Returns the number of matches.
// ------------------------------------------------------------
{
int i;
int c = 1;
int m = 0;

while(c <= 10)
{
i = getInteger();

if(c == i)
m++;

c++;
}

return m;
}

void showResults(int m)
// -----------------------------------------------------------
// Preconditions : m is set to the number of matches
// Postconditions: Displays the number of matches.
// -----------------------------------------------------------
{
cout << "The number of matches is " << m << endl;
}

// function main begins program execution
int main()
{
int matches; // variable in which the number of matches is stored

showPurpose();

matches = countOrderMatches();

showResults(matches);

return 0; // indicate that program ended successfully

} // end function main

output:

This program will ask for ten integers and count
how many times the number entered is the same as
the order it was entered.

Enter an integer: 4
Enter an integer: 2
Enter an integer: 1
Enter an integer: 4
Enter an integer: 9
Enter an integer: 6
Enter an integer: 2
Enter an integer: 3
Enter an integer: 1
Enter an integer: 5
The number of matches is 3


3.1 Using the switch statement, write a program that will request a number from the user in the
range 1-5. It should then output a message in this form:
You entered the number one.
{or two, or whatever was entered}
If the user entered a number other than 1-5, the program should output a message to that
effect.

// -------------------------------------------------------------
// Program CS110ques3_1
//
// Purpose: Reads an integer from the user between 1 and 5,
// displayes a message stating which integer was
// entered, and gives an error message if an
// incorrect entry is entered.
//
// Author : Janine Bouyssounouse
// Date : July 18, 2004
// -------------------------------------------------------------

#include

using std::cout;
using std::cin;
using std::endl;

void showPurpose();
int getInteger();
void showResults(int i);

void showPurpose()
// -------------------------------------------------------------
// Preconditions : none.
// Postconditions: The purpose of this program has been written
// to standard output.
// -------------------------------------------------------------
{
cout << "This program will ask for an integer between 1 and 5\n";
cout << "and displays a message stating which integer was\n";
cout << "entered. An error message is displayed if an input\n";
cout << "was received outside of the parameters.\n\n";
}

int getInteger()
// ------------------------------------------------------------
// Precondition : none.
// Postcondition: An integer is returned.
// ------------------------------------------------------------
{
int i;

cout << "Enter an integer between 1 and 5: "; // prompt
cin >> i; // read an integer

return i;
}

void showResults(int i)
// ------------------------------------------------------------
// Preconditions : i is set to the integer entered by the user.
// Postconditions: A message is displayed based on the integer
// entered.
// ------------------------------------------------------------
{
switch(i)
{
case 1:
cout << "You entered the number one.";
break;

case 2:
cout << "You entered the number two.";
break;

case 3:
cout << "You entered the number three.";
break;

case 4:
cout << "You entered the number four.";
break;

case 5:
cout << "You entered the number five.";
break;

case '\n':
case '\t':
case ' ':
break;

default:
cout << "Incorrect input value.";
}
}

// function main begins program execution
int main()
{
int input; // variable in which the number input is stored

showPurpose();

input = getInteger();

showResults(input);

return 0; // indicate that program ended successfully

} // end function main

output:

This program will ask for an integer between 1 and 5
and displays a message stating which integer was
entered. An error message is displayed if an input
was received outside of the parameters.

Enter an integer between 1 and 5: 3
You entered the number three.

-----------------------

This program will ask for an integer between 1 and 5
and displays a message stating which integer was
entered. An error message is displayed if an input
was received outside of the parameters.

Enter an integer between 1 and 5: 8
Incorrect input value.

3.2 Rewrite the following code with a for loop:
float Value;
cin >> Value;
while (Value <= 10) {
cout << Value;
++Value;
}

I decided I was supposed to write an entire program to go with this assignment to show the two loops were the same.

// -------------------------------------------------------------
// Program CS110ques3_2
//
// Purpose: Changing a while loop to a for loop with a
// float counter.A float is not generally a good idea
// to be used in a for loop. For loops are best used
// with integers.
//
// Author : Janine Bouyssounouse
// Date : July 18, 2004
// -------------------------------------------------------------

#include

using std::cout;
using std::cin;
using std::endl;

void showPurpose();
void whileLoop();
void forLoop();

void showPurpose()
// -------------------------------------------------------------
// Preconditions : none.
// Postconditions: The purpose of this program has been written
// to standard output.
// -------------------------------------------------------------
{
cout << "This program will make changes to a while loop\n";
cout << "to use it as a for loop.\n";
}

void whileLoop()
// ------------------------------------------------------------
// Precondition : none.
// Postcondition: The original while loop displaying Value.
// ------------------------------------------------------------
{
float Value;

cout << "Enter a float: ";
cin >> Value;

while(Value <= 10)
{
cout << "In the while loop the value is now " << Value << "\n";
++Value;
}
}

void forLoop()
// ------------------------------------------------------------
// Preconditions : none.
// Postconditions: The changed for loop displaying Value.
// ------------------------------------------------------------
{
float Value;

cout << "Enter a float: ";
cin >> Value;

for(;Value <= 10; Value++)
{
cout << "In the for loop the value is now " << Value << "\n";
}
}

// function main begins program execution
int main()
{
showPurpose();

whileLoop();

forLoop();

return 0; // indicate that program ended successfully

} // end function main

output:

This program will make changes to a while loop
to use it as a for loop.
Enter a float: 7.3
In the while loop the value is now 7.3
In the while loop the value is now 8.3
In the while loop the value is now 9.3
Enter a float: 7.3
In the for loop the value is now 7.3
In the for loop the value is now 8.3
In the for loop the value is now 9.3


3.3 Rewrite the code in the previous question using a do-while loop. Under what circumstances
will it act differently than the while and for versions of this code? Why?

// -------------------------------------------------------------
// Program CS110ques3_3
//
// Purpose: Changing a while loop to a do-while loop with a
// float counter.
//
// Author : Janine Bouyssounouse
// Date : July 18, 2004
// -------------------------------------------------------------

#include

using std::cout;
using std::cin;
using std::endl;

void showPurpose();
void whileLoop();
void doWhileLoop();

void showPurpose()
// -------------------------------------------------------------
// Preconditions : none.
// Postconditions: The purpose of this program has been written
// to standard output.
// -------------------------------------------------------------
{
cout << "This program will make changes to a while loop\n";
cout << "to use it as a do-while loop.\n";
}

void whileLoop()
// ------------------------------------------------------------
// Precondition : none.
// Postcondition: The original while loop displaying Value.
// ------------------------------------------------------------
{
float Value;

cout << "Enter a float: ";
cin >> Value;

while(Value <= 10)
{
cout << "In the while loop the value is now " << Value << "\n";
++Value;
}
}

void doWhileLoop()
// ------------------------------------------------------------
// Preconditions : none.
// Postconditions: The changed do-while loop displaying Value.
// ------------------------------------------------------------
{
float Value;

cout << "Enter a float: ";
cin >> Value;

do
{
cout << "In the do-while loop the value is now " << Value << "\n";
} while(++Value <=10);
}

// function main begins program execution
int main()
{
showPurpose();

whileLoop();

doWhileLoop();

return 0; // indicate that program ended successfully

} // end function main
output:

This program will make changes to a while loop
to use it as a do-while loop.
Enter a float: 7.8
In the while loop the value is now 7.8
In the while loop the value is now 8.8
In the while loop the value is now 9.8
Enter a float: 7.8
In the do-while loop the value is now 7.8
In the do-while loop the value is now 8.8
In the do-while loop the value is now 9.8

----------------

This program will make changes to a while loop
to use it as a do-while loop.
Enter a float: 10.8
Enter a float: 10.8
In the do-while loop the value is now 10.8

Under what circumstances will a do-while loop act differently than the while and for versions of this code? Why? The do-while loop will run through one time, even if Value is greater than 10. This is because the loop continuation condition does not get checked until the end of the first loop. In the other loops, the check is done before entering the loop, so the loop is bypassed all together if Value is greater than 10.

3.4 In a certain company, employees are paid extra for overtime using the following system.
For every hour over 40 worked, the employee is paid 1.5 times hourly wages. For every
hour over 50, the employee is paid 2 times hourly wages. For example, an employee who
earns $10 an hour and works for 55 hours would receive:
Regular pay: 40 hrs x $10 = $400
Time-and-a-half: 10 hrs x 1.5 x $10 = $150
Double: 5 x 2 x $10 = $100
Total: = $650
Write a program that prompts for the number of hours worked in a week and an hourly
wage, and then calculates and displays the weekly pay. The prompt for data should then
return and allow the user to enter more data. The user should be able to terminate the
program by entering -1 for the number of hours worked.


// -------------------------------------------------------------
// Program CS110ques3_4
//
// Purpose: Reads the weekly hours and hourly wage for an employee.
// Then calculates the salary of the employee give that
// hours worked over 40, up to 50 are at time and a half
// and hourse worked over 50 hours is as double time.
// Enter hours until a -1 is entered.
//
// Author : Janine Bouyssounouse
// Date : July 18, 2004
// -------------------------------------------------------------

#include

using std::cout;
using std::cin;
using std::endl;
using std::fixed;

#include

using std::setprecision;

void showPurpose();
double getHoursWorked();
double getHourlyWage();
double calculateWeeklyPay(double h, double w);
void showResults(double p);

void showPurpose()
// -------------------------------------------------------------
// Preconditions : none.
// Postconditions: The purpose of this program has been written
// to standard output.
// -------------------------------------------------------------
{
cout << "This program asks for hours worked in a week\n";
cout << "and the hourly wage for an employee. Then it\n";
cout << "calculates the weekly pay for that employee.\n";
cout << "This process continues until a value of -1\n";
cout << "is entered and then the program ends.\n\n";
}

double getHoursWorked()
// ------------------------------------------------------------
// Precondition : none.
// Postcondition: Hours worked in a week is returned.
// ------------------------------------------------------------
{
double i;

cout << "Enter the weekly hours, enter -1 to end: "; // prompt
cin >> i; // read an integer

return i;
}

double getHourlyWage()
// ------------------------------------------------------------
// Preconditions : none.
// Postconditions: Hourly wage is returned.
// ------------------------------------------------------------
{
double i;

cout << "Enter the hourly wage: "; // prompt
cin >> i; // read an integer

return i;
}

double calculateWeeklyPay(double h, double w)
// ------------------------------------------------------------
// Preconditions : h is set to hours worked in a week.
// w is set to hourly wage.
// Postconditions: Weekly pay is returned.
// ------------------------------------------------------------
{
double wp; //weekly pay

if(h <= 40)
{
wp = h * w;
}

if(h > 40 && h <= 50)
{
wp = (40 * w) + ((h - 40) * (1.5 * w));
}

if(h > 50)
{
wp = (40 * w) + (10 * (1.5 * w)) + ((h - 50) * (2 * w));
}

This should work, but it is a little risky. Floating-point types such as double are not always trustworthy for comparisons, plus these tests are a little redundant. I'd rather see:

if (h < = 40) { ...}
else if (h <= 50) {...}
else {...}

return wp;
}

void showResults(double p)
// ------------------------------------------------------------
// Preconditions : p is set to weekly pay
// Postconditions: Weekly pay is displayed.
// ------------------------------------------------------------
{
cout << fixed << setprecision(2);
cout << "The weekly pay for this employee is " << p << "\n\n";
}

// function main begins program execution
int main()
{
double hours; // hours worked in a week
double wage; // hourly wage
double pay; // weekly pay after calculation

pay = 0;

showPurpose();

hours = getHoursWorked();

while(hours != -1)
{
wage = getHourlyWage();

pay = calculateWeeklyPay(hours, wage);

showResults(pay);

hours = getHoursWorked();
}

return 0; // indicate that program ended successfully

} // end function main

output:

This program asks for hours worked in a week
and the hourly wage for an employee. Then it
calculates the weekly pay for that employee.
This process continues until a value of -1
is entered and then the program ends.

Enter the weekly hours, enter -1 to end: 40
Enter the hourly wage: 5
The weekly pay for this employee is 200.00

Enter the weekly hours, enter -1 to end: 41
Enter the hourly wage: 5
The weekly pay for this employee is 207.50

Enter the weekly hours, enter -1 to end: 50
Enter the hourly wage: 5
The weekly pay for this employee is 275.00

Enter the weekly hours, enter -1 to end: 51
Enter the hourly wage: 5
The weekly pay for this employee is 285.00

Enter the weekly hours, enter -1 to end: -1




website created by Janine Bouyssounouse. Last updated 02/08/07