Generating arrays

I have an assignment to generate two arrays of 100 values. One with a range of 0.30 - 1.00 and the other with a seed 1990*1*10. This is to be generated with a set precision of 4 decimals.

This is the first part of the assignment, I have the complete program unfinished but I'm uncertain to as if I even have this array correct.

1
2
3
4
5
6
7
8
9
10
11
12
	for(int i=0; i<100; i++)
	{
		array1[i] = (double)rand()/RAND_MAX*(1.00-0.30)+0.30;
	}
	
	srand(1990*1*10);
	for(int i=0; i<100; i++)
	{
		array2[i] = (double)rand()/RAND_MAX*(1.00-0.30)+0.30;
	}

	cout << setprecision(4) << endl;
It looks right to me if array one and two are
1
2
double array1[100];
double array2[100];
Yes they are. Now knowing that it is right, I do get results from my stats functions like mean. But they look like -3.225e-232, I should be getting numbers from 0.30 - 1.00, is something wrong with my set precision value?

Additionally, it did not look right at first because my max and min values are being outputted as 0. I have a function to sort the values in my header file that was assigned to us.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
void sort(double x[], int n)
{
	// Declare objects.
	int m;
	double hold;
	
	// Implement selection sort algorithm.
	for (int k=0; k<=n-2; ++k)
	{
		// Find position of smallest value in array
		// beginning at k
		m = k;
		for (int j=k+1; j<n-1; ++j)
		{
			if (x[j] < x[m])
				m = j;
		}
		// Exchange smallest value with value at k
		hold = x[m];
		x[m] = x[k];
		x[k] = hold;
	}
	
	return;
}


and this is what I have in my main file
1
2
3
	void sort(double array1[100], int array1);
	void sort(double array2[100], int array2);
	cout << setprecision(4) << array1 << endl;

int is referring to the integers in array1 and array2 so it should sort and set the values within 4 decimals for all 100 values correct?

I think I need to create a search index? Which I think is put in the beginning where I assign my variables which would be
1
2
3
4
	double array1[100], array2[100], max1, min1, mean1, med1,
		var1, stddev1, max2, min2, mean2, med2, var2, stddev2;
	int search_index;
		search_index = search(array1, 100, 0.725);


I'm almost there...
I don't think there is anything wrong with your precision value. I am wondering if your use of Rand() is wrong.

could you tell me the value of RAND_MAX?

The test I ran On just the random values with a arbitrary number didn't give me anything near that value range and the two numbers I tried were 70 and .7

I even tried, which gave me the range I wanted but not to 4 places:
 
(double)(rand() % 70 + 30)/100


I just tried a test like:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

#include <iostream>
#include <cstdlib>

using namespace std;

int main()
{
    double ArrayOne[100];

    for(int i = 0; i < 100; i++)
    {
        ArrayOne[i] = (double)(rand() % 70 + 30)/100;
//        ArrayOne[i] = (double)rand()/.7*(1.0-0.3)+0.3;
        cout << i << ": " << ArrayOne[i] << endl;
    }
}

and your equation I got numbers completely out of range.

I thought they looked right but I had to test them to be sure.

Last edited on
I think I figured out the problem I had. It wasn't that my array wasn't generating the random values and the random values for array 2 with the seed.

In my second attempt, I changed my set precision output to
cout << setprecision(4) << array1 << endl;
where I had array1, thinking it would set the value to 4 decimals to array1. If I just took it out, the set precision would output for all arrays above right? Doing this will give me my output values correctly instead of the -3.345e-434 I was getting?

Additionally, when finding the min and max values of my array, I had used the sort function provided using my header file.

I had
1
2
	void sort(double array1[100], int array1);
	void sort(double array2[100], int array2);

which needs to be
1
2
	sort(array1, int 100);
	sort(array2, int 100);


Will try these changes when I get home tonight, this is more or less for verficiation and noting the changes to look back on.
The mistake in my cout was not the problem. I'm still getting back odd numbers such as -3.225e-323 and 0's for my max/min values. The value for RAND_MAX is something along the lines of 32,xxx isn't it?

array1 should be generating numbers from 0.30 - 1.00 without a seed, so we should be getting values in the same order.

This is what I have now.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
	double array1[100], array2[100], max1, min1, mean1, med1,
		var1, stddev1, max2, min2, mean2, med2, var2, stddev2;
	
	for(int i=0; i<100; i++)
	{
		array1[i] = ((double)rand())/(RAND_MAX*(1.00-0.30)+0.30);
	}
	
	srand(4003);
	for(int i=0; i<100; i++)
	{
		array2[i] = ((double)rand())/(RAND_MAX*(1.00-0.30)+0.30);
	}
	sort(array1,100);
	sort(array2,100);
	cout << setprecision(4) << endl;


anyone?
¿How are you printing the arrays? (and obtainning the other values)


1
2
3
4
5
//array1[i] = ((double)rand())/(RAND_MAX*(1.00-0.30)+0.30);
//the parenthesis are wrong

double random = ((double) rand())/RAND_MAX; // -> [0;1]
return random*delta + inic;
Using functions to get information from the array
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
	cout << "Values for Array 1" << endl;
	
        maxval(array1, max1);
	cout << "Maximum Value: " << max1 << endl;
	
        minval(array1, min1);
	cout << "Minimum Value: " << min1 << endl;
	
	mean(array1, mean1);
	cout << "Mean: " << mean1 << endl;
	
	median (array1, med1);
	cout << "Median: " << med1 << endl;
	
	variance(array1, var1);
	cout << "Variance: " << var1 << endl;
	
	std_dev(array1, stddev1);
	cout << "Standard Deviation: " << stddev1 << endl << endl;


Am I not calling the functions correctly?
This is what is in my header file.
1
2
3
4
5
6
double maxval(const double x[], int n);
double minval(const double x[], int n);
double mean(const double x[], int n);
double median(const double x[], int n);
double variance(const double x[], int n);
double std_dev(const double x[], int n);
double maxval(const double x[], int n); The parameters are the array, and the number of items that the array holds.
The function returns the maximum value.
max1 = maxval(array1, 100);
Understood. So additionally, a search index function like
int search(const double A[], int n, double value);
is the function, of the arbitrary array, # of items of array, and the value to be indexed?
would look like
1
2
	searchval = search(array1, 100, 0.725);
	cout << "The index of 0.725: " << searchval << endl << endl;

Where I have searchval declared as an int in the beginning of the program?
Topic archived. No new replies allowed.