I was given the challenge to write a program using 3 different arrays. Each used to find the maximum, minimum, and the median of 20 different randomized numbers. I have really no idea how to handle arrays and built the program to the best of my abilities. But instead of getting the actual answer I've gotten the really weird out put of 008E150F, 008E1514, and 008E150A respectively.
There are probably easier ways to go about this, (but I have been forbidden of changing the format of the arrays) and I am probably missing some vital logic that either escaped me or wasn't given. I have tried throwing out my entire code and starting over in a different way. But surprisingly my first attempt was my most successful. So far my code is...
#include <cmath>
#include <iostream>
#include<cstdlib>
#include <ctime>
#include <time.h>
#include <iomanip>
//Value arrays
//Given form for max_value
double max_value ( double ran[], int n)
{
double max_ran;
int k;
max_ran=ran[0];
for (k=1; k<n; k++)
{
if (ran[k]>max_ran)
max_ran=ran[k];
}
return max_ran;
}
//Given form for min_value
double min_value (double ran[], int n)
{
double min_ran;
int k;
min_ran=ran[0];
for (k=1;k<n;k++)
{
if (ran[k]<min_ran)
min_ran=ran[k];
}
return min_ran;
}
//Given form for med_value
double median_value (double ran[], int n)
{
double median_ran;
int k;
k=floor(n/2);
if(n %2 ==1)
median_ran=ran[k];
else
median_ran=(ran[k]+ran[k-1])/2;
return median_ran;
}
usingnamespace std;
int main()
{
//Call all variables
double integer;
double max=0;
//SRAND
srand((unsigned)time(0));
//Header
cout << "stage number \t " << " Random number " << endl;
//For loop for counting random numbers
for (integer=0; integer<21; integer++)
{
//random number
double ran = 0 + (rand()/((float)RAND_MAX/(1000-0)));
//out put random numbers
cout << integer << "\t\t " << ran << endl;
double max_value[20]= {ran};
double min_ran[20]= {ran};
double median_ran[20] = {ran};
}
//Skip a few lines
cout << "\n\n" << endl;
//Output max,min, and median
cout << "Max Value " << max_value << endl;
cout << "Min Value " << min_value<< endl;
cout << "Median Value " << median_value<< endl;
//Terminate program
system ("pause");
return 0;
}
The randomizer works perfectally. But I've been trying to feed in the 20 numbers generated into the arrays and simply cout'ing them but I get the weird outputs and I am at a lost on how to go about finding a solution.
An example of a given output would be.
stage number Random number
0 831.752
1 870.754
2 339.671
3 276.223
4 633.137
5 972.381
6 495.865
7 488.846
8 758.416
9 887.326
10 138.524
11 619.007
12 531.388
13 64.4856
14 159.673
15 774.377
16 777.856
17 79.7754
18 172.063
19 595.721
20 638.661
Max Value 001E11D1
Min Value 001E133E
Median Value 001E1055
Press any key to continue . . .
Any help/tips/suggestions/advice would be most appreciative. Thank you for taking the time to read this.
cout << "Max Value " << max_value << endl;
cout << "Min Value " << min_value<< endl;
cout << "Median Value " << median_value<< endl;
These lines are going to print memory addresses since that's what they are. Those names are the names of functions! What you're seeing is where in memory the compiler has decided to stick the functions you declared at the top of the code. If you want to actually call those functions, you'll have to say max_value(); or something similar with the correct parameters.
Lines 90, 91, and 92 are probably not doing what you think they're doing. They're creating new arrays on each pass through the loop with only one element. These arrays are then destroyed when the for loop ends because they go out of scope.
At line 71, where your variables are being declared, add a new declaration: double arrayOfRands[20]; then, just under the cout at line 87, say arrayOfRands[integer] = ran; The first line declares an array, the other one fills the array as you go through the loop.