Rev B Lab4
1) Which of the following are illegal
identifier names, and why?
a) hello
b) int
c) good-bye
d) why_dont_you_continue
e) 1outOfFive
f) payRate
g) x
h) gr8
i) hi!
2) What is printed by the following
program?
#include<iostream>
using namespace std;
int main()
{
cout
<< "$$$" << '\t' << "&&&"
<< '\n';
cout
<< endl << endl
<< endl;
cout
<<'$'<<" * " << '\n';
cout
<<'a'<<'t';
cout
<<"good-bye!";
return 0;
}
3) Evaluate the following expressions:
a) 6 + 8 % 3
b) (7 – 15) + 62 – 4 / 3
c) 22.4 / 4 – 6.3 * 2
d) 15.46/2
e) 14/3-3%2
f) 14/3%3
Be sure to include comments at the beginning of the program with your name, date etc, and comments throughout the program to explain your code. Use const whenever appropriate.
4) Write a program to compute and print the cost of airline tickets from Boston to New York.
Your program prompts the user to input a ticket price.
Your program prompts the user to input the number of tickets to be bought.
Due to a membership in travel clubs you will receive a 13% discount.
There is 8.75% tax on the price of the tickets plus a airport charge of $15.
Your program should print out the total amount due in the following format (you may use spaces or the tab for the white space, which does not have to be a specific number of spaces), with the following input.
# of tickets: 5 @ $178.00
subtotal: <subtotal here>
amount of discount: <amount of discount>
total without tax: <total without tax>
tax: <tax>
airport charge: $15
total due: <total due>
Run your program on two more sets of input:
6 tickets at $150 each
10 tickets at $105.60 each
Print and submit all source code and 3 printouts of output
5)
If x=5,
y=6, z=4 and w=3.5evaluate each of the following statements, if possible. If not possible, state the reason.
a)
(x+z)%y=
b)
(x+y)%w=
c)
(y+w)%x=
d)
(x+y)*w=
e)
(x5y)%z=
f)
(y%z)%x=
g)
(x*z)%y=
h)
((x*y*w)*z=
6)
Write
the following as a C++ expression.
(-b+(b2-4ac))/2a=
7)
What is
the output of the following statements?
Suppose a and b are int
variables, c is a double variable, and a =13, b=5, and c=17.5.
a)
cout<<a/static_cast <double>( b )+2*c<<endl;
b)
cout<< static_cast <int>( c
)%5+a-b<<endl;
8)
Malik, any
edition, chapter 2, problem 24
SOLUTIONS
4)
/*Lab4 exercise4 Computes and prints cost of airline tickets*/
#include <iostream>
#include <iomanip>
using namespace std;
const double Discount=0.13;
const double Tax=0.0875;
const double AirportCharge=15.00;
int main()
{
double
numberTickets,ticketPrice,subtotal;
cout<<"number
of tickets purchased ";
cin>>numberTickets;
cout<<endl;
cout<<"ticket
price $";
cin>>ticketPrice;
cout<<endl<<endl;
cout<<fixed<<showpoint<<setprecision(2);
cout<<"#
of tickets:" <<'\t'<<numberTickets<<'\t'<<"@"<<'\t'<<'$'
<<ticketPrice<<endl<<endl;
subtotal=numberTickets*ticketPrice;
cout<<"subtotal"<<'\t'<<'$'<<subtotal<<endl;
double
discountAmount=Discount*subtotal;
cout<<"amount
of discount"<<'\t'<<'$'<<discountAmount<<endl;
double
totalBeforeTax=subtotal-discountAmount;
cout<<"total
without tax"<<'\t'<<'$'<<totalBeforeTax<<endl;
double
tax=Tax*totalBeforeTax;
cout<<"tax:"<<tax<<endl;
cout<<"airport
charge"<<'\t'<<'$'<<AirportCharge<<endl;
cout<<"total
due"<<'\t'<<'$'<<totalBeforeTax-tax-AirportCharge<<endl;
return(0);
}