Solved

Having Trouble with Nested Loops

Posted on 2001-06-21
13
302 Views
Last Modified: 2011-10-03
Can anyone help me? I'm totally lost.

I'm trying to write a nested loop code segment that produces the following ouput:

1
1 2
1 2 3
1 2 3 4

Thanks for your help.
0
Comment
Question by:Michelle21221
  • 4
  • 3
  • 2
  • +2
13 Comments
 

Accepted Solution

by:
johnbrewer1980 earned 25 total points
ID: 6216180
easy:
for (int i = 1; i <= 4; ++i) {
   for (int j = 1; j <= i; ++j) {
      cout << j;
   }
   cout << endl;
}
0
 
LVL 7

Expert Comment

by:KangaRoo
ID: 6216191
Looks a bit like homework. We can (may) not provide complete solutions to homework, but we can assist you.

Is there any code you've tried or written so far? Even when it's not working (yet), we can help you to get it right.
0
 
LVL 1

Expert Comment

by:glebspy
ID: 6217299
This is the correct answer

for (int i = 1; i <= 4; ++i) {
                for (int j = 1; j <= i; ++j) {
                   cout << j << " ";
                }
                cout << endl;
              }

cout<<endl<<"Thanks for you help."<<endl
0
 

Expert Comment

by:johnbrewer1980
ID: 6217313
Hey glesby, that's the same as my answer!
0
 
LVL 1

Expert Comment

by:glebspy
ID: 6217321
I'd like to acknowledge johnbrewer1980, whose answer was highly influential in helping to construct this answer.
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 6217407
#include "stdafx.h"
#include <iostream.h>

void OutputIt( int n ) {
    if ( n != 0 ) {
       OutputIt(n-1);
       cout << n << ' ';
    }
}

int main(int argc, char* argv[])
{
    OutputIt( 1 ); cout << endl;
    OutputIt( 2 ); cout << endl;
    OutputIt( 3 ); cout << endl;
    OutputIt( 4 ); cout << endl;
    return( 0 );
}

Thank you, thank you!  I'd like to thank all of the little people who contributed, however insignificantly to this production.  You LIKE me! you REALLY LIKE Me!

-- Dan
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 9

Expert Comment

by:ShaunWilde
ID: 6218052
#include "stdafx.h"

#include <string>
#include <iostream>

using namespace std;

int main(int argc, char* argv[])
{
     std::string szString;
     for (int n=1;n<=4;n++)
     {
          char szBuffer[32];
          itoa(n,szBuffer,10);
          szString+=szBuffer;
          cout << szString <<endl;
          szString += " ";
     }
     return 0;
}

my contribution to this party :) - this was all my own work and I will thank no one except the great almighty - whomever that might be
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 6219418
I've improved my algorithm:

#include "stdafx.h"
#include <iostream.h>

int main(int argc, char* argv[])
  cout << "Nested loop example:" << endl;
  cout << "1" << endl;
  cout << "1 2" << endl;
  cout << "1 2 3" << endl;
  cout << "1 2 3 4" << endl;
}

Simpler is always better.  Just be sure to keep the first line or the professor won't know how hard you worked.

-- Dan

0
 
LVL 9

Expert Comment

by:ShaunWilde
ID: 6220098
Dan - I hate to tell you this but - YOU made a mistake :)

your forgot your {

is that your first mistake this century ?
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 6221100
Just seeing if YOU were paying attention. Ha! made ya look! :---}

-- Dan
0
 
LVL 7

Expert Comment

by:KangaRoo
ID: 6221485
// Okay, you asked for it...

#include <iostream>

template<int N>
struct print_numbers
{
   ostream& print(ostream& out) const
   {
       print_numbers<N-1>().print(out);
       out << " " << N;
       return out;
   }
};

template<>
struct print_numbers<1>
{
   ostream& print(ostream& out) const
   {
       out << 1;
       return out;
   }
};

template<int K>
struct print_lines
{
   ostream& print(ostream& out) const
   {
      print_lines<K-1>().print(out);
      print_numbers<K>().print(out);
      out << endl;
      return out;
   }
};

template<>
struct print_lines<1>
{
   ostream& print(ostream& out) const
   {
      print_numbers<1>().print(out);
      out << endl;
      return out;
   }
};

int main()
{
   print_lines<4>().print(cout);
}
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 6221676
Michelle21221,
Ignore KangRoo's post.  It is one thing to bring a polished apple to the teacher.  It's quite another to backup a truck full of them to the classroom door...

-- Dan
0
 
LVL 9

Expert Comment

by:ShaunWilde
ID: 6221703
I'd fail it due to lack of comments myself
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now