Problems with my if loop

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jan 2008
Posts: 37
Reputation: gcardonav is an unknown quantity at this point 
Solved Threads: 0
gcardonav gcardonav is offline Offline
Light Poster

Problems with my if loop

 
0
  #1
Nov 2nd, 2009
Hi guys:

The following code is supposed to the following:
1 - Receive a random number and divide the number by 1000
2 - Then it needs to check if the number is greater than the series number and the cut point and convert the divided random number to either 1 or -1.
3 - Then do the sum of the (-1 and 1) and this will be my new series number.
4 - Then it will check again.

There is an initial condition that the sum[0] = 1. The cut points I need to use are either 7.5, 5 or 2.5.

My problem is that the code only returns a bunch of -1. I am certain that my problem is in my if ...else if statements. Can anyone point me into the right direction Please don't hesitate to ask any questions, I take criticism pretty well. Here is the code.

G

  1. /Program name = random.cpp
  2. #include <cstdlib>
  3. #include <iostream>
  4.  
  5. using namespace std;
  6.  
  7. int number_generator();
  8. int main()
  9. {
  10. int series;
  11. double average, times, cut_point;
  12. double series_number[1000001];
  13. double time_series_average[100];
  14. double sum[100];
  15. cout << "How long you want your series: ";
  16. cin >> series;
  17. cout << "How many times do you want to run the series: ";
  18. cin >> times;
  19. cout << "What is your cutting point: ";
  20. cin >> cut_point;
  21.  
  22. for (int j=0; j<times; j++)
  23. {
  24. for (int i=0; i<series; i++)
  25. {
  26. sum[0] = 1;
  27. double number_to_use = rand() % 9999;
  28. double actual_random_integer = number_to_use/1000;
  29. //cout << "number to use = " << number_to_use <<
  30. //cout << i << "= " << actual_random_integer << endl;
  31. if (actual_random_integer > cut_point && sum[j] > 0)
  32. {
  33. series_number[i] = 1;
  34. }
  35. else if (actual_random_integer > cut_point && sum[j] < 0)
  36. {
  37. series_number[i] = -1;
  38. }
  39. else if (actual_random_integer < cut_point && sum[j] > 0)
  40. {
  41. series_number[i] = 1;
  42. }
  43. else if (actual_random_integer < cut_point && sum[j] < 0)
  44. {
  45. series_number[i] = -1;
  46. }
  47. //cout << "Series nummber [" << i << "]" << series_number[i] << endl;
  48. sum[j]+=series_number[i];
  49. //cout << "sum = " << sum[j] << endl;
  50. }
  51. time_series_average[j] = sum[j]/series;
  52. cout << "(" << j << ") " << time_series_average[j] << endl;
  53. //delete [] time_series_average;
  54. }
  55. //cout << sum << endl;
  56. //average = sum/series;
  57. //cout << "AVERAGE = " << average << endl;
  58. }
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,842
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 503
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster
 
0
  #2
Nov 2nd, 2009
I'm not sure than sum[] array is initialized except for index 0. also, there is no call to srand, so I imagine you'd get the same results every time. You need to initialize the sum[] array before you use it.
Reply With Quote Quick reply to this message  
Reply

Tags
if...else, loop

Message:




Views: 342 | Replies: 1
Thread Tools Search this Thread



Tag cloud for if...else, loop
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC