alternate for loops? possible?

This is a discussion on alternate for loops? possible? within the C++ Programming forums, part of the General Programming Boards category; Is there a way to make a compiler execute 2 for loops simultaneously and the loops are alternate so that: ...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    50

    alternate for loops? possible?

    Is there a way to make a compiler execute 2 for loops simultaneously and the loops are alternate so that:
    Code:
    for loop1:
    		for (a=0; a<unknown1 ; a++)
    		{
    		cout<<"a"<<a<<" ";
    		}
    
    for loop2:
    		for (b=0; b<unknown2 ; b++)
    		{
    		cout<<"b"<<b<<" ";
    		}
    
    output:
    a0 b0 a1 b1 a2 b2 a3...
    ?

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,638
    For the type of result you want - Try nesting them.
    Code:
    FOR a to N
       FOR b to N - 1
           do et cetera
       ENDFOR
    ENDFOR

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    That doesn't alternate them.

    In theory, you can do that with gotos. I'll hit you if you do

    Why do you need it?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    50
    I tried both but it just can't be compiled.
    I am not sure how to use 'goto'. Is it 'goto for(.......);'?

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,451
    If you only have one "unknown", you could just write:
    Code:
    for (int i = 0; i < unknown; ++i)
    {
        // print a and b here
    }
    If you really do need two "unknowns", then one way is to write:
    Code:
    bool flag = true;
    for (int i = 0; flag; ++i)
    {
        flag = false;
        if (i < unknown1)
        {
            // print a here
            flag = true;
        }
        if (i < unknown2)
        {
            // print b here
            flag = true;
        }
    }
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    What happens when unknown1 is different to unknown2 ?

    Do you just get the excess of the greater?
    Code:
    	for (a=0, b=0; a<unknown1 && b<unknown2 ; a++, b++)
    	{
    		cout<<"a"<<a<<" ";
    		cout<<"b"<<b<<" ";
    	}
    
    	for (; a<unknown1 ; a++)
    	{
    		cout<<"a"<<a<<" ";
    	}
    
    	for (; b<unknown2 ; b++)
    	{
    		cout<<"b"<<b<<" ";
    	}
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Registered User
    Join Date
    Oct 2006
    Posts
    50
    Thank you, everyone. Actually, I am attempting an exercise of array. It has to be 2 unknowns. Whether any 1 of the content of the array is excess or not is not important here. The code is like this:
    Code:
    int array1[10],array2[10];
    
    //user input size1(0-10) of array1
    
    //user input a list of integers to array1(no.  of integers=size1)
    
    //user input size2(0-10) of array2
    
    //user input a list of integers to array2(no.  of integers=size2)
    
    //display the content of array1 & array2 alternately
    If I use 1 unknown and any 1 of the arrays has empty content, wrong code will be output.

    This is the screenshot of the unwanted output and only 1 unknown is used here:

    http://img213.imageshack.us/img213/5849/unwantednw8.jpg

    If the empty content can be prevented from being displayed, then 1 unknown is enough but I just have no idea.
    Last edited by Roy01; 10-18-2006 at 07:36 AM.

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    This can be done rather simply with a single for loop. I don't want to give too much away because figuring this out is likely the point of the assignment, but your for loop doesn't have to be fancy. It can just loop from 0 to 10. Then inside the loop you output both the array1 and array2 value if they are valid.

  9. #9
    Registered User
    Join Date
    Oct 2006
    Posts
    50
    Quote Originally Posted by Daved
    This can be done rather simply with a single for loop. I don't want to give too much away because figuring this out is likely the point of the assignment, but your for loop doesn't have to be fancy. It can just loop from 0 to 10. Then inside the loop you output both the array1 and array2 value if they are valid.
    Thank you for your big hint! I forget I can use boolean value to do that! What an idiot!lol Now this part is fixed.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alternate Languages in C++
    By Kadell in forum C++ Programming
    Replies: 5
    Last Post: 07-03-2007, 04:35 AM
  2. alternate rendering loop
    By psychopath in forum Game Programming
    Replies: 2
    Last Post: 07-16-2005, 12:30 PM
  3. Alternate energy sources
    By Govtcheez in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 02-02-2005, 06:07 PM
  4. alternate to string.data()
    By Eber Kain in forum C++ Programming
    Replies: 1
    Last Post: 06-07-2004, 12:59 PM
  5. alternate function to sprintf
    By Eber Kain in forum C++ Programming
    Replies: 5
    Last Post: 12-27-2002, 09:38 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21