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

New Topic/Question
Reply



MultiQuote


|