Docsity

Solutions to Algorithm Exercises - Computer Architecture | ECE 1331, Exams of Electrical and Electronics Engineering

Toggle sidebar
bg1
Solutions to Algorithm Exercises
Exercise 1: (*Directions for passing an exam--problem was the order of the steps*)
1.Read the material
2.Listen attentively in class
3.Outline the material
4.Get lots of sleep the night before the exam
5.Take the exam
Exercise 2: Nonalgorithm because it may not solve the problem. There are two
errors. First, an additional step needs to be added to provide for “looping” back
if the word sought is still before the current page. Secondly, if the search
begins on an even page, only even pages will be scanned; if search begins on an
odd page then only odd pages will be scanned. Thus an algorithm would be:
1.Turn to any page.
2.If word at top left of the page occurs alphabetically after the desired
word, go to step 4.
3.Turn ahead 10 pages and go to step 2.
4.Go back 1 page.
5.If word at the top right is before the desired word, go to step 3.
6.If word at the top left is after the desired word, go to step 4.
7.Scan page for desired word.
8.If it is found, write the definition.
9.If it is not found, write “not in the dictionary”.
Exercise 3: (*Find sum of positive integers between M and N*)
1.Input values for M and N.
2.Set sum1 = N(N+1)/2 (*Sum of integers from 1 to N*)
3.Set sum2 = M(M-1)/2 (*Sum of integers from 1 to M-1*)
4.Answer is sum1-sum2
Exercise 4:(Flowchart for Exercise 3--be sure you are in PageLayout View on Word)
pf3
pf4
pf5

Partial preview of the text

Download Solutions to Algorithm Exercises - Computer Architecture | ECE 1331 and more Exams Electrical and Electronics Engineering in PDF only on Docsity!

Solutions to Algorithm Exercises

Exercise 1: (Directions for passing an exam--problem was the order of the steps) 1.Read the material 2.Listen attentively in class 3.Outline the material 4.Get lots of sleep the night before the exam 5.Take the exam Exercise 2: Nonalgorithm because it may not solve the problem. There are two errors. First, an additional step needs to be added to provide for “looping” back if the word sought is still before the current page. Secondly, if the search begins on an even page, only even pages will be scanned; if search begins on an odd page then only odd pages will be scanned. Thus an algorithm would be: 1.Turn to any page. 2.If word at top left of the page occurs alphabetically after the desired word, go to step 4. 3.Turn ahead 10 pages and go to step 2. 4.Go back 1 page. 5.If word at the top right is before the desired word, go to step 3. 6.If word at the top left is after the desired word, go to step 4. 7.Scan page for desired word. 8.If it is found, write the definition. 9.If it is not found, write “not in the dictionary”. Exercise 3: (Find sum of positive integers between M and N) 1.Input values for M and N. 2.Set sum1 = N(N+1)/2 (Sum of integers from 1 to N) 3.Set sum2 = M(M-1)/2 (Sum of integers from 1 to M-1) 4.Answer is sum1-sum Exercise 4:(Flowchart for Exercise 3--be sure you are in PageLayout View on Word)

sum1N(N+1)/ sum2M(M-1)/ sumsum1-sum Start Read M,N Write “The sum is”,sum Stop

  1. Input value
  2. If end-of-file, output “error--no values to input” and stop.
  3. Initialize maxvalue,minvalue,count1,sum  value
  4. Input value
  5. While not end-of-file do a. count  count+ b. sum  sum+value c. If value>max, then max value d. If value<min, then min value e. Input value
  6. range max-min
  7. average  sum/count
  8. output max,min,range, average Exercise 8: (*To determine the minimum number of coins to make up a given amount of money. Input- Amount of money in dollars Output - Number of coins required *)
    1. Input amount
    2. amount  amount100 (to convert amount to cents*)
    3. nq Integer(amount/25), amountamount-25nq (find # quarters*)
    4. nd Integer(amount/10), amountamount-10nd (find # dimes *)
    5. nnInteger(amount/5), amountamount-5nn (find # nickels*)
    6. npamount
    7. Numbercoinsnq+nd+nn+np
    8. Output Numbercoins Exercise 9: 4.Pseudocode trace A. c B. t C. m = numberofvalues or nval; c = valuecount or vcnt; n = value; t = root D. 4 passes the test 5 does not pass the test 16 passes the test 10 does not pass the test 9 passes the test E. is it a perfect square? F. (see next page)

c 1 start Read m cc+1 cm?^ no yes Read n t 1 while tt < n tt+ yes no tt=n? yes no Write n, "does not pass the test" Write n, "passes the test" stop