| | |
Really stuck with this any help would be appreciated
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Jan 2008
Posts: 3
Reputation:
Solved Threads: 0
I've been working on this code for a simulation, but strange things keep happening. The trader object is (int buy, int inf, int t1). The values of buy and inf should only be 0 or 1, but i keep getting ridiculously high and even negative values and I cant see why. The t1 ends up being around e^-324 sometimes. can anyone tell me why?
Thanks
J
Thanks
J
cpp Syntax (Toggle Plain Text)
for(int i=0; i<512; i=i+2) //loops through the pairs of traders { double r1 = rand(); double r2 = rand(); double max = RAND_MAX; double eta1 = r1/max; double eta2 = r2/max; // Generates random numbers eta1, eta2 between 0 and 1 double P1 = order0[i].P_buy(t); double P2 = order0[i+1].P_buy(t); if( order0[i].getbuy()==1 )//if i has bought { // and if i+1 has bought as well if( order0[i+1].getbuy() == 1) { //then create new level of hierarchy order1[i] = trader1(order0[i], order0[i+1]); } else if( eta2 <= P2 )// if i+1 has not bought, will buy { order0[i+1].setbuy(1); //trader buys //then create new level of hierarchy order1[i] = trader1(order0[i], order0[i+1]); } } // at this point we know i has not bought else if( order0[i+1].getbuy() == 1) { if( eta1 <= P1 )// if i buys { //then create new level of hierarchy order1[i] = trader1(order0[i], order0[i+1]); } } // so neither has bought, see if they enter market else if ( eta1 <= P1 && eta2 <= P2 )// if both buy { order0[i].setbuy(1); order0[i+1].setbuy(1); //create new level of hierarchy order1[i] = trader1(order0[i], order0[i+1]); } else if ( eta1 <= P1 )//only i buys { order0[i].setbuy(1); order0[i+1].setinf(1);// i+1 is influenced order0[i+1].sett1(t); } else if ( eta2 <= P2 )// only i+1 buys { order0[i+1].setbuy(1); order0[i].setinf(1);// i is influenced order0[i].sett1(t); } }
Last edited by WolfPack; Jan 22nd, 2008 at 12:06 pm. Reason: Improved the indenting. For god's sake, use the preview button before posting long code.
You need to use your compiler's debugger to step through that code and find out what is causing the problem because it isn't apparent from what you posted.
I can tell you that lines 6 and 7 will produce very very small numbers. The purpose of dividing the return value of rand() by RAND_MAX (which is a huge number) isn't apparent.
I can tell you that lines 6 and 7 will produce very very small numbers. The purpose of dividing the return value of rand() by RAND_MAX (which is a huge number) isn't apparent.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
•
•
Join Date: Jan 2008
Posts: 3
Reputation:
Solved Threads: 0
Thanks for the reply. The thing is whenever I try to debug, the debugger quits... I'm also getting an error message that says 'run time error: the stack around the variable order 0 was corrupted, but i only get this every so often. I really don 't understand...
PS the reason for lines 6 and 7 is to return a probability between 0 and 1, hence dividing by rand max
PS the reason for lines 6 and 7 is to return a probability between 0 and 1, hence dividing by rand max
•
•
Join Date: Jan 2008
Posts: 4
Reputation:
Solved Threads: 0
Hi,
There's a really good chance that the problem is not in the function you posted.
Your compiler is right - your stack IS GETTING CORRUPTED and I can tell this from the intermittent values you describe seeing.
Chances are you have an array allocated on the stack (as in a local var) and you've gone past the end. For this to occur, it's probably happening in a function (or method) being called inside the one you posted. You might want to try a tool like valgrind if you're on Linux/x86.
Regards,
Warwick
There's a really good chance that the problem is not in the function you posted.
Your compiler is right - your stack IS GETTING CORRUPTED and I can tell this from the intermittent values you describe seeing.
Chances are you have an array allocated on the stack (as in a local var) and you've gone past the end. For this to occur, it's probably happening in a function (or method) being called inside the one you posted. You might want to try a tool like valgrind if you're on Linux/x86.
Regards,
Warwick
![]() |
Similar Threads
- Circles .... Locus... (C++)
- Stuck in safe mode (Windows NT / 2000 / XP)
- Stuck with the Trojan Cachecache kit! Please help! (Viruses, Spyware and other Nasties)
- JAVA animation problem - animating a bezier curve !!! (Java)
- Windows XP is stuck! (Windows NT / 2000 / XP)
- Can Somebody Please Help Me On My Coursework. I Am Well 'n' Truly Stuck (Windows NT / 2000 / XP)
- Advertisment icons stuck to my desktop!!! (Viruses, Spyware and other Nasties)
- Stuck on Mail() Function (PHP)
- ie6 hangs and so does ie6setup.exe (Web Browsers)
Other Threads in the C++ Forum
- Previous Thread: returning array
- Next Thread: C++ problem - new to the language and stuck
Views: 650 | Replies: 4
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api application array arrays assignment beginner binary bitmap c++ c/c++ calculator char class classes code coding compile compiler console conversion convert count data database delete developer display dll email encryption error file forms fstream function functions game generator getline givemetehcodez graph homeworkhelper iamthwee ifstream image input int java lazy lib loop looping loops map math matrix memory multidimensional multiple newbie news node number output parameter pointer problem program programming project proxy python random read recursion recursive reference return sort sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






