Really stuck with this any help would be appreciated

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jan 2008
Posts: 3
Reputation: jf404 is an unknown quantity at this point 
Solved Threads: 0
jf404 jf404 is offline Offline
Newbie Poster

Really stuck with this any help would be appreciated

 
0
  #1
Jan 22nd, 2008
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

  1. for(int i=0; i<512; i=i+2) //loops through the pairs of traders
  2. {
  3. double r1 = rand();
  4. double r2 = rand();
  5. double max = RAND_MAX;
  6. double eta1 = r1/max;
  7. double eta2 = r2/max;
  8. // Generates random numbers eta1, eta2 between 0 and 1
  9. double P1 = order0[i].P_buy(t);
  10. double P2 = order0[i+1].P_buy(t);
  11.  
  12. if( order0[i].getbuy()==1 )//if i has bought
  13. {
  14. // and if i+1 has bought as well
  15. if( order0[i+1].getbuy() == 1)
  16. {
  17. //then create new level of hierarchy
  18. order1[i] = trader1(order0[i], order0[i+1]);
  19. }
  20. else if( eta2 <= P2 )// if i+1 has not bought, will buy
  21. {
  22. order0[i+1].setbuy(1); //trader buys
  23. //then create new level of hierarchy
  24. order1[i] = trader1(order0[i], order0[i+1]);
  25. }
  26. }
  27. // at this point we know i has not bought
  28. else if( order0[i+1].getbuy() == 1)
  29. {
  30. if( eta1 <= P1 )// if i buys
  31. {
  32. //then create new level of hierarchy
  33. order1[i] = trader1(order0[i], order0[i+1]);
  34. }
  35. }
  36. // so neither has bought, see if they enter market
  37. else if ( eta1 <= P1 && eta2 <= P2 )// if both buy
  38. {
  39. order0[i].setbuy(1);
  40. order0[i+1].setbuy(1);
  41. //create new level of hierarchy
  42. order1[i] = trader1(order0[i], order0[i+1]);
  43. }
  44. else if ( eta1 <= P1 )//only i buys
  45. {
  46. order0[i].setbuy(1);
  47. order0[i+1].setinf(1);// i+1 is influenced
  48. order0[i+1].sett1(t);
  49. }
  50. else if ( eta2 <= P2 )// only i+1 buys
  51. {
  52. order0[i+1].setbuy(1);
  53. order0[i].setinf(1);// i is influenced
  54. order0[i].sett1(t);
  55. }
  56. }
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,661
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1500
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Really stuck with this any help would be appreciated

 
0
  #2
Jan 22nd, 2008
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.
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.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3
Reputation: jf404 is an unknown quantity at this point 
Solved Threads: 0
jf404 jf404 is offline Offline
Newbie Poster

Re: Really stuck with this any help would be appreciated

 
0
  #3
Jan 22nd, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,661
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1500
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Really stuck with this any help would be appreciated

 
0
  #4
Jan 22nd, 2008
What compiler are you using ? The program is probably indexing beyond the bounds of arrays, or the index values are crap. Can't really help much without the whole program.
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.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 4
Reputation: wazzam is an unknown quantity at this point 
Solved Threads: 0
wazzam wazzam is offline Offline
Newbie Poster

Re: Really stuck with this any help would be appreciated

 
0
  #5
Jan 23rd, 2008
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
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 650 | Replies: 4
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC