![]() |
|
#1
|
|||
|
|||
Pseudocode nested loop exampleFor 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
|
|||
|
|||
Re: help with psuedocode nested loop exampleQuote:
The language "For j = 1 step 1 to i" implies the following: Code:
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
|
|||
|
|||
Re: help with psuedocode nested loop exampleI 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
|
|||
|
|||
Re: help with psuedocode nested loop exampleQuote:
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:
Output: Code:
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
|
|||
|
|||
Re: help with psuedocode nested loop exampleCan 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:
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
|
||||
|
||||
Re: help with psuedocode nested loop exampleQuote:
__________________
During the election they said Obama could only be elected when pigs fly. Well, we currently have an epidemic of Swine Flu. Coincidence? |
|
#7
|
|||
|
|||
Re: Pseudocode nested loop exampleThanks 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 GIDBlog
Toyota - 2009 May Promotion by Nihal
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
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