generating a sequence 1, 12, 123, 1234, ...
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11

Thread: generating a sequence 1, 12, 123, 1234, ...

  1. #1
    Join Date
    Mar 2011
    Posts
    153

    generating a sequence 1, 12, 123, 1234, ...

    Hi

    I've been trying to write a program to generate the sequence:
    1
    12
    123
    1234
    12345
    ...

    It is the user who decides how many terms of the sequence are displayed. For example, the terms given above are five. Could you please help me with it? I think I need to use the for loop here. Given below are some random steps I put down while trying! Please help me.

    Code:
    #include <iostream>
    #include <cstdlib>
    
    using namespace std;
    
    int main()
    
    {
         
         int i, prevterm = 1, nexterm;
         
         for ( i=0; i<=n; i++)
         
         {
         
         nexterm
         
         cout << prevterm << prevterm
         
         nexterm = ++prevterm
         
         nth term  = 10^(n-1)(n-1) + n
         2 + (2-1) = 21
         3 + 2 = 23
         
         1
         12
         123
         1234
         12345

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    11,986

    Re: generating a sequence 1, 12, 123, 1234, ...

    Seems like you need two loops to me. The outer loop controls how many lines you write and the inner loop controls how many characters you write on each line. The upper bound of the inner loop will be the current iteration of the outer loop.

    I can't tell what you're up to with the math you're doing in there. I don't see any math required.

    Since there's nothing Visual C++ specific, this should be in the general C++ forum, not Visual C++/

  3. #3
    Join Date
    Mar 2011
    Posts
    153

    Re: generating a sequence 1, 12, 123, 1234, ...

    Thank you, GCDEF. If you think it belongs to the other sections, then you can move it there.

    Best regards
    H

  4. #4
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    11,986

    Re: generating a sequence 1, 12, 123, 1234, ...

    Quote Originally Posted by heights View Post
    Thank you, GCDEF. If you think it belongs to the other sections, then you can move it there.

    Best regards
    H
    I'm not a moderator.

  5. #5
    Join Date
    Mar 2011
    Posts
    153

    Re: generating a sequence 1, 12, 123, 1234, ...

    Quote Originally Posted by GCDEF View Post
    I'm not a moderator.
    It's okay then.

    Regards
    H

  6. #6
    Join Date
    Apr 2007
    Posts
    150

    Re: generating a sequence 1, 12, 123, 1234, ...

    Treat it as a string and it is fairly easy to do.

    If you have to do it mathematically take note of the addition required to get to each iteration.
    From 1 to 12 you add 11
    From 12 to 123 you add 111 etc.
    Which translates to adding 1 + 10^1, 11 + 10^2 etc.
    That all works well in a for loop.

    Zapper

  7. #7
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    11,986

    Re: generating a sequence 1, 12, 123, 1234, ...

    Quote Originally Posted by zapper222 View Post
    Treat it as a string and it is fairly easy to do.

    If you have to do it mathematically take note of the addition required to get to each iteration.
    From 1 to 12 you add 11
    From 12 to 123 you add 111 etc.
    Which translates to adding 1 + 10^1, 11 + 10^2 etc.
    That all works well in a for loop.

    Zapper
    That's still more work than you need to to. All you need is two loops and output the loop counters. No math or strings required.

  8. #8
    Join Date
    Mar 2008
    Location
    Turin / Italy
    Posts
    178

    Re: generating a sequence 1, 12, 123, 1234, ...

    GCDEF is right. No math or strings involved.

    Code:
    
    
    Last edited by SkyNetTo; April 23rd, 2011 at 05:53 PM. Reason: Didn't know that was a homework.

  9. #9
    ovidiucucu's Avatar
    ovidiucucu is offline Moderator/Reviewer Power Poster
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    7,994

    Re: generating a sequence 1, 12, 123, 1234, ...

    [ Moved thread ]
    Ovidiu Cucu
    "When in Rome, do as Romans do."
    Visit: Microsoft Virtual Academy
    Follow: https://twitter.com/#!/ovidiucucu
    My blog: http://codexpert.ro/blog/author/ovidiu-cucu/

  10. #10
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    11,986

    Re: generating a sequence 1, 12, 123, 1234, ...

    Quote Originally Posted by SkyNetTo View Post
    GCDEF is right. No math or strings involved.
    We don't typically just up and do somebody's homework for them.

  11. #11
    Join Date
    Mar 2008
    Location
    Turin / Italy
    Posts
    178

    Re: generating a sequence 1, 12, 123, 1234, ...

    Oops sorry, didn't know that was a homework, i'm deleting the code.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center