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 = 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

``````/Program name = random.cpp
#include <cstdlib>
#include <iostream>

using namespace std;

int number_generator();
int main()
{
int series;
double average, times, cut_point;
double series_number;
double time_series_average;
double sum;
cout << "How long you want your series: ";
cin >> series;
cout << "How many times do you want to run the series: ";
cin >> times;
cout << "What is your cutting point: ";
cin >> cut_point;

for (int j=0; j<times; j++)
{
for (int i=0; i<series; i++)
{
sum = 1;
double number_to_use = rand() % 9999;
double actual_random_integer = number_to_use/1000;
//cout << "number to use = " << number_to_use <<
//cout << i <<  "= " << actual_random_integer << endl;
if (actual_random_integer > cut_point && sum[j] > 0)
{
series_number[i] = 1;
}
else if (actual_random_integer > cut_point && sum[j] < 0)
{
series_number[i] = -1;
}
else if (actual_random_integer < cut_point && sum[j] > 0)
{
series_number[i] = 1;
}
else if (actual_random_integer < cut_point && sum[j] < 0)
{
series_number[i] = -1;
}
//cout << "Series nummber [" << i << "]" << series_number[i] << endl;
sum[j]+=series_number[i];
//cout << "sum = " << sum[j] << endl;
}
time_series_average[j] = sum[j]/series;
cout << "(" << j  <<  ") "  << time_series_average[j] << endl;
//delete [] time_series_average;
}
//cout << sum << endl;
//average = sum/series;
//cout << "AVERAGE = " << average << endl;
}``````

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.