Program crash during 'find' loop!

infinite loop due to syntax error?

Page 1 of 1

4 Replies - 1444 Views - Last Post: 02 October 2010 - 04:15 PM Rate Topic: -----

#1 shoot to thrill  Icon User is offline

  • New D.I.C Head

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

Program crash during 'find' loop!

Posted 01 October 2010 - 11:27 AM

Hi guys,

I have been having some problems with a 'find' 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. I isolated the problem to a 'find' loop that is being used to generate an array of 'N' variables with specific conditions;

 
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));
 }



Could anyone tell me how this may cause an infinite loop, or for the program to simply stop in it's tracks? It could simply be a syntax error or something to do with the random no. generator, though I'm not massively sure at this stage (as Im sure you can tell! lol)

Any help on this is much appreciated anyway! :)

Thanks, Ryan

This post has been edited by shoot to thrill: 01 October 2010 - 11:30 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Program crash during 'find' loop!

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10816
  • View blog
  • Posts: 40,320
  • Joined: 27-December 08

Re: Program crash during 'find' loop!

Posted 01 October 2010 - 11:56 AM

Can you show us more of the code? Also, what language are you working in? I'll move the thread to the appropriate forum. :)
Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8347
  • View blog
  • Posts: 31,913
  • Joined: 06-March 08

Re: Program crash during 'find' loop!

Posted 01 October 2010 - 07:26 PM

what is rand() ? what is find(...) ?
the one that Iknow is in the standard C library

This post has been edited by pbl: 02 October 2010 - 04:14 PM

Was This Post Helpful? 0
  • +
  • -

#4 shoot to thrill  Icon User is offline

  • New D.I.C Head

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

Re: Program crash during 'find' loop!

Posted 02 October 2010 - 03:15 PM

Ah just realised I posted this in the wrong part, my bad! Cheers for moving the thread macosxnerd :) The language is C++ by the way. Here is the code as it is 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] = C[i] = D[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));         
     }
     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];
     }
     cout<<endl;

  x++;
  cout<<endl; 
  
  }
  
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 (sometimes 1 or 2, sometimes the desired number, and everything in between haha :S). Anyway 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.

Thanks for help on this anyway guys, Ive got an incoming deadline to try and get results for so the help on this is much appreciated! :)

Ryan
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8347
  • View blog
  • Posts: 31,913
  • Joined: 06-March 08

Re: Program crash during 'find' loop!

Posted 02 October 2010 - 04:15 PM

move back to C/C++
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1