Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Logical advice...

Logical advice...
Thread Tools
Fresh-Faced Recruit
Join Date: Jan 2001
Status: Offline
Reply With Quote
Sep 27, 2002, 10:44 PM
 
I'm doing my best to learn C; however, I'm having trouble learning to think like a programmer. Loops (for, while, etc..) are giving me a really tough time. I've tried sitting down with a pencil and paper and tracing the steps, but it still the core logic and concept still seem to evade me. For instance, doing this...
1
22
333
4444
55555

Took me over an hour to figure out using the for loop. I couldn't do get it with the while loop. Anything I can read or do (other than just keep plugging away and hope I get it) to shed some light on the subject.

Kevin
     
Professional Poster
Join Date: Apr 2001
Location: Long Beach, CA
Status: Offline
Reply With Quote
Sep 27, 2002, 10:48 PM
 
Try using TWO for loops... one inside the other. I'd give you more details, but I get the feeling this is a programming assignment.

Figuring out the logic of programming is just a matter of breaking a task down into smaller and smaller steps until the steps are so incredibly small that they are incredibly easy to define.

Example: how do you go from n to nn? multiply by 10 and add n.

How do you get the prime numbers from 1 to 100? Check each number from 1 to 100 to see if any of the numbers smaller than that number will divide it. How do you do that? Break it down into smaller steps. How do you do that? Well, you do it in your brain whenever you try to answer that question. How do you do it in your brain?

Back to the original task: What's the next number in the sequence? How do you know? What did you have to do to create that number?

Virtually everything can be broken down into simpler steps... it's not necessarily easy to do that. How do you define creativity? That's likely the only thing that can't be...
(Last edited by Detrius; Sep 27, 2002 at 10:53 PM. )

ACSA 10.4/10.3, ACTC 10.3, ACHDS 10.3
     
Professional Poster
Join Date: Oct 1999
Location: :ИOITAↃO⅃
Status: Offline
Reply With Quote
Sep 28, 2002, 06:43 AM
 
I agree that plain ol' practice is the best way to get the idea.

Sometimes it's nice to start with a language that's very easy with regards to syntax, so you can work on just getting the concepts. You might open up the Script Editor to just noodle around with Applescript for awhile, since you can just write some code and press "play" to get immediate feedback, and the "results" window will show you what you've done.


As far as the for loop vs. the while loop, the thing to keep in mind is that they are exactly the same thing, just expressed differently. For every loop, you will have:
1. Something you set up at the start of your loop.
2. A step that you perform on each iteration of the loop.
3. A condition that you check to decide whether to finish the loop.

For example, a simplified version of your assignment might be to print:
1
2
3
4
5

In a for loop, you cram the setup, the condition, and the step all in one:

int i;
for (i = 1; i <= 5; i++)
printf("%d\n,i);


In a while loop, you might break those apart into several pieces, but the loop is fundamentally the same:

int i=1;
while (i <= 5)
{
printf("%d\n,i);
i++;
}


Anyhow, have fun.
     
Mac Elite
Join Date: Feb 2001
Location: adrift in a sea of decadent luxury and meaningless sex
Status: Offline
Reply With Quote
Sep 28, 2002, 02:26 PM
 
Mithras, did you forget to close your quotes in the printf's, or is this a C trick I don't know about?
blackmail is such an ugly word. I prefer extortion. the X makes it sound cool
     
Professional Poster
Join Date: Oct 1999
Location: :ИOITAↃO⅃
Status: Offline
Reply With Quote
Sep 28, 2002, 03:21 PM
 
Originally posted by lucylawless:
Mithras, did you forget to close your quotes in the printf's, or is this a C trick I don't know about?
Eaten by the board, I guess. Should have used a code tag.
     
Admin Emeritus
Join Date: Oct 2000
Location: Boston, MA
Status: Offline
Reply With Quote
Sep 28, 2002, 05:58 PM
 
Like the other posters have said, practice will get you far. By doing that you'll be able to quickly map ideas to steps and steps to code.

Programming is like looking at a mathematics problem and knowing what tools and theorems you need to solve it -- it comes with practice and time.
"Against stupidity, the gods themselves contend in vain" (Schiller)
     
Fresh-Faced Recruit
Join Date: Jan 2001
Status: Offline
Reply With Quote
Sep 28, 2002, 07:56 PM
 
Thanks for the encouragement to keep practicing. It's starting to become more clear (at least the basics are anyway, I'm less than half way through the program). Would a book like Code Complete or Algorithms help any?
Kevin
     
Mac Elite
Join Date: May 1999
Location: San Jose, CA
Status: Offline
Reply With Quote
Sep 29, 2002, 04:07 PM
 
Originally posted by Mithras:
As far as the for loop vs. the while loop, the thing to keep in mind is that they are exactly the same thing, just expressed differently.
Actually no, Mithras.

At the simplest level, a for loop will execute at least once, a while loop may never run at all.

For example:

for (i = 1; i > 1; i++)
{
printf("This will run once\n");
}

wheres:

i=1
while (i > 1)
{
printf("This will never run\n");
i++
}

The difference is that a for loop performs the check at the end of each iteration, whereas the while loop performs the check at the beginning. As a result a for loop will ALWAYS run at least once. A subtle difference that can make a huge impact on your program.
Gods don't kill people - people with Gods kill people.
     
Admin Emeritus
Join Date: Oct 2000
Location: Boston, MA
Status: Offline
Reply With Quote
Sep 29, 2002, 05:17 PM
 
Actually no, Camelot.

Both for and while loops do their condition testing at the beginning. It's the do...while that will run once under your conditions.
"Against stupidity, the gods themselves contend in vain" (Schiller)
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 01:51 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2