EXAM 1A – Fall 2017
A) (16 points) Show
what is printed by the following program:
#include<iostream>
using namespace std;
int main()
{
int officeCapacity
= 21;
int office = 0;
int workers = 62;
while (workers > 0)
{
cout << "Assigning #" << office + 1 << " office\n";
workers -= officeCapacity;
if (workers > 0)
cout << "Workers left: " << workers << endl;
office++;
}
return 0;
}
Assigning #1 office
Workers left: 41
Assigning #2 office
Workers left: 20
Assigning #3 office
Press any key to continue . . .
B) (16 points) What is printed by the
following code?
#include<iostream>
using namespace std;
int main()
{
bool discountAllowed
= true;
int items = 4;
double x = 4.5;
if (items > 0 && discountAllowed)
{
items = items *
2;
if (items < 10)
discountAllowed = false;
}
else if (items > 0)
{
items++;
}
cout
<< "Items: " << items << ' ' << items % 3 << ' ' << items / 3 << endl;
if (!discountAllowed
&& 6 - 1.5 == x || 6 + x == 0)
cout << "Good Day!";
else
cout << "See you later!";
system("PAUSE");
return 0;
}
Items: 8 2 2
Good Day!
C)
(48 points)Short Answer Questions:
When asked to write
code in this section, do not write full programs.
a) Use a loop: Write the C++ code that prints out the numbers from 1 to 100 and their squares (the number raised to the 2nd power). Your output should be (here are the first four lines, but there are 100 lines in the output):
1 1
2 4
3 9
4 16
.
.
.
.
.
int i;
for (i = 1; i <= 100; i++)
cout
<< i << ' '
<< i * i << endl;
b) Write the C++ code (not a full program!!!) that computes a worker’s salary. The code prompts the user for number of hours worked and salary per hour. It prints the total check for the user. If the user works more than 35 hours, the user gets a bonus of $2 for every hour worked. Remember to declare variables
int hours;
double salPerHour;
double total;
cout << "Enter
number of hours and salary per hour: ";
cin >>
hours >> salary;
total = salary * hours;
if (hours > 35)
total += 2 * hours; // add
2 for every hour worked as bonus
cout << total;
c) Write the C++ code that (50 times!) keeps prompting the user to enter an integer! The code prints out the sum of all the integers that are EVEN.
int sum = 0;
int i;
int num;
for (i = 0; i < 50; i++)
{
cout
<< "Enter a number";
cin
>> num;
if (num % 2 == 0)
sum += num; //this is the line that adds the num
into sum
}
cout << "sum of all even numbers: " << sum;
d) Given:
char letter, code;
Write the C++ code that prints out "BOTH ARE!" if letter and code both contain upper case letters (A-Z) in the alphabet. Otherwise, the code
prints out: "NO!"
if
(letter >= 'A' && letter <= 'Z'
&& code >= 'A' && code <= 'Z')
cout
<< "BOTH ARE!";
else
cout << "NO!";
D) (20 points) Write a full program, including comments, that computes the final cost for a cell phone bill.
AT&T has two choices for unlimited plans: Unlimited
Choice and Unlimited Plus. Both unlimited plans allow up to 10 lines. For two
lines, the Unlimited Choice plan starts at $125 per month, and Unlimited Plus
starts at $155. Each additional line beyond the first two is an extra $20 per
month. Federal Universal Service Fund fees are 18.8% (of the total) additional
charge.
Write code that prompts the user for the plan (choice or
plus), and prompts the user for the number of lines. The code prints out the
total amount due each month.
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
char plan;
int
lines;
double total;
cout << "Enter
number of lines: ";
cin >>
lines;
cout << "Enter
plan type (P for plus and c for choice): ";
cin >>
plan;
if (plan == 'c')
total = 125; //base
price for 2 lines
else //plan
is p
total = 155; //base
price for 2 lines
if (lines > 2) //need
to charge for the extra lines
total += ((lines - 2) * 20); //$20
for every extra line over 2
//add federal fee
total *= 1.188; //same
as total = total + total * .188;
cout << setprecision(2) << total << endl;
return 0;
}