3.14 Assignments

Questions

1. What is a boolean variable?

2. What is the correct order of evaluation for boolean expressions?

3. Given the boolean variables p, q and r with p = TRUE, q = FALSE and r = TRUE, what are the values of the following BOOLEAN expressions?

	a. p AND q OR NOT r
	b. p OR q AND NOT r 
	c. p OR q AND q OR r 
	d. p AND r AND NOT q

4. Evaluate the following BOOLEAN expressions.

	a. (1 <= 5) OR (6 DIV 2 = 3)
	b. (3 + 5 < 9) AND (14 MOD 3 = 2)
	c. (5 # 7) & (2 DIV 5 < 1)
	d. (7 DIV 2 - 13 MOD 5 < 8) AND NOT ((2 = 9) OR (7 >= -3))
	e. (5 + 3 * 5 > 39) OR (-3 < 20 + 4 * -3)
	f. (NOT(5 + 4 > 2)) AND (5 MOD 1 < 1)

5. What could be improved in the statement sequence:

IF finished = TRUE
  THEN
    a := a + 1
  END;

6. What could be improved in the statement sequence:

flag := TRUE;
WHILE flag
  DO
    WriteString ("enter a cardinal here ==> ");
    ReadCard (myCard);
    flag := (ReadResult () = allRight);
  END;

7. What is wrong with the loop:

count := 11;
WHILE count # 0
  DO
    statement sequence;
    DEC (count, 2);
  END;

8. What is wrong with the loop:

REPEAT
  count := 7;
  statement sequence;
  DEC (count, 2);
UNTIL count < 0;

9. What is wrong with the loop:

realCount := 10.0;
REPEAT
  statement sequence;
  DEC (realCount, 2);
UNTIL realCount < 0;

10. What is wrong with the loop:

realCount := 10.0;
REPEAT
  statement sequence;
  realCount := realCount - 1.0;
UNTIL realCount = 0;

11. Show by writing a few lines of code in a skeletal framework, how to check the outcome of read operations in STextIO, SWholeIO and SRealIO

12. Show by writing a few lines of code in a skeletal framework, how one could import a variable called Done from each of two modules called RealInOut and InOut into the same program. Include code showing how the two are referred to in the program.

13. Check the manuals for your system, and write down how to do the following (where applicable):

a. obtain a printout of a program

b. obtain a "compile time listing" with statistical details on the lines, an index into the code compiled, and other details as may be available.

c. obtain a "hard copy" of a run of a program

d. list data files to the printer

14. Research and describe the "year 2000 problem."

15. List and discuss several common strategies for avoiding errors; for finding errors after they have occurred.

Problems:

16. Write a program that will either convert inches to centimetres or vice versa, depending on user input at run time.

17. Write a program that will compute the perimeter of either a square given one side, a triangle given all three sides, or a circle given the radius. The task is chosen by the user and the program must ask the appropriate questions in each case.

18. Expand the small section of code related to figure 3.2 into a full blown program that will ask for four marks, find their average, and then print appropriate pass/fail messages.

19. Modify the program SortTwo to sort three real numbers into order.

20. Modify the program AverageWordLength to improve the algorithm for determining the number of words. Add statements to keep track of and print out the number of words of 1, 2, 3...10, and more than 10, letters. Ensure that the statistical summary you print at the end is clear and easy to read.

21. The least common multiple (LCM) of two numbers is the smallest number that they both divide into. Write a program LCM to calculate this from two inputs. (Hint: research this first; there is a relationship between GCD and LCM.)

22. Extend the mini-calculator to include exponentiation (xy) as well as the option to add, subtract, multiply and divide.

23. Research, plan, write and test programs to compute

a. the sum of the first n odd numbers 1 + 3 + 5 + 7 +...+ 2n-1

b. the sum of the first n even numbers 2 + 4 + 6 + 8 + ... + 2n.

c. the sum of the first n squares 1 + 4 + 9 + 16 + ... + n2.

In each case, you must not use a loop to do the calculation.

24. Extend the GCD program further to allow for three input numbers; for n input numbers.

25. Modify #16 above to ask how many conversions are to be done and carry on from there.

26. Write a program which will read in a sequence of cardinals from the keyboard and then inform the user what is:

a. the largest of the numbers.

b. the smallest of the numbers.

c. the number of cardinals entered.

d. the total of the numbers.

e. the average of the numbers.

The user should be prompted to enter some specific value as a flag to terminate the sequence. (Hint: You could use zero for this, but a letter would work too, as it would set SIOResult.ReadResult() to a value other than allRight). The five pieces of information all need to be computed, but a menu should be given to the user and only the statistic that the user wants should be provided.

27. The Board of Governors at Trinity Western University has decided to grant a 3% pay raise, and they wish to know

a. what the new salaries will be

b. what the total new salary bill will be

c. how much extra money this will cost the University.

Write a program that will answer all their questions, given the following table of present salaries.

Amount	Number of faculty receiving this
$41 000		2
$41 300		4
$42 000		8
$42 100		6
$43 600		5
$43 000		2
$44 000		8
$44 500		2
$45 000		2
$45 300		1

28. Modify the solution to #27 so that those receiving less than $42 500 receive 4%, those between $42 501 and $43 999 receive 3% and those receiving $44 000 or more receive 2%. Compare the total cost of the two methods.

29. Write a program which prints out the decimal equivalents of the series of fractions 1/2, 1/4, 1/8, 1/16, etc. in a column. (You can't continue this forever, but do it for fourteen steps.) Now print a column beside this one with the sum of all the fractions printed up to that point. The numbers should start .5, .75, .875, etc. Because the intermediate results are wanted, one cannot dispense with a loop. Can the sum ever reach the total one? Why or why not?

30. Check your answer to #29 by writing a loop to add the first twenty terms in this sequence and print just the result. The first fifty. The first hundred. Hmmm.

31. Modify the Module BetterGradePoint to use reals for the points and to compute the grade point average of up to five subjects. You could use a nonexistent grade such as "Z" to indicate when the averaging is finished.

32. Add to #31 the ability to take into account the number of semester hours of credit that a course is worth, so that courses have their individual grade points weighted by the number of credits (0 - 5) that the course is worth. Add also the ability to give an extra third of a point for courses with marks like "A+" or "C+" and to subtract a third of a point for those with the corresponding "minus" letter grades.

33. Go back to Chapter 2, problem #38-39. Expand this program to handle addition, subtraction, and division as well as multiplication. The choice of which operation to do will be up to the user. In the case of division, the full long division algorithm is to be written out with all the steps shown, along with the quotient and remainder.

34. Using the GCD algorithm developed in this chapter, develop a program which will "reduce" common fractions and print out the result in the following form:

60      3
--- =  ---
100     5

35. Add to #34 the ability to read in the fraction to be reduced as a decimal numeral.

36. Write a program that accepts a positive cardinal and either produces its prime factors or states that it is a prime.

37. Write a program to process payroll data. Numeric input should consist of the hourly pay, number of hours at regular time, and number of hours overtime (paid at time-and-a-half.) Deduct 6% for pension, 2.5% for unemployment insurance, 20% for taxes, and $20 if the employee is in the stock sharing plan. Output should consist of a neat table showing hours, gross pay, deductions, and net pay.

38. Write a program to convert the units miles per gallon to the units litres per hundred kilometres and vice-versa. (Look up conversion factors.) Test the program by having a user input distances and fuel consumption in either metric, imperial (or U.S.) units, and producing both results as output.

39. Write a program that will find the sum of the digits of a number and then say if it is divisible by 9, by 3 but not 9, by neither.

40. The motion of an object under a constant acceleration a is given by the formula d = ut + .5at2, where d is the distance travelled, t is the time elapsed in seconds, and u is the initial velocity. Write a program that will take these inputs and print a table of values of distance vs time for a period of 20 seconds at intervals of one second; for a period of n seconds at intervals of Dn seconds.

41. Write a complement to the program DateCalc that accepts as input the year, and the day number within that year and produces the date.

42. Add to the consecutive date calculation program the ability to check for invalid dates. (Hint: convert a date, and convert it back. Does it equal what it started as or not?)

43. Look up Julian dates and write a program to accept as input a year, month, and day and compute the Julian date.

44. Add to #43 the ability to convert Julian dates to conventional dates.

45. Write a program that will format a date typed in by the user in one of three ways:

	January 9, 1950
	1950 01 09 (yyyy mm dd with leading zeros as needed)
	9/1/1950

46. Write a program to print out a calendar month given the year and month.

47. Write a program to compute simple interest where the data provided by the user is Principal, Annual Rate, and the starting and ending dates, which could be some years apart.


Contents