Computer Programming

A few months ago I was talking to David about computers and trying to explain what exactly binary was. He had heard me mention that computers used 1's and 0's to store and send information but he didn't understand how they could. So I started trying to teach him how we use base10 numbering (probably because we have 10 fingers) and that binary (base2) is just a different way to show the same numbers. After several minutes and many questions I think he almost understood it because then he asked if there were other numbering systems, specifically he wanted to know if there was one based on 3. I told him that you could have a number system based on any number you wanted and that another system used by computers was called hexidecimal which is just a different name for base16. Well, I hadn't heard anything about it since that day so I thought he had forgotten.

This summer we have been talking more with David about what kind of job he wants to get when he grows up and the career he has decided to try for right now is Video Game Programmer. We have talked with him and tried to get him to think about what things he would need to start learning if he really wanted to do that. One of the things we mentioned was that he should try doing some computer programming to see if he really liked it. They don't offer that at his school so he asked if I would teach him. This week I have started teaching him some basic programming skills using Pascal and we will then move on to C in a week or two (once we receive some books I ordered).

That brings me back to the base3 numbering system. I asked him what kind of program he wanted to write and gave him examples of simple games like Rock, Paper, Scissors or Guess a Number Between 1 and 10. He asked if we could make a program to convert a base10 number to base3. So that's what we did. Here is the program:


program base3 (input, output);

label user_input;

var x,y,z,a,b,c,d : integer;
begin
user_input:
writeln('Enter a number between 0 and 80:');
readln(x);
write('You entered ');
writeln(x);
if x > 80 then begin
writeln('Invalid number entered.');
writeln();
goto user_input;
end else if x < 0 then begin
writeln('Invalid number entered.');
writeln();
goto user_input;
end;
a := x div 27;
y := x mod 27;
b := y div 9;
z := y mod 9;
c := z div 3;
d := z mod 3;
write(x);
write(' in base3 is ');
writeln(a,b,c,d)
end.

It will convert a number from 0 to 80 to base3. For example if you enter 26 it would return 0222. Daivd was very excited that it worked and I hope to keep him interested in programming. If anyone has any suggestions please post a comment.

3 comments:

This comment has been removed by the author.

September 17, 2008 at 9:34 AM  

Now you can teach him about dynamic programming! Make it a base X (0-16) converter instead of just a base 3. That would help teach they underlying principles of why the program works rather than just the implementation of it. Like the idea that the next digit placeholder in a number is related to the previous digit by multiplying it by the base you are using, etc. Looking forward to all the Hello Worlds to come!

Dan

September 17, 2008 at 9:37 AM  

My husband is such a geek and I LOVE IT!

September 17, 2008 at 10:22 AM  

Newer Post Older Post Home