3 Replies - 1415 Views - Last Post: 03 October 2010 - 01:01 PM Rate Topic: -----

#1 shoot to thrill   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 30-May 09

Infinite loop with the 'find' function

Posted 03 October 2010 - 10:08 AM

Hi guys,

I am having some problems with a loop that Im using. The program is set up to do some calculations using an iterative loop, however I am finding that in most cases it crashes after an indefinite number of cycles (sometimes it just does 1 iteration, sometimes the specified number and everything in between). Anyway I isolated the problem to a 'find' loop that is being used to generate an array of 'N' variables with specific conditions. Here is the code in full;

#include <iostream>
#include <fstream>
#include <stdio.h>
#include <stdlib.h>
#include <ctime> 
#include <algorithm>
#include <set>
#include <math.h>

#define pi 3.14159265

using namespace std;

int main()
{
    srand(time(0));     //Initiate random number generator
    
    int N, m0, t;
    int* q;
    
    float p;
      
//-----------------------DEFINITIONS--------------------

    cout<<"Number of economic agents (even numbers only, upper limit ~ 30,000)?\n";
//    cin>> N;
N=6;    
//    cin.ignore();
    
    if (N % 2 != 0)
       {
       printf("Even number of agents required. Please restart program\n");
       cin.get();
       }
    
    cout<<"Value of 'p' (in between 0 and 1)?\n";
//    cin>> p;
p=0.5;
//    cin.ignore();
    
    cout<<"Value of initial starting money (for ALL nodes)?\n";
//    cin>> m0;
m0=1000;
//    cin.ignore();
    
    cout<<"Number of iterations?\n";
//    cin>> t;
t=10;
//    cin.ignore();
    cout<<endl;
    
    int S[N];
    float A[N], B[N], C[N], D[N];
    int x = 0;
    
    for (int i = 0; i < N; i++)
       {  
       A[i]  = m0;
       B[i]  = 0;
       }
       
//---------------------------MAIN LOOP--------------------------------


while (x < t)  
  {
        
  for (int i = 0; i < N; i++)
     {
     S[i] = 0;
     printf("%f ", A[i]);
     }
     cout<<endl;
       
  for (int j = 0; j < N; j++)
     {
     do { 
     S[j] = rand()%(N);
     q = find(S, S + j, S[j]);
     } while ((S[j] == j) || (q != S + j));          //conditionals
     }
     cout<<endl;
    
  for (int i = 0; i < N; i+=2)
     {
     B[(S[i])] = A[(S[i])] - p*(A[(S[i])]);             //BUYER
     B[(S[i + 1])] = A[(S[i + 1])] + p*(A[(S[i])]);     //SELLER
     }
    
  
  for (int i = 0; i < N; i++)
     {
     A[i] = B[i];
     }

  x++;  
  }
  
cin.get();
}





When I say I isolated the issue to the 'find' loop, I did this by placing simple cout statements in between each step in the program. However I may be wrong and the source of the issue may well be somewhere else.. the issue being (just to make sure its being understood properly) that the program will only execute so many iterations each time it is ran with no pattern in how many iterations it gets through. You may want to run the program and see for yourself! The strange thing is I have used a very similar 'find' loop before in another program with no problems whatsoever so Im not sure what could be causing problems with regards to this one??

By the way (just for clarification,) it is at the moment set up in a 'testing' config (via fixing the initial user-inputs). Also the 'BUYER' and 'SELLER' parts correspond to certain equations governing the dynamics of the model.

Any help on this issue is much appreciated anyway! :)

Thanks, Ryan

Is This A Good Question/Topic? 0
  • +

Replies To: Infinite loop with the 'find' function

#2 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6260
  • View blog
  • Posts: 24,030
  • Joined: 23-August 08

Re: Infinite loop with the 'find' function

Posted 03 October 2010 - 10:13 AM

The quickest way to debug a crash like this is to compile the program in debug mode and run it in a debugger. When it crashes it will stop and point to the exact line at which the catastrophic error is being generated, and you can check the variables at that point in time to narrow down and/or discover the exact cause.

EDIT: Your most likely culprit is here:
B[(S[i + 1])]


This is found in a loop where the bounding conditional i is between 0 and N - 1 (i.e., for (int i = 0; i < N; i+=2), and S is an array of size N. What happens here when you're in your final loop iteration and i = N - 1?


EDIT 2: Oops, never mind; as you're incrementing by two, i + 1 will be a valid array index in the last iteration.

This post has been edited by JackOfAllTrades: 03 October 2010 - 10:19 AM

Was This Post Helpful? 1
  • +
  • -

#3 shoot to thrill   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 30-May 09

Re: Infinite loop with the 'find' function

Posted 03 October 2010 - 12:20 PM

@JackOfAllTrades; Cheers for the tip and having a look dude! Yea Im still a bit of a noob when it comes to programming (as Im sure you can tell with the lengthiness of the code! haha). Though I didnt know you could use debugging in that regard so thanks for the tip! :)

Can anyone else possibly shed some light on what might be causing this issue please?

Thanks, Ryan
Was This Post Helpful? 0
  • +
  • -

#4 Guest_gues_haxx*


Reputation:

Re: Infinite loop with the 'find' function

Posted 03 October 2010 - 01:01 PM

What he means is, programmers don't just run the program and analyze the output. We have tools available for debugging.

With Visual Studio 2010, you build in DEBUG config, then hit "Start Debugging." You can set breakpoints at which you want the program to halt execution temporarily, and even view the contents of the variables you have declared in your program.

But more importantly you can step through your program watching the values of the variables, and making sure the flow of control and logic work as expected.

Please learn to do this, there are similar tools available for other compilers/IDEs.
Was This Post Helpful? 0

Page 1 of 1