# Difference between revisions of "Solution 3"

Line 98: | Line 98: | ||

{|style=color-backgroud="white" cellspacing="20" | {|style=color-backgroud="white" cellspacing="20" | ||

|[[Programming_Assignment_3|previous]] | |[[Programming_Assignment_3|previous]] | ||

− | |[[ | + | |[[Contents|contents]] |

|[[Procedures|next]] | |[[Procedures|next]] | ||

|} | |} |

## Revision as of 22:25, 25 November 2007

3Ea - Solutions (author: Tao Yue, state: unchanged)

**Solution to Fibonacci Sequence Problem**

1: (* Author: Tao Yue 2: Date: 19 July 1997 3: Description: 4: Find the first 10 Fibonacci numbers 5: Version: 6: 1.0 - original version 7: *) 8: 9:programFibonacci;10: 11:var12: Fibonacci1,Fibonacci2:integer;13: temp:integer;14: count:integer;15: 16:begin(* Main *) 17: writeln('First ten Fibonacci numbers are:');18: count:=0;19: Fibonacci1:=0;20: Fibonacci2:=1;21:repeat22:write(Fibonacci2:7);23: temp:=Fibonacci2;24: Fibonacci2:=Fibonacci1+Fibonacci2;25: Fibonacci1:=Temp;26: count:=count+1 27:untilcount=10;28: writeln;29: 30: (* Of course, you could use a FOR loop or a WHILE loop 31: to solve this problem. *) 32: 33:end.(* Main *)

**Solution to Powers of Two Problem**

1: (* Author: Tao Yue 2: Date: 13 July 2000 3: Description: 4: Display all powers of two up to 20000, five per line 5: Version: 6: 1.0 - original version 7: *) 8: 9:programPowersofTwo;10: 11:const12: numperline=5;13: maxnum=20000;14: base=2;15: 16:var17: number:longint;18: linecount:integer;19: 20:begin(* Main *) 21: writeln('Powers of ',base,', 1 <= x <= ',maxnum,':');22: (* Set up for loop *) 23: number:=1;24: linecount:=0;25: (* Loop *) 26:whilenumber<=maxnumdo27:begin28: linecount:=linecount+1;29: (* Print a comma and space unless this is the first 30: number on the line *) 31:iflinecount>1then32:write(', ');33: (* Display the number *) 34:write(number);35: (* Print a comma and go to the next line if this is 36: the last number on the line UNLESS it is the 37: last number of the series *) 38:if(linecount=numperline)andnot(number*2>maxnum)then39:begin40: writeln(',');41: linecount:=0 42:end;43: (* Increment number *) 44: number:=number*base;45:end;(* while *) 46: writeln;47: 48: (* This program can also be written using a 49: REPEAT..UNTIL loop. *) 50: 51:end.(* Main *)

Note that I used three constants: the base, the number of powers to display on each line, and the maximum number. This ensures that the program can be easily adaptable in the future.

Using constants rather than literals is a good programming habit to form. When you write really long programs, you may refer to certain numbers thousands of times. If you hardcoded them into your code, you'd have to search them out. Also, you might use the same value in a different context, so you can't simply do a global Search-and-Replace. Using a constant makes it simpler to expand the program.

Also note that I used the `longint` type for the number variable. This is because to fail the test `number <= 20000`, `number` would have to reach `32768`, the next power of two after `16384`. This exceeds the range of the integer type: `-32768` to `32767`. (try it without `longint` and see what happens)

previous | contents | next |