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.
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.
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
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:
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.
Am I not calling the functions correctly?
This is what is in my header file.
1 2 3 4 5 6
double maxval(constdouble x[], int n);
double minval(constdouble x[], int n);
double mean(constdouble x[], int n);
double median(constdouble x[], int n);
double variance(constdouble x[], int n);
double std_dev(constdouble x[], int n);
double maxval(constdouble 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(constdouble 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?