Vous êtes sur la page 1sur 9

Lab Questions for Chapter 3

1.1. In C++, decisions to execute or not execute a statement or group


of statements can be made by using the statement if (condition). The
condition must be an expression that can be evaluated as either true or
false. If it evaluates to true, a succeeding statement or group of
statements enclosed in { ... }, called a block statement will be
executed.
Take a look at the following program:
#include <iostream>
using namespace std;
int main()
{
double initial_value;
double final_value;
cout << "Please enter the initial balance: ";
cin >> initial_value;
if (initial_value > 90 )
{
initial_value = initial_value + 10;
final_value = initial_value;
}
else
{
initial_value = initial_value + 5;
final_value = initial_value;
}
cout <<"Final Value" << final_value << endl;
return 0;
}
How much money will be added to the balance when you enter an initial
value of $100?
Ans: $110 will be added to the balance when you enter an initial value
of $100.
1.2.

Using the program in Lab 1.1:

#include <iostream>
using namespace std;
int main()
{
double initial_value;
double final_value;
cout << "Please enter the initial balance: ";
cin >> initial_value;
if (initial_value > 90 )
{

initial_value = initial_value + 10;


final_value = initial_value;
}
else
{
initial_value = initial_value + 5;
final_value = initial_value;
}
cout <<"Final Value" << final_value << endl;
return 0;
}
How much money will be added to the balance when you enter an initial
value of $85?
Ans: If the user inputted a number less than 90, the program will add 5
to that number. For example, if the user inputted an initial value of
85 dollars, then the program will compute and display 90 dollars.
2.1 The relational operators in C++ are == != < > <= and >=
Formulate the following conditions in C++:
x is positive
x is zero or negative
x is at least 8
x is less than 8
x and y are both zero
Ans:
1. x
2. x
3. x
4. x
5. x

> 0
>= 0
>= 8
< 8
= y = 0

2.2. Formulate the following conditions


A last name starts with the letter H.
A last name starts with the letters Mac.
A last name comes before "Jones".
Ans:
1. if (name == H)
2. if (name == Mac)
3. if (name < Jones)
3.1. How should a program respond to user input that would result in
division by zero? You can use the if/else format to explicitly specify
the action to be taken:
if (condition)
// Do something ...
else
// Do something different ...
The electric company gives a discount on electricity based upon usage.
The normal rate is $.60 per Kilowatt Hour (KWH). If the number of KWH

is above 1,000, then the rate is $.45 per KWH. Write a program that
prompts the user for the number of Kilowatt Hours used and then
calculates and prints the total electric bill.
Ans:
#include <iostream>
using namespace std;
int main()
{
cout << "Enter the number of kilowatt hours used: ";
double kwh;
cin >> kwh;
if (kwh <= 1000)
{
double bill1 = .60 * kwh;
cout << "Total electric bill: $" << bill1 << endl;
}
else
{
double bill2 = .45 * kwh;
cout << "Total electric bill: $" << bill2 << endl;
}
system("pause");
return 0;
}

3.2. According to your program in Lab 3.1, how much will it cost for:
900 KWH?
1,754 KWH?
10,000 KWH?
Ans:
1. $540
2. $789.3
3. $4500
4.1. The if/else decision for electric rates that was implemented in
Lab 3.1 can be extended to select from more than two possible outcomes.
The if ... else if ... else syntax is used to select exactly one of
several possible actions.
if (condition1)
// Do something ...
else if (condition2)
// Do something different ...
else
// Do something generic ...
Write a program to compute the electric bill when there are 4 different
rates depending on KWH used:
Usage
Rate
0-999 KWH
$0.60
1000-1499 KWH
$0.45
1500-1999 KWH
$0.40
2000 or more KWH $0.35
Ans:

#include <iostream>
using namespace std;
int main()
{
cout << "Enter the number of kilowatt hours used: ";
double kwh;
cin >> kwh;
if (kwh < 1000)
{
double bill1 = .60 * kwh;
cout << "Total electric bill: $" << bill1 << endl;
}
else if ((kwh >= 1000) && (kwh < 1500))
{
double bill2 = .45 * kwh;
cout << "Total electric bill: $" << bill2 << endl;
}
else if ((kwh >= 1500) && (kwh < 2000))
{
double bill3 = .40 * kwh;
cout << "Total electric bill: $" << bill3 << endl;
}
else
{
double bill4 = .35 * kwh;
cout << "Total electric bill: $" << bill4 << endl;
}
system("pause");
return 0;
}

7.
/*
*/

Which values of nyear cause the following loops to terminate?


Counts number of years between a user-input year and the
year 3000.

int main()
{
int nyear;
int millennium = 3000;

int years = 0;
cout << "Please enter the current year";
cin >> nyear;
while (nyear != millennium)
{
nyear++;
years++;
}

cout << " Another " << years << " years to the millennium." << "\n";
return 0;

Ans: I found that the while loop iterates one last time at nyears =
2999 and terminates when the number stored in memory is 3000.
Note: The operator,!=, compares two values, and if the values are not
equal, then the program deems it true. So, when I entered 3001, I
expected iterations to continue because 3001 is not equal to 3000, but
the program didnt display anything.
9.1. A variable that counts the iterations of a loop is called a loop
index. Looping can be accomplished with a loop index and a while loop
or we can use a for loop, with the following syntax:
for (loop_index = start_value; condition; index_increment)
Write a program controlled by two for loops that will produce the
following listing of inclusive dates, from the 5th Century B.C. through
the 5th Century A.D.
Century
Century
Century
Century
Century
Century
Century
Century
Century
Century

5
4
3
2
1
1
2
3
4
5

BC
BC
BC
BC
BC
AD
AD
AD
AD
AD

400-499
300-399
200-299
100-199
1-99
1-99
100-199
200-299
300-399
400-499

Ans:
#include <iostream>
using namespace std;
int main()
{
int year1 = 400;
for (int a = 5; a >= 1; a--)
{
int year2 = year1 + 99;
if (a == 1)
{
year1 = 1;

cout << "Century " << a << " BC " << year1 << "-" << year2 <<

endl;

cout << "Century " << a << " AD " << year1 << "-" << year2

<< endl;
}
else
{
endl;

cout << "Century " << a << " BC " << year1 << "-" << year2 <<
year1 = year1 - 100;
year2 = year1 + 99;

}
}
year1 = 100;
int year2 = 199;
for (int a = 2; a <= 5; a++)
{
cout << "Century " << a << " AD " << year1 << "-" << year2 << endl;
year1 = year1 + 100;
year2 = year2 + 100;
}
system("pause");
}

10.1. One loop type might be better suited to a purpose than another.
The following usages are idiomatic.
for
while
do while

A loop with a known number of iterations


A loop with an unknown number of iterations
A loop with at least one iteration

Convert this while loop to a do while loop:


/*

Computes a running sum of user-input integers.

*/
#include <iostream>
using namespace std;
int main()
{
int sum = 0;
int n = 1;
while (n != 0)
{

cout << "Sum = " << sum << "\n";


cout << "Please enter a number, 0 to quit ";
cin >> n;
sum = sum + n;
cout << "Sum = " << sum

<< "\n";

}
return 0;
}
Ans:
#include <iostream>
using namespace std;
int main()
{
int sum = 0;
int n = 1;
do
{

12.1.

cout << "Sum = " << sum << "\n";


cout << "Please enter a number, 0 to quit ";
cin >> n;

sum = sum + n;
cout << "Sum = " << sum << "\n";
} while (n != 0);
return 0;

Convert this for loop to a while loop:

#include <iostream>
using namespace std;
int main()
{
for (int i = 1; i <= 10; i++)
{
cout << i << " squared equals " << i * i << "\n";
}
return 0;
}
Ans:
#include <iostream>
using namespace std;
int main()
{
int i = 1;
while (i <= 10)
{
cout << i << " squared equals " << i * i << "\n";
i++;
}
system("pause");

13. Write a program to print out a 3 by 5 multiplication table that


looks like this:
1
2
3

2
4
6

3
6
9

4
8
12

5
10
15

Ans:
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
for (int i = 1; i <= 5; i++)
{
if (i == 5)
{
int product = 1 * i;
cout << product;
}
else
{
int product = 1 * i;
cout << product << setw(6);
}
}
cout << endl;
for (int i = 1; i <= 5; i++)
{
if (i == 5)
{
int product = 2 * i;
cout << product;
}
else
{
int product = 2 * i;
cout << product << setw(6);
}
}
cout << endl;
for (int i = 1; i <= 5; i++)
{
if (i == 5)
{
int product = 3 * i;
cout << product;
}
else
{
int product = 3 * i;
cout << product << setw(6);
}
}
cout << endl;

system("pause");

14. Write a program that asks the user to enter a salary and adds this
value to a departmental salary. The program should ask the user to enter
-1 when done entering data.
Hint: use bool more = true; to keep entering data. When the user enters
-1
if (salary == -1) more becomes false and the program sums the
total salaries and out puts Departmental Total Salary =
Ans:
#include<iostream>
using namespace std;
int main()
{
double sum = 0;
bool more = true;
while (more)
{
cout << "Enter a salary: ";
double salary;
cin >> salary;
if (salary == -1)
{
more = false;
cout << "Departmental Total Salary: " << sum << endl;
}
sum = sum + salary;
}
system("pause");
}