Thread: Looping through a list infinitely

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    930

    Looping through a list infinitely

    Hello and Happy New Year,

    I would like to loop through a list of names infinitely like this:

    Harry
    Dick
    Tom
    ===
    Dick
    Tom
    Harry
    ===
    Tom
    Dick
    Harry

    etc...

    I have this for the moment:

    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
        int i = 0, j = 0;
    
        char * Names[] =
        {
            "Harry ", "Dick", "Tom",
        };
    
        int iSize = (sizeof(Names)/sizeof(Names[0]));
    
        while(1)
        {
            for(; i<=iSize; i++)
            {
    
                if(j >= iSize) j = 0;
                cout << Names[j++] << endl;
            }
            cout << endl;
    
            if(j == iSize) j = 1;
        }
    
        return 0;
    }
    Using Windows 10 with Code Blocks and MingW.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    You mean a permutation -> Permutation - Wikipedia

    Also, you posted this C++ program on the C board.

    If it really is C++, use this.
    std::next_permutation - cppreference.com
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    930
    Thank you, yes, its permutation what I was looking for, with permutation it is much simple.

    I managed to do it with for loop after all.

    Code:
    #include <windows.h>
    #include <iostream>
    using namespace std;
    
    int main()
    {
        int j = 0, iCounter = 0;
    
        char * Names[] =
        {
            "Harry ", "Dick", "Tom",
        };
    
        int iSize = (sizeof(Names)/sizeof(Names[0]));
        
    
        int global = iSize+3;
    
        while(global--) // just for test purpose, to avoid going on forever
        {
            for(int i=0; i<iSize; i++)
            {
    
                if(j == iSize) j = 0;
                cout << Names[j++] << endl;
    
            }
            cout << " = = = " << endl;
    
            if(iCounter == (iSize-1))
            {
                j = 0;
                iCounter = -1;
            }
            else if((iCounter < iSize))
            {
                j = iCounter + 1;
            }
    
            iCounter++;
        }
    
        return 0;
    }
    Last edited by Ducky; 01-01-2023 at 04:16 AM.
    Using Windows 10 with Code Blocks and MingW.

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    930
    No, finally its not permutation what I was looking for.

    This is permutation:
    abc
    acb
    bac
    bca
    cab
    cba

    And I was looking for this:
    abc
    bca
    cab
    Using Windows 10 with Code Blocks and MingW.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    How about
    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
        int j = 0, iCounter = 0;
    
        const char * Names[] =
        {
            "Harry ", "Dick", "Tom",
        };
    
        int iSize = (sizeof(Names)/sizeof(Names[0]));
    
        for ( int start = 0 ; start < iSize ; start++ ) {
            for ( int count = 0 ; count < iSize ; count++ ) {
                int index = (start + count) % iSize;
                cout << Names[index] << endl;
            }
            cout << "===" << endl;
        }
    
        return 0;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Dec 2007
    Posts
    930
    Yes, that's a simpler solution than mine.

    With only the modulus operator you replaced half of my code

    Thank you Salem.
    Last edited by Ducky; 01-02-2023 at 10:43 AM.
    Using Windows 10 with Code Blocks and MingW.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why does this line of code loop infinitely?
    By C-UL8R in forum C Programming
    Replies: 1
    Last Post: 10-12-2020, 02:33 PM
  2. Loop is running infinitely.please help...
    By V8cTor in forum C++ Programming
    Replies: 2
    Last Post: 08-07-2014, 01:38 AM
  3. case, menu system, loops infinitely on invalid input
    By ModeSix in forum C Programming
    Replies: 12
    Last Post: 05-08-2011, 04:12 PM
  4. Infinitely looping inclusion!
    By kidburla in forum C++ Programming
    Replies: 3
    Last Post: 11-29-2005, 07:40 PM
  5. is there an infinitely large integer type?
    By MKashlev in forum C++ Programming
    Replies: 7
    Last Post: 08-10-2002, 02:31 PM

Tags for this Thread