I've been focusing my latest programming exercises in exploiting stack allocated C arrays, since I want to take advantage of each feature of the language to solve particular problems, whether there are better ways around them or not (after this, I'll try to rewrite the program to use vectors, std::arrays and std::sort).
In this case, I wanted to build a basic data management program, able to caculate the mean, median and mode(or at least one of them) of any array of data , using my own routings to sort it and getting the desired results. All of them use templates, since I would be using all sorts of numeric values:
//Returns the median of an array
template<class T>
float getMedian(T array[], unsigned int size)
{
T newArray[size];
copyArray(newArray, array, size);
orderValues(newArray, size);
if(size % 2 == 0)
return (newArray[size / 2] + newArray[(size - 1) / 2]) / 2;
else
return newArray[size / 2];
}
//Returns the mean of an array.
template<typename T>
float getMean(T array[], unsigned int size)
{
float sum = 0;
for(int i = 0; i < size; i++)
sum += (float)array[i];
return sum / size;
}
//Returns the mode of an array
template<typename T>
T getMode(T array[], unsigned int size)
{
T newArray[size];
copyArray(newArray, array, size);
orderValues(newArray, size);
int alreadyCounted[size];
int modeArray[size];
bool access;
fillArrayWithCeros(alreadyCounted, size);
fillArrayWithCeros(modeArray, size);
for(int i = 0; i < size; i++)
{
if(alreadyCounted[i] == 0)
{
for(int j = i; j < size; j++)
{
int count = 0;
if(newArray[j] == newArray[i] && j != i)
{
modeArray[i]++;
alreadyCounted[j] = 1;
}
}
}
}
return newArray[getHigherIndexFrom(modeArray, size)];
}
The resulting functions perform pretty well with hard coded values:
int main()
{
float arrayFloat[] = {1.56, 1.49, 1.51, 1.53, 1.52, 1.50, 1.54,
1.48, 1.49, 1.49, 1.49,
1.49, 1.49, 1.48, 1.50, 1.49, 1.49, 1.50, 1.53, 1.48, 1.56,
1.50, 1.54, 1.48, 1.47, 1.50, 1.50, 1.54, 1.53, 1.52, 1.49,
1.52, 1.54, 1.53, 1.49, 1.50, 1.48, 1.47, 1.51, 1.50, 1.49,
1.54, 1.52, 1.51, 1.54, 1.55, 1.53, 1.56, 1.50, 1.49, 1.47,
1.49, 1.52, 1.54, 1.51, 1.52, 1.48, 1.53, 1.50, 1.53, 1.49,
1.53, 1.50, 1.47, 1.50, 1.51, 1.50, 1.51, 1.51, 1.55, 1.54,
1.53, 1.52, 1.50, 1.48, 1.50, 1.49, 1.53, 1.50, 1.52, 1.56,
1.50, 1.54, 1.52, 1.49, 1.49, 1.48, 1.49, 1.55, 1.52, 1.56,
1.49, 1.48, 1.50, 1.49, 1.51, 1.54, 1.52, 1.55, 1.49 };
int arrayInt[] = {115, 25, 57, 74, 72, 41, 97, 15, 30, 21, 29,
31, 31, 9, 42, 22, 21, 46, 77, 7, 114,
34, 91, 19, 4, 36, 36, 96, 74, 65, 29,
69, 91, 86, 23, 42, 7, 0, 58, 38, 21,
87, 69, 52, 96, 112, 74, 118, 37, 31, 4,
31, 66, 95, 50, 71, 11, 87, 46, 86, 31,
76, 35, 2, 42, 58, 44, 49, 58, 103, 87,
80, 69, 36, 11, 45, 27, 86, 42, 65, 118,
46, 96, 63, 21, 25, 13, 33, 112, 60, 119,
23, 16, 34, 25, 58, 92, 70, 107, 30, 53,
74, 110, 1, 111, 1, 46, 18, 88, 89, 83,
86, 15, 59, 29, 36, 84, 43, 70, 76, 103 };
//Shows the entire array on the screen
printArray(arrayInt, arraySize(arrayInt));
//Calls the functions that get the mean, the median and the mode of the array in std::cout statements.
printArrayInfo(arrayInt, arraySize(arrayInt));
printArray(arrayFloat, arraySize(arrayFloat));
printArrayInfo(arrayFloat, arraySize(arrayFloat));
}
Output:
Population: 121
115, 25, 57, 74, 72, 41, 97, 15, 30, 21, 29,
31, 31, 9, 42, 22, 21, 46, 77, 7, 114,
34, 91, 19, 4, 36, 36, 96, 74, 65, 29,
69, 91, 86, 23, 42, 7, 0, 58, 38, 21,
87, 69, 52, 96, 112, 74, 118, 37, 31, 4,
31, 66, 95, 50, 71, 11, 87, 46, 86, 31,
76, 35, 2, 42, 58, 44, 49, 58, 103, 87,
80, 69, 36, 11, 45, 27, 86, 42, 65, 118,
46, 96, 63, 21, 25, 13, 33, 112, 60, 119,
23, 16, 34, 25, 58, 92, 70, 107, 30, 53,
74, 110, 1, 111, 1, 46, 18, 88, 89, 83,
86, 15, 59, 29, 36, 84, 43, 70, 76, 103
Mean: 54.5455
Median: 49.0000
Mode: 31
Population: 100
1.56, 1.49, 1.51, 1.53, 1.52, 1.50, 1.54, 1.48, 1.49, 1.49, 1.49,
1.49, 1.49, 1.48, 1.50, 1.49, 1.49, 1.50, 1.53, 1.48, 1.56,
1.50, 1.54, 1.48, 1.47, 1.50, 1.50, 1.54, 1.53, 1.52, 1.49,
1.52, 1.54, 1.53, 1.49, 1.50, 1.48, 1.47, 1.51, 1.50, 1.49,
1.54, 1.52, 1.51, 1.54, 1.55, 1.53, 1.56, 1.50, 1.49, 1.47,
1.49, 1.52, 1.54, 1.51, 1.52, 1.48, 1.53, 1.50, 1.53, 1.49,
1.53, 1.50, 1.47, 1.50, 1.51, 1.50, 1.51, 1.51, 1.55, 1.54,
1.53, 1.52, 1.50, 1.48, 1.50, 1.49, 1.53, 1.50, 1.52, 1.56,
1.50, 1.54, 1.52, 1.49, 1.49, 1.48, 1.49, 1.55, 1.52, 1.56,
1.49, 1.48, 1.50, 1.49, 1.51, 1.54, 1.52, 1.55, 1.49
Mean: 1.5099
Median: 1.5000
Mode: 1.4900
However, in order to get bigger chunks of data, I decided to write a function that fills the array with random values:
//Fills an array with random numbers
template<typename T>
void fillArrayRandomly(T array[], unsigned int size, T maxValue, T minValue = 0)
{
srand(unsigned(time(0)));
for(int i = 0; i < size; i++)
{
//I used static_cast to convert rand into the desired type (like a float or a double)
array[i] = minValue + static_cast<T>(rand()) / (static_cast<T>(RAND_MAX / (maxValue - minValue)));
}
}
Random numbers with integers worked just fine, but the same floats randomly generated get all the operations wrong.
Population: 121
115, 25, 57, 74, 72, 41, 97, 15, 30, 21, 29,
31, 31, 9, 42, 22, 21, 46, 77, 7, 114,
34, 91, 19, 4, 36, 36, 96, 74, 65, 29,
69, 91, 86, 23, 42, 7, 0, 58, 38, 21,
87, 69, 52, 96, 112, 74, 118, 37, 31, 4,
31, 66, 95, 50, 71, 11, 87, 46, 86, 31,
76, 35, 2, 42, 58, 44, 49, 58, 103, 87,
80, 69, 36, 11, 45, 27, 86, 42, 65, 118,
46, 96, 63, 21, 25, 13, 33, 112, 60, 119,
23, 16, 34, 25, 58, 92, 70, 107, 30, 53,
74, 110, 1, 111, 1, 46, 18, 88, 89, 83,
86, 15, 59, 29, 36, 84, 43, 70, 76, 103
Mean: 54.5455
Median: 49.0000
Mode: 31
Population: 100
1.56, 1.49, 1.51, 1.53, 1.52, 1.50, 1.54, 1.48, 1.49, 1.49, 1.49,
1.49, 1.49, 1.48, 1.50, 1.49, 1.49, 1.50, 1.53, 1.48, 1.56,
1.50, 1.54, 1.48, 1.47, 1.50, 1.50, 1.54, 1.53, 1.52, 1.49,
1.52, 1.54, 1.53, 1.49, 1.50, 1.48, 1.47, 1.51, 1.50, 1.49,
1.54, 1.52, 1.51, 1.54, 1.55, 1.53, 1.56, 1.50, 1.49, 1.47,
1.49, 1.52, 1.54, 1.51, 1.52, 1.48, 1.53, 1.50, 1.53, 1.49,
1.53, 1.50, 1.47, 1.50, 1.51, 1.50, 1.51, 1.51, 1.55, 1.54,
1.53, 1.52, 1.50, 1.48, 1.50, 1.49, 1.53, 1.50, 1.52, 1.56,
1.50, 1.54, 1.52, 1.49, 1.49, 1.48, 1.49, 1.55, 1.52, 1.56,
1.49, 1.48, 1.50, 1.49, 1.51, 1.54, 1.52, 1.55, 1.49
Mean: 1.5100 <- Wrong(badly rounded).
Median: 1.5046 <- Wrong.
Mode: 1.5590 <- Wrong.
I think it must be a problem with static casting the random number generator. Can someone give me some inside into this issue? Why is the random number generator affecting the results?
Aucun commentaire:
Enregistrer un commentaire