Thread: use of "for loops"

  1. #1
    C++Pandit
    Join Date
    Jul 2008
    Posts
    49

    use of "for loops"

    is it possible to get an output like
    1
    22
    333
    4444
    55555
    using for loops?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Yes.

  3. #3
    C++Pandit
    Join Date
    Jul 2008
    Posts
    49
    can anyone give a hint or somethin?


    please!

  4. #4
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Sure, turn this in:

    Code:
    int i ; 
    for (i = 0 ; i < 1 ; i++) printf("1") ; 
    for (i = 0 ; i < 1 ; i++) printf("22") ; 
    for (i = 0 ; i < 1 ; i++) printf("333") ; 
    for (i = 0 ; i < 1 ; i++) printf("4444") ; 
    for (i = 0 ; i < 1 ; i++) printf("55555") ;
    Mainframe assembler programmer by trade. C coder when I can.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> can anyone give a hint or somethin?

    The condition of the loop doesn't have to use a plain number. So:
    Code:
    for (int i = 0; i < 10; ++i)
    You don't have to use a number like 10 there. You can use a variable.

  6. #6
    Registered User
    Join Date
    Oct 2008
    Location
    Sweden
    Posts
    4
    Hi

    Yes that is possible, here is an example:
    Code:
    int i = 1;
    for (int n = 0; n < 5; n++)
    {
    	for (int x = 0; x < i; x++)
    	{
    		cout << i;
    	}
    	i++;
    	cout << endl;
    }

  7. #7
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    Please don't post solutions. The OP learns nothing this way. Worse still if it's a school assignment.

    Even then, I think there are "cleaner" ways to do it than you have shown.

  8. #8
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Quote Originally Posted by Dino View Post
    Sure, turn this in:

    Code:
    int i ; 
    for (i = 0 ; i < 1 ; i++) printf("1") ; 
    for (i = 0 ; i < 1 ; i++) printf("22") ; 
    for (i = 0 ; i < 1 ; i++) printf("333") ; 
    for (i = 0 ; i < 1 ; i++) printf("4444") ; 
    for (i = 0 ; i < 1 ; i++) printf("55555") ;
    Haha! That is amusing.

    Example:
    Code:
    /* Copyright (c) 2008. master5001. All rights reserved. */
    #include <vector>
    #include <iostream>
    
    int main(void)
    {
      std::vector<int> a;
      std::vector<int>::iterator begin, end;
      int i, j, k;
      
      std::cout << "Put a number! "; // no time for pleasantries in this program.
      std::cin >> j;
    
      j = +j;
    
      for(int i = 0; i < j;)
       a.push_back(++i);
    
      for(begin = a.begin(), end = a.end(); begin != end; begin++, --k)
      {
        for(i = 0; i < *begin; ++i)
          std::cout << *begin;
        std::cout << std::endl;
      }
    
      return 0;
    }
    Turn that in.
    Last edited by master5001; 10-07-2008 at 12:39 PM.

  9. #9
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Quote Originally Posted by cyberfish View Post
    Please don't post solutions. The OP learns nothing this way. Worse still if it's a school assignment.

    Even then, I think there are "cleaner" ways to do it than you have shown.
    Or take my approach and make people scratch their heads and wonder "Why did you do it that way, you moron..."

  10. #10
    Registered User
    Join Date
    Oct 2008
    Location
    Sweden
    Posts
    4
    Quote Originally Posted by cyberfish View Post
    Please don't post solutions. The OP learns nothing this way. Worse still if it's a school assignment.

    Even then, I think there are "cleaner" ways to do it than you have shown.
    yes you are right, I'll keep that in mind

  11. #11
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Your loop could have been designed more similarly to my print loop. You are introducing unnecessary variables into your method.

  12. #12
    Registered User
    Join Date
    Sep 2008
    Posts
    14
    Quote Originally Posted by cyberfish View Post
    Please don't post solutions. The OP learns nothing this way. Worse still if it's a school assignment.

    Even then, I think there are "cleaner" ways to do it than you have shown.
    Why not? Some people like me actually learn by example. Its not that everyone wants you to do their work for them just give them a basic example.

    -DenKain

  13. #13
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    Why not? Some people like me actually learn by example. Its not that everyone wants you to do their work for them just give them a basic example.
    If the OP posted what he has been trying, we will help him with that. It's not whether you can follow a solution and say "it makes sense". But rather whether you can come up with it in the first place. And then there's the homework assignment issue... I'm sure no professor/instructor wants their student to post their homework assignment on a forum and copy a solution from there.

  14. #14
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> Why not?

    Because it's cheating. Learning by example is fine if it is not an example of a solution to homework. There's no surefire way to distinguish between homework assignments and people learning on their own, so the result is a policy to not provide solutions to homework questions.

  15. #15
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    A forum is an interactive medium. Examples can be found on static media such as tutorial web-sites and books. I will hush now before people point out how often I post examples... hell including this time.

Popular pages Recent additions subscribe to a feed