GIDForums  

Go Back   GIDForums > Computer Programming Forums > 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, 20: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, 21:32
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,218
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, 21: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, 22:01
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,218
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.)
  #5  
Old 02-Feb-2009, 18:50
emgorode emgorode is offline
New Member
 
Join Date: Feb 2009
Posts: 2
emgorode is on a distinguished road

Re: help with psuedocode nested loop example


Can anyone help me out? I got an assignment and one of the questions asks to produce that same output but in pseudo-code. The OP's pseudo-code seemed a little over the top for what we're learning. But I like the idea of the FOR statement within the FOR statement.

All I have is:
Code:
For x = 1 to 5 For y = 1 to ?
See, I'm not sure because above it's mentioned to put "For y = 1 to y" but I wasn't sure how it would know when to end. Can someone explain that statement? And I don't get how it's adding on a number while remembering the prior numbers. In other words, I understand how it could become

1
12

but not how it would then transition to 123. Wouldn't it just become 3. How is it retaining 12 as a number on which to build off of?

Please get at me. Any help is appreciated. This class is going through material way too fast. Thanks.
  #6  
Old 06-Feb-2009, 23:50
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,335
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

Re: help with psuedocode nested loop example


Quote:
Originally Posted by emgorode
Can anyone help me out?
Sure. Start your own thread and don't hijack someone else's that's over a year old.
__________________

During the election they said Obama could only be elected when pigs fly. Well, we currently have an epidemic of Swine Flu. Coincidence?
  #7  
Old 07-Feb-2009, 20:46
emgorode emgorode is offline
New Member
 
Join Date: Feb 2009
Posts: 2
emgorode is on a distinguished road

Re: Pseudocode nested loop example


Thanks for wasting my time with that response. The guidelines state not to start new threads on the same topic. Seeing that I wanted my output to be identical to the original poster's, I went ahead and continued a thread rather than starting a new one. Sorry for being conservative. Sheesh.
 
 

Recent GIDBlogToyota - 2009 May Promotion by Nihal

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 C++ Forum 1 16-Apr-2007 20:03
Text-Based Roulette Game mfm1983 C++ Forum 5 29-Nov-2006 13:20
Array 1 dimensional help please asap lion123 C Programming Language 10 18-Feb-2005 22:53
Nested for loop with function Tori C++ Forum 11 08-Nov-2004 14:02
[Header]Classifying the FOR loop mithunjacob C++ Forum 0 20-Jun-2004 17:39

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

All times are GMT -6. The time now is 16:31.


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