Fw: [Courses] [C Programming] Anyone still here?

Sue Stones suzo at bigpond.net.au
Tue May 28 13:21:54 EST 2002


 ----- Original Message -----
 From: "Linda Mayhugh" <lmayhugh at drizzle.com>
 To: <courses at linuxchix.org>
 Sent: Saturday, 25 May 2002 1:59
 Subject: Re: [Courses] [C Programming] Anyone still here?


 > > Ask questions. :)
 >
 > Hello all,
 >
 > I have a different sort of question, a very basic level one: I've done
 > some simple programming in a few languages and it's fun. But I always
 > feel like I'm missing some basic level understanding of the process. I
was
 > a science major, not CS, and so just figured out how to write some Perl,
 > etc out of necessity. My question lies in how do you know how to approach
 > a problem, how do you know where to start, other than "well see if this
 > works, if not then try something else, until something apparently works"?
 > That approach strikes me as a great way to write really bad mistakes.
 >

 I agree, you often end up with such a mess you don't know why it works
 or wether it is correct.

 > This may not even be the real question that hangs me up, but it's all I
 > have figured out how to ask so far. I've read books and asked people
 > this before and have never gotten an answer that works (least helpful was
 > a former boss who archly told me that CS students consider being taught
 > how to program as a insult, beneath their dignity. He thought my
questions
 > to be evidence of a lack of wits).

 mmm, never come across this attitude before.  I've spent years learning how
 to program.  Infact my currnt degree spends more time on how to program
 than it does on actually programming!

 >
 > So is there some philosophy to programming, some underpinning that is not
 > syntax specific, or is it that my way of thinking isn't the same as the
 > originators of programming and I'll just never genuinely understand it?
Is
 > it one of those things that's either intuitive to you or it isn't?
 >

 As you can see from the replies that you have received there are lots of
 different
 philosophies.  The one that I was taught and whole-heartedly agree with is
 called top
 down approach.  It is called this because you start at the general
 description of
 what you want to model/program and then work down to the detail of the
 actual
 code.

 Let me give you an example, suppose I want to write a program to test
 multiplication
 skills for some one learning their "times tables", up to 12 time table.  I
 want to
 randomly ask the user what is a * b and then check the answer, if the
answer
 is wrong
 I will give them a second go, then if it is still wrong tell them the right
 answer.

 Actually by describing what I want to do I have done the first step of the
 top down
 approach.  Lets set this out differently.

 randomly ask the user what is a * b
 check the answer
 if the answer is wrong
     I will give them a second go
 if it is still wrong
     tell them the right answer


 Now I can see that I have missed out, or implied a step so I will add that
 in...

 randomly ask the user what is a * b
 check the answer
 if the answer is wrong
     I will give them a second go
     check the answer
 if it is still wrong
     tell them the right answer

 I will shorten the phrases a little, and make them a little more uniform,

 ask question
 check answer
 IF    answer wrong
     repeat question
     check answer
     IF     answer wrong
         print answer

 Now each of these steps, need to be broken down more.  If you are writing
 procedural code these steps become procedures, if you are writing OO
 code some of the procedures are encapsulated in the objects.  Since this is
 a course on C programming I will use a procedural approach.  (I'm more
 familiar with that too).

 To indicate that and perhaps make it easier to read I will change the way I
 have written the steps.

 ask_question
 check_answer
 IF    answer_wrong
     repeat_question
     check_answer
     IF     answer_wrong
         print_answer


 So now lets look at the first step, ask_question, to do that we need to

 choose a
 choose b
 print "what is a * b?"

 The second step is check_answer, we need to

 read in the users answer
 calculate the correct answer
 compare user's answer to the correct answer

 IF answer_wrong  doesn't need to be broken down any more, either it is
 true or false.

 To repeat_question we need to
 look up a    (possibly)
 look up b
 print "what is a * b?"

 The second time we check the answer is the same as the first, ditto with
the
 test.

 finally we need to print_answer

 calculate the answer
 print "a * b is 'answer' "

 Note that these steps can be broken down differently without abandoning the
 approach.
 eg I could calculate the answer every time I need it, or I could store it
 somewhere and
 then refer to that answer every time I need it.

 These steps then need to be further refined and broken down...

 looking at ask_question  I need to decide how to choose a random number
 between
 1 and 12, lets call this step choose_a_number.  First of all we will get a
 random number
 between 0 and 1, then change it to a number between 1 and 12.


 To choose_a_number

 get a random number
 multiply it by 12
 add 1
 take the integer part.

 (This actually works)  Many languages have a built in way of choosing a
 random number I think that
 C produces a random number between 0 and 1.  Lets assume that we can get a
 random number in that range, if not we would have to write a procedure to
do
 so.

 This process is repeated for other procedures. Note that at this stage I
 haven't written any
 code.  It is only once the steps get to the level of choose_a_number that
we
 start to pay much
 attention to coding.  At this stage the steps are things that can easily be
 done in C.  It is only now
 that I will turn this into code.  It is only at this stage that you try
 things to see if they work.

 And because I have started with an overview I know that what I have
 done is correct, each step is simple enough for me lo look over and check
my
 logic, and see
 if it is correct.  I can also change the program to for different
 circumstances.  If I decide that I
 want to use this for a younger child who is only capable of multiplying in
 numbers up to 5 it is
 simple to change the code to do so.  If fact for this very reason it is
much
 better style to not use
 specific numbers in code but to use constants that can easily be changed,
so
 instead of using 12
 I will use max_multiple, then all I have to do is set max_multiple to 12 at
 the beginning of the program
 and we are away.

 Well I have run out of time, but I hope that this make some sense at least.
 (Perhaps I should
 have written it in a more formal way, it seems to have ended up being quite
 long) - I think I got a
 bit carried away).  Since this is just an email, and a huge subject you may
 be completely
 confused.  Ask more if you want to know.  if you wanted you could try
 turning the example
 into code.

 sue






More information about the Courses mailing list