Title: Assignment
1Assignment 4
2PSEUDOCODEProblem 3.34
- Use flip function to simulate flipping a coin
- Present a summary in terms of number of heads and
number of tails - Error proof by using a default switch with a
resting on rim message
3FlowchartExercise 3.34
start
Call flip
True
Counter lt 100
False
Add 1 to heads
True
Side 1
summarize
False
End
Add 1 to tails
Add 1 to counter
4Source CodeProblem 3.34
//Noorisha Nawaz // Solution to assignment
3.34 // Tossing a two-sided coin 100
times include ltiostream.hgt include
ltiomanip.hgt include ltstdlib.hgt void main
() int head 0 int tail 0 for ( int
toss 0 toss lt 100 toss ) int
flip() switch ( flip() ) case
1 head cout ltlt "heads" ltlt"\t" break c
ase 2 tail cout ltlt "tails"
ltlt"\t" break default cout ltlt "Standing on
the rim!"
cout ltlt "Tossing the coin 100 tomes has resulted
in\n " cout ltlt setw( 13 ) ltlt head ltlt "
heads\n" ltlt endl cout ltlt setw( 13 ) ltlt tail
ltlt " tails\n" ltlt endl if((headtail) lt
100) cout ltlt "and landed on the rim " ltlt (100
- (headtail) ) ltlt " times!"ltltendl return
int flip() int face 0 face 1
(rand() 2) return face
5OutputProblem 3.34
6Problem 3.36
- Computer Assisted Instruction
- Prints Appropriate Responses for Right and Wrong
Anwsers
7PSEUDOCODEProblem 3.36
- Computer Assisted Instruction
- Prints Appropriate Responses for Right and Wrong
Anwsers - Uses switch structure to randomize encouragement
8Source CodeProblem 3.36
//Noorisha Nawaz //CAI prolem //Solution to
assignment 3.36 include ltiostream.hgt include
ltstdlib.hgt include lttime.hgt void
correctMessage( void ) void incorrectMessage(
void ) void multiplication( void ) int
main() srand( time( 0 ) )
multiplication() return 0
void correctMessage( void ) switch ( rand()
4 ) case 0 cout ltlt "Very
good!" break case 1
cout ltlt "Excellent!" break case
2 cout ltlt "Nice work!"
break case 3 cout ltlt "Keep up
the good work!" break cout ltlt
"\n\n"
void incorrectMessage( void ) switch (
rand() 4 ) case 0 cout ltlt
"No. Please try again." break
case 1 cout ltlt "Wrong. Try once
more." break case 2
cout ltlt "Don't give up!" break
case 3 cout ltlt "No. Keep trying."
break cout ltlt "\n? "
9Source CodeProblem 3.36 Page 2
void multiplication() int x, y, response
0 while ( response ! -1 ) x
rand() 10 y rand() 10 cout
ltlt "How much is " ltlt x ltlt " times " ltlt y
ltlt " (-1 to End)? " cin gtgt response
while ( response ! -1 response ! x y )
incorrectMessage() cin gtgt
response if ( response ! -1)
correctMessage() cout
ltlt "Come Back Soon!" ltlt endl
10OutputProblem 3.36
11PSEUDOCODEProblem 4.2
- Check if there are seats remaining
- Request preference (smoking /non smoking
section) - If seats of requested preference remain assign a
seat - If requested section is sold out offer seat in
other section - If accepted assign the seat
- else suggest the next shuttle
- If no seats remain, apologize and suggest next
shuttle
12Source CodeProblem 4.2
//Noorisha Nawaz //Solution to Assignment 4
Exercise 4.20 //flight reservation
problem include ltiostream.hgt include
ltctype.hgt void main() const int
seats 10 int plane3 0 // array
with smoking seats, nonsmoking and total int
preference //smoking preference int
nonsmokers 10 //non smoking seats beginning in
back int smokers 1 //smoking beginning in
front while(plane2 lt seats ) cout
ltlt "\n\n\t\tWelcome to Mile High
Airlines.\n\n\n\n"ltltendl cout ltlt "Would you
prefer smoking, or non smoking?\n" ltlt "1
for smoking, 2 for non smoking\n"ltltendl cin
gtgt preference
13Source CodeProblem 4.2
//smoking if (preference 1 )
if(plane0lt5) plane0
plane2 cout ltlt "Your assigned seat is
"ltlt smokers ltltendl smokers
else cout ltlt "All smoking seats
are full.\n" ltlt "Enter 2 to accept a
non-smoking seat?\n"ltltendl cout ltlt "Enter
0 if you would rather wait for the next
flight\n"ltltendl cin gtgt preference
14Source CodeProblem 4.2
//nonsmoking if (preference 2)
if(plane1lt5)
plane1 plane2 cout ltlt "Your
assigned seat is "ltlt nonsmokers ltltendl
nonsmokers-- else cout
ltlt "All non-smoking seats are full.\n" ltlt
"Please book a smoking seat\n"ltltendl
for(int j 0 j lt 1000000 j)
if(plane2seats) cout ltlt
"Sorry! All seats sold out.\n" cout ltlt "Next
shuttle in 3 hours\n\n\n\n\n\n\n\n\n"ltltendl
return
15OutputProblem 4.2
16Problem 4
- Problem 4 - 22
- Sales Slips
- Prompts for Salespersons ID, Product ID and Sales
of the Product. - Returns a Formatted Array listing sales for the
Salesperson and Product and Total Sales
17PSEUDOCODEProblem 4
- Define array productArray
- Read salesperson, product and value
- while salesperson is not equal to -1 scale
person and product enter value into ProductArray
person product enter person, product and
value - Add up array person and array product
- Print array with headings ands totals
18Flow ChartProblem 4.22
Begin
Enter person, product and value
Move value to person product
Person -1
Total array rows and columns
Print array with headings and totals
End
19Source CodeProblem 4.22
cout ltlt "\nThe total sales for each salesperson
are displayed" ltlt " at the end of each
row,\n" ltlt "and the total sales for" ltlt "
each product are displayed at the bottom of
each\n" ltlt "column.\n " ltlt setw( 12 ) ltlt
1 ltlt setw( 12 ) ltlt 2 ltlt setw( 12 ) ltlt 3
ltlt setw( 12 ) ltlt 4 ltlt setw( 12 ) ltlt 5 ltlt setw( 13
) ltlt "Total\n" ltlt setiosflags( iosfixed
iosshowpoint ) for ( int i 1 i lt
people i ) totalSales 0.0
cout ltlt i for ( int j 1 j lt
products j ) totalSales sales i
j cout ltlt setw( 12 ) ltlt
setprecision( 2 ) ltlt sales i j
productSales j sales i j
cout ltlt setw( 12 ) ltlt setprecision( 2 ) ltlt
totalSales ltlt '\n' cout ltlt "\nTotal"
ltlt setw( 8 ) ltlt setprecision( 2 ) ltlt
productSales 1 for ( int j 2 j lt
products j ) cout ltlt setw( 12 ) ltlt
setprecision( 2 ) ltlt productSales j cout
ltlt endl return
//Noorisha Nawaz // Answer to Assignment 4.22
//Sales Slips include ltiostream.hgt include
ltiomanip.hgt void main() const int people
5 const int products 6 float sales
people products 0.0 float value
float totalSales float productSales
products 0.0 int salesPerson int
product cout ltlt "Enter the salesperson (1
- 4), product number (1 - 5), " ltlt "and
total sales.\nEnter -1 for the salesperson"
ltlt " to end input.\n" cin gtgt salesPerson
while ( salesPerson ! -1 ) cin gtgt
product gtgt value sales salesPerson
product value cin gtgt salesPerson
20OutputProblem 4.22