Help in for, while and do-while loop

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Synapse
    New Member
    • Dec 2006
    • 26

    Help in for, while and do-while loop

    I really need your help everyone. i need a program that results the output below using for, while, and do-while loop..3 program codes using those 3 loops...please. .i really need your help.

    Output:

    1
    1 2
    1 2 3
    1 2 3 4
    1 2 3 4 5
    1 2 3 4 5 6
    1 2 3 4 5 6 7
    1 2 3 4 5 6 7 8
    1 2 3 4 5 6 7 8 9
    1 2 3 4 5 6 7 8 9 10

    and also the reverse

    10 9 8 7 6 5 4 3 2 1
    10 9 8 7 6 5 4 3 2
    10 9 8 7 6 5 4 3
    10 9 8 7 6 5 4
    10 9 8 7 6 5
    10 9 8 7 6
    10 9 8 7
    10 9 8
    10 9
    10

    Great help is really appreciated..th ank you very much.
  • horace1
    Recognized Expert Top Contributor
    • Nov 2006
    • 1510

    #2
    the simplest way to approach the problem is to attempt to print
    1
    1 2
    1 2 3
    1 2 3 4
    1 2 3 4 5
    1 2 3 4 5 6
    1 2 3 4 5 6 7
    1 2 3 4 5 6 7 8
    1 2 3 4 5 6 7 8 9
    1 2 3 4 5 6 7 8 9 10
    using one of the loop constructs.

    e.g. the algorithm in pseudo code using a while() would be something like
    Code:
    i=1
    while(i < 11)
       j=1
       while (j <= i)
           print j
           j = j + 1
       print '\n'
       i = i + 1
    when that works implement the reverse, then the other constructs

    Comment

    • Synapse
      New Member
      • Dec 2006
      • 26

      #3
      Thank you very much for that quick reply sir..by the way, i used the for loop but then then the output turns out like this..

      112123123456123 456712345678123 456789123456789 10 :(

      can somebody give me an idea how to break the line...?

      here's my code:

      #include <iostream.h>
      #include <conio.h>

      int main (void)

      {
      int i, j;
      for(i=1; i<=10; i++)
      for(j=1; j<=i; j++)
      cout << j;
      return 0;
      }

      Comment

      • horace1
        Recognized Expert Top Contributor
        • Nov 2006
        • 1510

        #4
        Originally posted by Synapse
        Thank you very much for that quick reply sir..by the way, i used the for loop but then then the output turns out like this..

        112123123456123 456712345678123 456789123456789 10 :(

        can somebody give me an idea how to break the line...?

        }
        you forgot to print the newline at the end of the outer loop, e.g.
        Code:
        #include <iostream>
        using namespace std;
        
        int main (void)
        {
        int i, j;
        for(i=1; i<=10; i++)
          {
           for(j=1; j<=i; j++)
              cout << j;
           cout << endl;   // ** you needed this!
          }
        return 0;
        }
        avoid using <iostream.h> as it is a deprecated or antiquated header. With modern C++ compilers use <iostream> as above.

        Comment

        • Shakje
          New Member
          • Jan 2007
          • 5

          #5
          Code:
          #include <iostream>
          using namespace std;
          
          int main (void)
          {
          int i, j;
          for(i=1; i<=10; i++)
            {
             for(j=1; j<=i; j++)
                cout << j;
             cout << endl;   // ** you needed this!
            }
          return 0;
          }
          For those who may not know this, you can have an optional Parameter in SQL. It took me forever to figure it out.


          If you're doing lots of couts as in this case, it's better to get into the habit of doing cout << "\n" no? :D

          Comment

          • Synapse
            New Member
            • Dec 2006
            • 26

            #6
            I've already got it before you put the code but that was a great help too.
            Now,can you help me with this using the while and do-while loop?
            my code isn't working yet.. :(
            Thanks for the help guys...

            Comment

            • kashish
              New Member
              • Jan 2007
              • 1

              #7
              What is the syntax of new operator overloading ?

              Comment

              • Synapse
                New Member
                • Dec 2006
                • 26

                #8
                #include <iostream>

                using namespace std;


                when is use this syntax, it says Unable to open include file 'IOSTREAM'

                declaration syntax error in using namespace std;

                im using Turbo C/C++ Compiler by Borland.

                Comment

                • horace1
                  Recognized Expert Top Contributor
                  • Nov 2006
                  • 1510

                  #9
                  Originally posted by Synapse
                  #include <iostream>

                  using namespace std;


                  when is use this syntax, it says Unable to open include file 'IOSTREAM'

                  declaration syntax error in using namespace std;

                  im using Turbo C/C++ Compiler by Borland.
                  I assume this is Turbo C V2.0 or V3.0 for DOS - you will need to use the <iostream.h> format as V2.0 and 3.0 are pre the new header files

                  Comment

                  • horace1
                    Recognized Expert Top Contributor
                    • Nov 2006
                    • 1510

                    #10
                    Originally posted by Shakje
                    Code:
                    #include <iostream>
                    using namespace std;
                    
                    int main (void)
                    {
                    int i, j;
                    for(i=1; i<=10; i++)
                      {
                       for(j=1; j<=i; j++)
                          cout << j;
                       cout << endl;   // ** you needed this!
                      }
                    return 0;
                    }
                    For those who may not know this, you can have an optional Parameter in SQL. It took me forever to figure it out.


                    If you're doing lots of couts as in this case, it's better to get into the habit of doing cout << "\n" no? :D
                    in situations like this I tend to use endl so if the program crashes the streams have been flushed (endl inserts a new-line character into the stream and for buffered streams flushes the buffer). When outputing lots of character based information I tend to put \n in the the strings and an endl on the last one.

                    Comment

                    • Shakje
                      New Member
                      • Jan 2007
                      • 5

                      #11
                      For loops are the best way to do looping when you have a fixed number of iterations, but you can use while loops in the same way quite easily. All a for loop really does is include the stuff you have to do in the line you start it.

                      while loops keep on executing everything between the braces while the condition in brackets is true. Eg:

                      Code:
                      int counter = 0;
                      
                      while(counter < 10)
                      {
                         counter++;
                      }
                      This keeps adding 1 to counter until it reaches 10.

                      This can also be written without the braces:

                      Code:
                      int counter = 0;
                      
                      while(counter<10)
                        counter++;
                      A do while works slightly differently, but is the same idea. It's written:

                      Code:
                      int counter = 0;
                      
                      do
                      {
                        counter++;
                      } while(counter<10);
                      The only real difference is that with a while loop the condition is tested before any code is executed in the loop, whereas the code in a do while loop will execute at least once before the condition is tested.

                      So in the following example:

                      Code:
                      while(true)
                      {
                        cout << "LOL";
                      }
                      nothing will happen, but if you do the same with a do while:

                      Code:
                      do
                      {
                        cout << "LOL";
                      } while(true);
                      you will get at least one output, because the loop doesn't finish until the very end.

                      Shouldn't be too difficult to modify the above ;)

                      Comment

                      • Synapse
                        New Member
                        • Dec 2006
                        • 26

                        #12
                        weww! that was a nice nifty tutorial..thanx boss. thank you very much. im goin to try the code then.

                        harigato :)

                        Comment

                        • Synapse
                          New Member
                          • Dec 2006
                          • 26

                          #13
                          #include <stdio.h>
                          #include <conio.h>

                          int main(void)

                          {
                          clrscr();
                          int i, j;

                          for(i=1; i<=10; i++)

                          {
                          while (i<=10)
                          {
                          putchar(j+'0');
                          ++i;

                          if (j==10)
                          j=0;
                          else
                          ++j;
                          printf("j");
                          }
                          j=1;

                          }
                          return 0;
                          }


                          that's my while code up there but the input is

                          123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789
                          and so on.....

                          the input must be

                          1
                          12
                          123
                          1234
                          12345
                          123456
                          1234567
                          12345678
                          123456789
                          12345678910

                          Comment

                          • horace1
                            Recognized Expert Top Contributor
                            • Nov 2006
                            • 1510

                            #14
                            is this what you were trying to do?
                            Code:
                            #include <stdio.h>
                            #include <conio.h>
                            
                            int main(void)
                            
                            {
                            clrscr();
                            int i=0, j;
                            while (i<=10)
                            {
                             j=0;
                             while(j<=i)
                               {   
                                printf("%d", j);
                                j++;
                                }
                             printf("\n");
                            i++;
                            }
                            return 0;
                            }

                            Comment

                            • Synapse
                              New Member
                              • Dec 2006
                              • 26

                              #15
                              thanx sir...but im still a little confuse how the while or do-while loop works.
                              can somebody explain it to me pls...:(

                              Comment

                              Working...