CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Nov 2012
    Posts
    21

    Writing this sequence of numbers in code

    How can I write the list(or pattern like number) list into a loop or something equal to that in c++?

    1=3
    2=3
    3=3
    4=3
    5=6
    6=6
    7=6
    8=6
    9=9
    10=9
    11=9
    12=9
    13=12
    14=12
    15=12
    16=12
    17=15

    and so on..

    extra note: the left column is like the strlen of a string..I want the value on the left side compared to the string length. So if I have a string that is 14 characters long, I will get 12.
    Last edited by terryeverlast; May 10th, 2014 at 08:51 AM.

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Writing this sequence of numbers in code

    Quote Originally Posted by terryeverlast
    How can I write the list(or pattern like number) list into a loop or something equal to that in c++?
    What is the pattern involved? For example, at a glance it seems that the numbers on the left hand side of the '=' on each line are the natural numbers, counting from 1. The numbers of the right hand side appear to be multiples of 3, except that each multiple of 3 appears four times. From here, it should be possible to derive a function to make the numbers on the left hand side to the corresponding numbers on the right hand side, upon which this becomes a simple exercise of implementing it.

    Quote Originally Posted by terryeverlast
    the left column is like the strlen of a string..I want the value on the left side compared to the string length. So if I have a string that is 14 characters long, I will get 12.
    Sorry, but I don't follow. I don't see how a string of with a length of 14 corresponds to 12.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: Writing this sequence of numbers in code

    I don't really understand the question re string length, but this will produce the output as shown in post #1
    Code:
    #include <iostream>
    using namespace std;
    
    const int maxnum = 100;
    
    int main()
    {
    	for (int i = 1; i <= maxnum; ++i)
    		cout << i << "=" << (((i - 1) / 4) + 1) * 3 << endl;
    
    }
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured