summaryrefslogtreecommitdiff
path: root/assignments/1.md
blob: 4247f105da43378ad0840ef41ae72f1a1fafe2c3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# Assignment 1 – choose ONE exercise each from Chapters 2 and 3

## Chapter 2: Exercises

1. Write pseudocode instructions to carry out each of the following
   computational operations:
  * Determine the area of a triangle given values for the base `b` and the height `h`.
  * Compute the interest earned in 1 year given the starting account balance B
     and the annual interest rate / and assuming simple interest, that is, no
     compounding. Also determine the final balance at the end of the year.
  * Determine the flying time between two cities given the mileage M between
    them and the average speed of the airplane.
2. Using only the sequential operations described in Section 2.2.2, write an
   algorithm that gets values for the starting account balance B, annual
   interest rate I, and annual service charge S. Your algorithm should output
   the amount of interest earned during the year and the final account balance
   at the end of the year. Assume that interest is compounded monthly and the
   service charge is deducted once, at the end of the year.
3. Using only the sequential operations described in Section 2.2.2, write an
   algorithm that gets four numbers corresponding to scores received on three
   semester tests and a final examination. Your algorithm should compute and
   display the average of all four tests, weighting the final exam twice as
   heavily as a regular test.
4. Write an algorithm that gets the price for item A plus the quantity
   purchased. The algorithm prints the total cost, including 6% sales tax.
5. Write an if/then/else primitive to do each of the following operations:
  * a. Compute and display the value `x / y` if the value of `y` is not `0`. if
    `y` does have the value `0`, then display the message `Unable to perform the
    division.`
  * b. Compute the area and circumference of a circle given the radius `r` if
    the radius is greater than or equal to `1.0`; otherwise, you should compute
    only the circumference.
6. Modify the algorithm of Exercise 2 to include the annual service charge only
   if the starting account balance at the beginning of the year is less than
   $1,000. If it is greater than or equal to $1,000, then there is no annual
   service charge.
7. Write an algorithm that uses the `loop` (1) to input 10 pairs of numbers,
   where each pair represents the score of a football game with the Computer
   State University (CSU) score listed first, and (2) for each pair of numbers,
   determine whether CSU won or lost. After reading in these 10 pairs of values,
   print out the won/lost/tie record of CSU. In addition, if this record is
   perfect 10-0, then print out the message `Congratulations on your undefeated
   season.`
8. Modify the test-averaging algorithm of Exercise 3 so that it reads in 15 test
   scores rather than 4. There are 14 regular tests and a final examination,
   which counts twice as much as a regular test. Use a loop to input and sume
   the scores.
9. Modify the sales computation algorithm of Exercise 4 so that after finishing
   the computation for one item, it starts on the computation for the next. This
   iterative process is repeated until the total cost exceeds $1,000.
10. Write an algorithm that is given your electric meter readings (in
    kilowatt-hours) at the beginning and end of each month of the year. The
    algorithm determines your annual cost of electricity on the basis of a
    charge of 6 cents per kilowatt-hour for the first 1,000 kilowatt-hours of
    each month and 8 cents per kilowatt-hour beyond 1,000. After printing out
    your total annual charge, the algorithm also determines whether you used
    less than 500 kilowatt-hours for the entire year and, if so, prints out a
    message thank you for conserving electricity.
11. Develop an algorithm to compute gross pay. The inputs of your algorithm are
    the hours worked per week and the hourly pay rate. The rule for determining
    gross pay is to pay the regular pay rate for all hours worked up to 40,
    time-and-a-half for all hours over 40 up to 54, and double time for all
    hours over 54. Compute and display the value for gross pay using this rule.
    After displaying one value, ask the user whether he or she wants to do
    another computation. Repeat the entire set of operations until the user says
    no.
12. Develop a formal argument that "proves" that the sequential search algorithm
    shown in Figure 2.13 cannot have an infinite loop; that is, prove that it
    will always stop after a finite number of operations.
13. Modify the sequential search algorithm of Figure 2.13 so that it works
    correctively even if the names in the directory are not unique, that is, if
    the desired name occurs more than once. Your modified algorithm should find
    every occurrence of NAME in the directory and print out the telephone number
    corresponding to every match. In addition, after all the numbers have been
    displayed, your algorithm should print out how many occurrences of NAME were
    located. For example, if NAME occurred three times, the output of the
    algorithm might look something like this:

        528-5638
        922-7874
        488-2020
14. Use the Find Largest algorithm of Figure 2.14 to help you develop an
    algorithm to find the median value in a list containing `N` unique numbers.
    The median of `N` numbers is defined as the value in the list in which
    approximately half the values are larger than it and half the values are
    smaller than it. For example, consider the following list of seven number:

      25, 50, 83, 44, 91, 20, 55

    The median value is 50 because three values (20, 26, 44) are smaller and
    three values (55, 83, and 91) are larger. if `N` is an even value, then the
    number of values larger than the median will be one greater than the number
    of values smaller than the median.
15. With regard to the Find Largest algorithm of Figure 2.14, if the numbers in
    our list were not unique and therefore the largest number could occur more
    than once, would the algorithm find the first occurrence? The last
    occurrence? Every occurrence? Explain precisely how this algorithm would
    behave when presented with this new condition.
16. On the sixth line of the Find Largest algorithm of Figure 2.14, there is an
    instruction that reads, `while(i<=n) do`. Explain exactly what would happen
    if we changed that instruction to read as follows:
    * a. `while(i>=n) do`
    * b. `while(i<n) do`
    * c. `while(i==n) do`
17. On the seventh line of the Find Largest algorithm of Figure 2.14, there is
    an instruction that reads `if A > largest so far then ...`. Explain exactly
    what would happen if we changed that instruction to rea as follows:
    * a. `if A >= largest so far then...`
    * b. `if A < largest so far then...`

    Looking back at your answers in Exercises 16 and 17, what do they say about
    the importance of using the correct relational operations (<,==,>,>=,<=,!=)
    when writing out either an iterative or conditional algorithmic primitive?
18. Refer to the pattern-matching algorithm in Figure 2.16.
  * a. What is the output of the algorithm as it currently stands if our text is
    `We must band together and handle adversity` and we search for `and`?
  * b. How could we modify the algorithm so that it finds only the complete word
    `and` rather than the occurrence of the character sequance `a`, `n` and `d`
    that is contained within another word, such as `band`?