Labex 3
Labex 3
Labex 3
LABORATORY EXERCISE #1
Looping Structure
DATE SUBMITTED
LAB 5.1.1 while loop
(Counter-controlled)
#include <iostream>
using namespace std;
int main()
{
char letter = 'a';
Questions:
1. This program is not user friendly. Run it a few times and explain
why.
- I type all of the character and it only stop when I type letter x or X it is
because the statement in while is (letter != 'x') so that all of the character not equal
to x will be displayed.
int main(){
int main(){
int month=1;
cout << "Enter the total rainfall for the month " << month << endl;
cout << "Enter -1 when you are finished" << endl;
cin >> rain;
return 0;
}
5. Run the program several times with various input. Record your
results. Are they correct? What happens if you enter –1 first?
What happens if you enter only values of 0 for one or more
months? Is there any numerical data that you should not enter?
- If you entered the -1 first it will display no data has been entered unless you put data
first before enter -1. And if you enter 0 for one or more months it will record 0 as 0.
int main() {
int number;
float cost;
char beverage;
bool validBeverage;
cout << fixed << showpoint << setprecision(2);
do
{
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 << endl << endl;
cout << "Enter the beverage A,B,C, or D you desire" << endl;
cout << "Enter E to exit the program" << endl << endl;
cin >> beverage;
switch(beverage) {
case 'a':
case 'A':
case 'b':
case 'B':
case 'c':
case 'C':
case 'd':
case 'D':
validBeverage = true;
break;
default:
validBeverage = false;
}
if (validBeverage == true)
{
cout << "How many cups would you like?" << endl;
cin >> number;
}
switch (beverage){
case 'a':
case 'A':
cost = number * 1.0;
cout << "The total cost is $ " << cost << endl;
break;
case 'b':
case 'B':
cost = number * 0.75;
cout << "The total cost is $ " << cost << endl;
break;
case 'c':
case 'C':
cost = number * 1.25;
cout << "The total cost is $ " << cost << endl;
break;
case 'd':
case 'D':
cost = number * 2.50;
cout << "The total cost is $ " << cost << endl;
break;
case 'e':
case 'E':
cout << "Please come again" << endl;
break;
default:
cout << "Error! Please input a valid selection.";
return 0;
}
if (validBeverage == true)
with the line
if (validBeverage)
and run the program again. Are there any differences in the execution of the
program? Why or why not?
- There are no differences between if (validBeverage == true) and if
(validBeverage) because above of this true is assign to variable validBeverages.
The for.cpp program has the user input a number n and then finds
the mean of the first n positive integers. The code is shown below:
// This program has the user input a number n and then finds the
// mean of the first n positive integers
#include <iostream>
using namespace std;
int main()
{
int value; // value is some positive number n
int total = 0; // total holds the sum of the first n //positive
numbers
int number; // the amount of numbers
float mean; // the average of the first n positive
// numbers
cout << "Please enter a positive integer" << endl;
cin >> value;
if (value > 0)
{
for (number = 1; number <= value; number++)
{
total = total + number;
}
// curly braces are optional since there is only one
// statement
cout << "The mean average of the first " << value
<< " positive integers is " << mean << endl;
}
else
cout << "Invalid input - integer must be positive"
<< endl;
return 0;
}
10. Why is the typecast operator needed to compute the mean in the
statement mean = static_cast(float)(total)/value; ? What do you
think will happen if it is removed? Modify the code and try it.
Record what happens. Make sure that you try both even and odd
cases. Now put static_cast<float> total back in the program.
Answer: The type_cast operator converts the data type to another, for
example, from int to float. When removed, it still shows accurate result when
value is odd. However, when value is even, the decimal place on the result is
ignored. For example, the mean of the first 4 positive integers becomes 2
instead of 2.5.
11. What happens if you enter a float such as 2.99 instead of an
integer for value? Try it and record the results.
-The program only reads the whole number and ignores the decimal value.
Below is an example screen output:
12. Modify the code so that it computes the mean of the consecutive
positive integers n, n+1, n+2, . . , m, where the user chooses n and
m. For example, if the user picks 3 and 9, then the program should
find the mean of 3, 4, 5, 6, 7, 8, and 9, which is 6.
Answer:
#include <iostream>
using namespace std;
int main(){
int value;
int total = 0;
int number,x;
float mean;
cout << "Please enter a positive integer as the starting point: ";
cin >> x;
cout << "Please enter a positive integer as the end point: ";
cin >> value;
if (value > 0)
{
for (number = x; number <= value; number++)
{
total = total + number;
}
cout << "The mean average of the " << (value-x)+1 << " positive integers is " <<
mean << endl;
}
else
cout << "Invalid input - integer must be positive"
<< endl;
return 0;
}
LAB 5.4 Nested Loops
// This program finds the average time spent programming by a // student each day
over a three-day period.
// PLACE YOUR NAME HERE
#include <iostream>
using namespace std;
int main()
{
int numStudents;
float numHours, total, average;
int student,day = 0; // these are the counters for the loops
cout << "This program will find the average number of” <<” hours a
day that a student spent programming” <<” over a long
weekend\n\n";
cout << "How many students are there? " ;
cin >> numStudents;
return 0;
}
Note that the inner loop of this program is always executed
exactly three times—once for each day of the long weekend.
Modify the code so that the inner loop iterates n times, where n is
a positive integer input by the user. In other words, let the user
decide how many days to consider just as they choose how many
students to consider.
Sample Run:
This program will find the average number of hours a day that a
student spent programming over a long weekend
13. Modify the program above so that it also finds the average
number of hours per day that a given student studies biology as
well as programming. For each given student include two
prompts, one for each subject. Have the program print out which
subject the student, on average, spent the most time on.
Answer:
#include <iostream>
using namespace std;
int main()
{
int numStudents;
float numHours1,numHours2, total1, total2,average1,average2;
int student,day = 0;
cout << "This program will find the average number of hours a day that a student
spent programming and biology over a long weekend\n\n";
cout << "How many students are there? " ;
cin >> numStudents;
if (average1>average2){
cout << "Student " << student << " spent more time in programming than studying
biology.";
}
else{
cout << "Student " << student << " spent more time in studying biology than
programming.\n\n";
}
}
return 0;
}
Tasks
Develop your own code for the 3 programming exercises stated
below. For each program use file name <lastname_Probn.cpp>
where n is the problem number.
Problem 1:
Sample Run:
****************************************************
****************************************************
****************************************************
****************************************************
****************************************************
****************************************************
****************************************************
****************************************************
****************************************************
****************************************************
****************************************************
****************************************************
====================================================
====================================================
The total number of people surveyed is 5.
The results are as follows:
====================================================
====================================================
Problem 2:
Suppose Dave drops a watermelon off a high bridge and lets it fall
until it hits the water. If we neglect air resistance, then the distance d in
meters fallen by the watermelon after t seconds is d = 0.5 * g * t2,
where the acceleration of gravity g = 9.8 meters/second2. Write a
program that asks the user to input the number of seconds that the
watermelon falls and the height h of the bridge above the water. The
program should then calculate the distance fallen for each second from t
= 0 until the value of t input by the user. If the total distance fallen is
greater than the height of the bridge, then the program should tell the
user that the distance fallen is not valid.
Sample Run 1:
Please input the time of fall in seconds : 2
Please input the height of the bridge in meters : 100
Sample Run 2:
Please input the time of fall in seconds : 4
Please input the height of the bridge in meters : 50
Sample Run:
How many tellers worked at Nation’s Bank during each of the last
three years? 2
How many days was teller 1 out sick during year 1 ? 5
How many days was teller 1 out sick during year 2 ? 8
How many days was teller 1 out sick during year 3 ? 2
How many days was teller 2 out sick during year 1 ? 1
How many days was teller 2 out sick during year 2 ? 0
How many days was teller 2 out sick during year 3 ? 3
The 2 tellers were out sick for a total of 19 days during the last
three years.
ANSWERS:
Problem 1:
Source Code
#include <iostream>
using namespace std;
int main()
{
int choose;
int coff=0;
int tea=0;
int coke=0;
int oj=0;;
int costumer = 1;
cout << endl;
cout << "\t1. Coffee";
cout << "\t2. Tea";
cout << "\t\t3. Coke";
cout << "\t\t4. Orange Juice" << endl << endl;
cout <<
"***************************************************************************
******
**********************" << endl;
cout << endl;
cout << "Please input the favorite beverage of person #" << costumer << ": "<< endl;
cout << endl;
cout << "Choose 1, 2, 3, or 4, from the above menu or -1 to exit the program" << endl;
cin >> choose;
cout << endl;
while(choose<-1)
{
cout << "That is not an option. Please enter a valid number: ";
cin >> choose;
}
while(choose>0)
{
switch(choose)
{
case 1:
coff++;
break;
case 2:
tea++;
break;
case 3:
coke++;
break;
case 4:
oj++;
break;
default:
cout << "Sorry that's not on the menu. Please try again: ";
cin >> choose;
}
costumer++;
cout << "Please input the favorite beverage of person #" << costumer << ": "<< endl;
cout << endl;
cout << "Choose 1, 2, 3, or 4, from the above menu or -1 to exit the program" <<
endl;
cin >> choose;
cout << endl;
}
if(choose = -1)
{
cout << "Beverage";
cout << "\tNumber of Votes" << endl;
cout << "********************************" << endl;
cout << "Coffee";
cout << "\t\t\t" << coff << endl;
cout << "Tea";
cout << "\t\t\t" << tea << endl;
cout << "Coke";
cout << "\t\t\t" << coke << endl;
cout << "Orange Juice";
cout << "\t\t" << oj << endl;
}
return 0;
}
Problem 2:
Source Code
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
int x = 0, y = 0;
float seconds = 0, distance = 0;
float gravity = 9.8;
cout << "\tPlease input the time of fall in seconds: ";
cin >> x;
cout << "\tPlease input the height of the bridge in meters: ";
cin >> y;
cout << endl << endl;
cout << "\tTime Falling (seconds)";
cout << "\t\tDistance Fallen (meters)" << endl;
cout <<
"*******************************************************************"
<< endl << endl;
while(seconds<=x)
{
cout << "\t\t" << seconds << "\t\t\t\t" << distance << endl;
seconds+=1;
distance = 0.5 * gravity * pow(seconds,2);
}
return 0;
}
Problem 3:
Source Code
#include <iostream>
using namespace std;
int main()
{
int tellers_count;
int sickdays = 0;
cout << "How many tellers worked at Metro Bank during each of the last three years? ";
cin >> tellers_count;
cout << endl;
if(tellers_count<1)
{
cout << "That's a low count of tellers. Try to enter atleast 1 or more." << endl;
exit(0);
}
for(int teller = 1; teller<=tellers_count; teller++)
{
for(int year = 1; year<4; year++)
{
int days = 0;
cout << " How many days was teller " << teller << " out sick during
year " << year << " ? ";
cin >> days;
sickdays += days;
}
}
cout << "\nThe " << tellers_count << " tellers were out sick for a total of " << sickdays <<
"
days during the last 3 years.\n";
return 0;
}