Thread: Multivariable For Loop

  1. #1
    Registered User Mcdom34's Avatar
    Join Date
    Jun 2012
    Location
    North Royalton, Ohio, United States
    Posts
    22

    Multivariable For Loop

    Greetings!
    I'm trying to iterate through two arrays, using one for loop, but whenever I try to print both array elements, I get an IndexOutOfBounds error. I've also tried using two different variables to keep track of the two array's indices, but that failed as well. Both of my methods are posted below.
    Code:
    for(int i = 0, j = 0; i < average.Length, j < high.Length; i++, j++){
                    Console.Write("  Week {0} -- {1}\t  Week {2} -- {3}\n", (i + 1), average[i], (j+1), high[j]); 
    }
    And this
    Code:
    for (int i = 0; i < average.Length; i++)
                    Console.Write("  Week {0} -- {1}\t  Week {0} -- {2}\n", (i + 1), average[i], high[i]);
    Also, every time I post code here, I get multiple extra blank lines. How can I get rid of those? Thanks.

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    The second method makes i increased until it is equal to average.Length-1 .This may surpasses borders of array high.

  3. #3
    Registered User Mcdom34's Avatar
    Join Date
    Jun 2012
    Location
    North Royalton, Ohio, United States
    Posts
    22
    Both arrays are of the same size, but array average is of type double and array high is of type int. Would that cause an issue?

  4. #4
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    double or int does not affect in this case.You are interested in the number of cells,not the type of them right now.Make sure that your arrays are allocated properly and no 'garbage' are held in it.

  5. #5
    Registered User
    Join Date
    Jun 2003
    Posts
    129
    Simple, one of the arrays is not as long as the other one.
    He who asks is a fool for five minutes, but he who does not ask remains a fool forever.

    The fool wonders, the wise man asks. - Benjamin Disraeli

    There are no foolish questions and no man becomes a fool until he has stopped asking questions. Charles Steinmetz

  6. #6
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    That loop assumes both arrays are the same length yet there is no guarantee from the code that they will be. This is a crash waiting to happen. Incidentally since i and j must be within the same exact range you can ditch j and just use i. If that does not completely iterate over both arrays then you are using the wrong construct to analyze their contents. At the very least you should check to ensure both arrays are the same length and throw an exception if they are not.
    Last edited by VirtualAce; 06-28-2012 at 05:12 PM.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > for(int i = 0, j = 0; i < average.Length, j < high.Length; i++, j++)
    So is the comma operator the same as boolean && in C and C++?

    Perhaps
    for(int i = 0, j = 0; i < average.Length && j < high.Length; i++, j++)
    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.

  8. #8
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by DanFraser View Post
    Simple, one of the arrays is not as long as the other one.
    Quote Originally Posted by VirtualAce View Post
    That loop assumes both arrays are the same length yet there is no guarantee from the code that they will be.
    Notice that Mcdom34 says the arrays are of same size.

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > Notice that Mcdom34 says the arrays are of same size.
    Saying it and proving it are two different things.

    If they were the same, where would IndexOutOfBounds come from?
    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.

  10. #10
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by Salem View Post
    > Notice that Mcdom34 says the arrays are of same size.
    Saying it and proving it are two different things.
    Correct

    If they were the same, where would IndexOutOfBounds come from?[/QUOTE] If i knew,i would have answered already

  11. #11
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Your compiler is telling you they are not the same length. Also before you go correcting me make sure you understand what I am saying. Although the code will work if the arrays are the same size there is no check to ensure they are. Quite simply this is bad code and has the potential to cause a crash.
    Last edited by VirtualAce; 06-29-2012 at 04:59 PM.

  12. #12
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Assuming that average.Length and high.Length are equal (the code will fail if they are not), I fail to see how
    Code:
    for(int i = 0, j = 0; i < average.Length, j < high.Length; i++, j++)
    {
                    Console.Write("  Week {0} -- {1}\t  Week {2} -- {3}\n", (i + 1), average[i], (j+1), high[j]);
    }
    differs from
    Code:
    for(int i = 0; i < average.Length; i++)
    {
                    Console.Write("  Week {0} -- {1}\t  Week {2} -- {3}\n", (i + 1), average[i], (i+1), high[i]);
    }
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  13. #13
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    Quote Originally Posted by Mcdom34 View Post
    Greetings!
    I'm trying to iterate through two arrays, using one for loop, but whenever I try to print both array elements, I get an IndexOutOfBounds error. I've also tried using two different variables to keep track of the two array's indices, but that failed as well. Both of my methods are posted below.
    Code:
    for(int i = 0, j = 0; i < average.Length, j < high.Length; i++, j++){
                    Console.Write("  Week {0} -- {1}\t  Week {2} -- {3}\n", (i + 1), average[i], (j+1), high[j]); 
    }
    And this
    Code:
    for (int i = 0; i < average.Length; i++)
                    Console.Write("  Week {0} -- {1}\t  Week {0} -- {2}\n", (i + 1), average[i], high[i]);
    Also, every time I post code here, I get multiple extra blank lines. How can I get rid of those? Thanks.
    The first is not even a legal C# expression. The standard states the conditional part of a for loop must be a boolean expression - while the first and third part are comma-delimited expression lists. I don't know what you're using to run this program but visual C# won't even compile that first piece.

    Anyhow, you have the exception - just catch it and check the values of i, j, and the .Length values, and you should see quickly where you're stepping beyond the boundary.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 10
    Last Post: 06-03-2011, 11:19 AM
  2. Replies: 23
    Last Post: 04-05-2011, 03:40 PM
  3. Multivariable issues
    By thintheherd in forum C Programming
    Replies: 1
    Last Post: 05-31-2010, 11:56 PM
  4. for loop ignoring scanf inside loop
    By xIcyx in forum C Programming
    Replies: 2
    Last Post: 04-17-2007, 01:46 AM
  5. Multivariable Functions
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 06-14-2003, 03:55 AM