Thread: Can you place two arrays in one loop?

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    32

    Can you place two arrays in one loop?

    Hello, I was wondering if there's a way to place two arrays in one loop. For example, say I had an integer and double array of the same size, let's say 30. I want to place 30 random values in each of them in one loop, and then display each value for both of them in a second loop.

    Code:
    int setArray[30];
    srand(time(0));
     
    int i;
    for (i=0; i < 30; i++)
    {
    int r = rand ( )%31;
    setArray[i] = r;
    }
    ^ How would I put the double in there as well?

    Thanks!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You can access both the arrays in the loop body using the same index.
    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

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    32
    thank you!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Loop that contains arrays
    By christianB in forum C Programming
    Replies: 13
    Last Post: 07-20-2011, 07:58 AM
  2. help with for loop with arrays
    By jorgejags in forum C Programming
    Replies: 4
    Last Post: 11-03-2008, 06:42 PM
  3. pointers and linked lists in place of arrays
    By kordric in forum C++ Programming
    Replies: 8
    Last Post: 05-14-2008, 10:59 AM
  4. Arrays and For Loop
    By Jack1982 in forum C++ Programming
    Replies: 9
    Last Post: 10-10-2007, 04:57 AM
  5. Loop printing arrays
    By CHurst in forum C Programming
    Replies: 2
    Last Post: 12-14-2005, 08:13 PM