Help with this For Loop

This is a discussion on Help with this For Loop within the C++ Programming forums, part of the General Programming Boards category; Here is part of my code. The rest is just the function definitions. .I have an array of 20 x ...

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    7

    Question Help with this For Loop

    Here is part of my code. The rest is just the function definitions. .I have an array of 20 x 20 that outputs the temperature of a plate. I need to reiterate through a loop until no cell in the array changes more than 0.1 degree(I refresh the values through every iteration. How would you monitor the largest change for any cell in an array in order to determine when to stop iterating? Right now I have tried, but the below doesn't output correctly. I believe it is because I am incorrectly defining my previous one to compare the current one with. Any ideas?
    Code:
     while (true)
    {
        bool update = true;
        for (int a = 1; a < array_size -1; a++)
        {
            for (int b = 1; b < array_size -1; b++)
            {
                
                    hot_plate[a][b] = sum_cell(hot_plate, a, b);
                
            }
        }
        
        
        for (int a = 1; a < array_size-1; a++)
        {
            for (int b = 1; b < array_size-1; b++)
            {
                hot_plate_next[a][b]=sum_cell(hot_plate_next, a,b);
                if (abs(hot_plate_next[a][b] - hot_plate[a][b]) < 0.1)
                {
                    update = false;
                }
                hot_plate_next[a][b] = hot_plate[a][b];
                cout << hot_plate[a][b] << " ";
            }
    
    
        }
        
        if (!update) break;
    }
    Last edited by skittlesaddict; 10-18-2012 at 09:49 AM.

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    2,584
    all of your instances of "b < array_size - 1" should be "b < array_size"
    also, array indexes start at zero, not one.

    an array of 20 items has indexes 0 to 19, and it looks like your for loops iterate over a range of 1 to 18. I'm only guessing at the value of "array_size" based on your explanation, because it's not declared in your code snippet.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 12-26-2011, 07:36 PM
  2. Replies: 23
    Last Post: 04-05-2011, 04:40 PM
  3. Help.. newbie for loop..stuck with the loop..
    By jochen in forum C Programming
    Replies: 15
    Last Post: 10-01-2007, 01:31 AM
  4. for loop ignoring scanf inside loop
    By xIcyx in forum C Programming
    Replies: 2
    Last Post: 04-17-2007, 02:46 AM
  5. stays in loop, but it's not an infinite loop (C++)
    By Berticus in forum C++ Programming
    Replies: 8
    Last Post: 07-19-2005, 12:17 PM

Tags for this Thread


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