GIDForums  

Go Back   GIDForums > Computer Programming Forums > CPP / C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 01-Nov-2007, 19:59
avg81 avg81 is offline
New Member
 
Join Date: Nov 2007
Posts: 5
avg81 is on a distinguished road

Pseudocode nested loop example


For i = 1 step 1 to 5
{
For j = 1 step 1 to j
Output j
End for //loop on j
Output newline
}
End for //loop on i


How does this code produce this output?
1
12
123
1234
12345

I see step 1 to five in outer loop first so we travel to the inner
loop for( j=1 step 1 to j) j =1 so output the first 1
What i dont see is where does j get increased so the next
"12" will print

hope i made my question clear! Take your time no hurry
  #2  
Old 01-Nov-2007, 20:32
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,627
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: help with psuedocode nested loop example


Quote:
Originally Posted by avg81
...where does j get increased...

The language "For j = 1 step 1 to i" implies the following:
Code:
set j equal to 1 perform the loop operations increment j if j is less than i then perform the loop operations again else don't perform the loop operations again (that is: drop out of the loop to the next instruction after the loop)

In C we would write for(j = 1; j < i; j++)


You can step through it just as if you were the computer:

Here's what happens the first time through the outer loop (i = 1):
i = 1, (j goes from 1 to 1: output j), output newline

Result:
1(newline)

Here's the second time through the outer loop:
i = 2, (j goes from 1 to 2: output j), output newline

Result:
12(newline)

Here's the third:
i = 3, (j goes from 1 to 3: output j) , output newline

Result:
123(newline)

etc.

Regards,

Dave
  #3  
Old 01-Nov-2007, 20:40
avg81 avg81 is offline
New Member
 
Join Date: Nov 2007
Posts: 5
avg81 is on a distinguished road

Re: help with psuedocode nested loop example


I can almost see it except for, where is this instruction in my psuedocode?

if j is less than i

you wrote The language "For j = 1 step 1 to i" implies the following: .........

my school has this on their page

For i = 1 step 1 to 5
{
For j = 1 step 1 to j // this line might be mistyped by school
Output j
End for //loop on j
Output newline
}
End for //loop on i

I thought this might have been a miss type at my school. What do you think?
  #4  
Old 01-Nov-2007, 21:01
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,627
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: help with psuedocode nested loop example


Quote:
Originally Posted by avg81
my school has this on their page

For i = 1 step 1 to 5
{
For j = 1 step 1 to j // this line might be mistyped by school
Output j
End for //loop on j
Output newline
}
End for //loop on i

I apologize for not spotting it. Your original question was a good one, and I just missed the little detail that was bothering you. Yes. the statement "For j = 1 step 1 to j" is incorrect. (Or, maybe it would be better to say that with that statement you definitely won't get the stated output.)

It's almost like cheating to make the computer tell us, and I am sure you were supposed to analyze it as it was written. You definitely spotted a bug.

Anyhow:
CPP / C++ / C Code:
#include <stdio.h>

int main()
{
    int i, j;
    for (i = 1; i <= 5; i++) {
        for (j = 1; j <= i; j++) {
            printf("%d",j);
        }
        printf("\n");
    }
    return 0;
}

Output:
Code:
1 12 123 1234 12345

Regards,

Footnote: My restatement of the pseudocode also had an error. I'm just so used to thinking of loops in C a certain way that I didn't quite get it right. (That's another way of saying that I was wrong.) Note the way that I actually wrote the loop statements in the "real" code, and make corrections to my previous statements. Real code is actually easier than pseudocode for me, since there is really no universally accepted standard for pseudocode. (That's no excuse, however, for my missing it.)
 
 

Recent GIDBlog2nd Week of IA Training by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Nested For Loop thread dragon71 CPP / C++ Forum 1 16-Apr-2007 19:03
Text-Based Roulette Game mfm1983 CPP / C++ Forum 5 29-Nov-2006 12:20
Array 1 dimensional help please asap lion123 C Programming Language 10 18-Feb-2005 21:53
Nested for loop with function Tori CPP / C++ Forum 11 08-Nov-2004 13:02
[Header]Classifying the FOR loop mithunjacob CPP / C++ Forum 0 20-Jun-2004 16:39

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 03:37.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.