Thread: alternate for loops? possible?

  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
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    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,895
    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
    28,413
    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;
        }
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    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.

  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,366
    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, 07: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, 10:38 PM