IC210 AY16 Final Practicum Exam

Practicum Rules

How to submit a solution

You will be using the submit script to submit your practicum solutions.
  1. To submit your first solution give the command:
    submit p1prFinal p1prFinal.cpp
  2. To submit your second solution give the command:
    submit p2prFinal p2prFinal.cpp
  3. To submit your third solution give the command:
    submit p3prFinal p3prFinal.cpp

Problem 1

Write a program p1prFinal.cpp that reads in a file describing colors, where each color is defined by a line of the form
inA.txt colors in
original order
inA.txt colors
ordered by brightness
 
 
 
 
 
 
 
 
 
 
 
 
 
 
	CornflowerBlue 100,149,237 #6495ED
         ----------     |   |   |  -------  
            name       red  |  blue   hex
                          green
... where the only spaces are beween the name and the rgb triple, and between the rgb triple and the hex value. The rgb values are integers in the range 0..255 inclusive. The first line of the file is of the form N=number, where number is the number of colors listed in the file.

Your program should print out the colors in order of decreasing brightness, where brightness is defined as the average of the rgb values. In the above example, CornflowerBlue has an brightness of (100+149+237)/3 = 162.0. Black (rgb value 0,0,0) has brightness 0, so it is the least bright color possible ... which should make some sense. If colors have the same brightness, they should appear alphabetically by name.

Note: here are some input files: inA.txt, inB.txt, inC.txt, inD.txt, inE.txt.

S A M P L E    R U N S
~/$ ./p1
> inA.txt
LightYellow 255,255,224 #FFFFE0
Gainsboro 220,220,220 #DCDCDC
Aqua 0,255,255 #00FFFF
DarkGray 169,169,169 #A9A9A9
Tomato 255,99,71 #FF6347
YellowGreen 154,205,50 #9ACD32
DimGray 105,105,105 #696969
~/$ ./p1
> inE.txt
Ivory 255,255,240 #FFFFF0
MintCream 245,255,250 #F5FFFA
Lavender 230,230,250 #E6E6FA
LemonChiffon 255,250,205 #FFFACD
LightGoldenRodYellow 250,250,210 #FAFAD2
Cyan 0,255,255 #00FFFF
Fuchsia 255,0,255 #FF00FF
Magenta 255,0,255 #FF00FF
~/$ ./p1
> inB.txt
Lavender 230,230,250 #E6E6FA
LightGoldenRodYellow 250,250,210 #FAFAD2
BlanchedAlmond 255,235,205 #FFEBCD
Pink 255,192,203 #FFC0CB
PaleGoldenRod 238,232,170 #EEE8AA
LightSkyBlue 135,206,250 #87CEFA
Silver 192,192,192 #C0C0C0
PaleGreen 152,251,152 #98FB98
Orchid 218,112,214 #DA70D6
SandyBrown 244,164,96 #F4A460
RosyBrown 188,143,143 #BC8F8F
SlateBlue 106,90,205 #6A5ACD
SpringGreen 0,255,127 #00FF7F
DarkSlateBlue 72,61,139 #483D8B
SeaGreen 46,139,87 #2E8B57
Brown 165,42,42 #A52A2A
~/$ ./p1
> inC.txt
Ivory 255,255,240 #FFFFF0
LavenderBlush 255,240,245 #FFF0F5
BlanchedAlmond 255,235,205 #FFEBCD
NavajoWhite 255,222,173 #FFDEAD
Wheat 245,222,179 #F5DEB3
Plum 221,160,221 #DDA0DD
SkyBlue 135,206,235 #87CEEB
Orchid 218,112,214 #DA70D6
MediumTurquoise 72,209,204 #48D1CC
Tomato 255,99,71 #FF6347
DarkOrchid 153,50,204 #9932CC
SlateBlue 106,90,205 #6A5ACD
MediumVioletRed 199,21,133 #C71585
RebeccaPurple 102,51,153 #663399
DarkCyan 0,139,139 #008B8B
DarkSlateGray 47,79,79 #2F4F4F

Problem 2

This program involves a simple process that takes an initial value a, and modulus b, and produces an infinite sequence of numbers starting with x = a and applying the following "step"
xnew = (xold * a) % b
So, for example, if a = 58 and b = 72
      step 1          step 2          step 3          step 4          step 5          step 6
58 -----------> 52 -----------> 64 -----------> 40 -----------> 16 -----------> 64 -----------> 40 -----------> ...
    (58*58)%72      (52*58)%72      (64*58)%72      (40*58)%72      (16*58)%72      (64*58)%72
i.e. 58*a  % b       52*a  % b       64*a  % b       40*a  % b  ...
For this problem, we want to know how many "steps" it takes until we see our first repeated value. So, for the example a = 58 and b = 72 shown above, we see that after 5 steps we see our first repeated value (64).

Write a program p2prFinal.cpp that reads integers a and b, 1 < a < b, and prints out the number of steps of the above process until the first repeated value is seen.
NOTE: You may make no assumptions about the number of steps that will be required!

S A M P L E    R U N S
~/$ ./p2
> 58 72
repeat after 5 steps    
~/$ ./p2
> 12 89
repeat after 8 steps    
~/$ ./p2
> 3 17
repeat after 16 steps    
~/$ ./p2
> 7971 13977
repeat after 777 steps    
~/$ ./p2
> 5 13977
repeat after 4656 steps    





Check Out These Illustrative Examples:  
with a = 2, b = 15 
initial = 2
step 1 -> 4
step 2 -> 8
step 3 -> 1
step 4 -> 2
1st repeat is 2
with a = 2, b = 16 
initial = 2
step 1 -> 4
step 2 -> 8
step 3 -> 0
step 4 -> 0
1st repeat is 0
with a = 2, b = 20 
initial = 2
step 1 -> 4
step 2 -> 8
step 3 -> 16
step 4 -> 12
step 5 -> 4
1st repeat is 4
with a = 5, b = 7 
initial = 5
step 1 -> 4
step 2 -> 6
step 3 -> 2
step 4 -> 3
step 5 -> 1
step 6 -> 5
1st repeat is 5

Problem 3

Write a program p3prFinal.cpp that tracks transfers between bank accounts, and reports balances. It exits as soon as someone goes bankrupt, reporting the name of that person. Here are the details:

  1. The program starts by reading a filename from the user, then reads information about accounts from that file. Samples are inX.txt, and inY.txt. Here's the simplest example:
    4 accounts
    87623 Joe Cool $890.76
    45672 Dan Roche $123.45
    98783 Ric Crabbe $1209.78
    67878 Chris Brown $72.48
  2. After reading in the file, the program allows for two user commands:
    • show acctNum
      This prints the information about account acctNum in the exact same format as used in the input file. You may assume the user input is properly formatted, but if theaccount number is bad (i.e. no acccount has that number) the program simply prints "Error!" and moves on to the next command.
    • transfer $amount acctNum1 to acctNum2
      This does what it says: transfers $amount from acctNum1 to acctNum2. You may assume the user input is properly formatted, but if either account number is bad (i.e. no acccount has that number) the program simply prints "Error!" and moves on to the next command. If the account acctNum1 doesn't have enough money to make the transfer, print the message "firstname lastname is bankrupt!" and quit the program.

S A M P L E    R U N S
~/$ ./p3                           
inX.txt
> show 45672
45672 Dan Roche $123.45
> show 67878
67878 Chris Brown $72.48
> show 31415
Error!
> transfer $100 45672 to 67878
> show 45672
45672 Dan Roche $23.45
> show 67878
67878 Chris Brown $172.48
> transfer $100 45672 to 67878
Dan Roche is bankrupt!
~/$ ./p3                           
inY.txt
> transfer $200 223 to 198
Error!
> show 223
Error!
> show 222
222 Aretha Franklin $387.87
> transfer $200 222 to 198
> show 222
222 Aretha Franklin $187.87
> transfer $200 222 to 198
Aretha Franklin is bankrupt!