On the plus side, I ordered 3 books online last night for c and c++. They're not exactly new, but better than nothing, right? Anyways.
This is absolutely horrible, I know. If I can get it to work, I'll try and clean it up to put the functions outside of main and then call them and such, probably work on my documentation too. But this is my code thus far.
I've tried a few different things such as making most things float so I wouldn't have to convert/cast, and that failed spectacularly. My values are ints. I attempted making an array of type float in hopes I could just use that for the methods that are of type float. However, I'm still getting 0. I've attempted making another variable of type float to use in the division aspect of the calculation, but I couldn't get the value of a to transfer to it. Or rather, it would and be 0. I'm not used to this where if you declare a variable as 0 at the beginning, it stays that way and that the stored value doesn't change. If I make the variable mean an int, I get the correct whole number but 0 if it's float.
Code:
#include <stdlib.h>#include <stdio.h>
#include <time.h>
#include <math.h>
//code from qsort example
int intComp(const void* a, const void* b) {
int valA = *((int*)a);
int valB = *((int*)b);
if (valA < valB) return -1;
if (valA == valB) return 0;
else return 1;
}
//Main program. PS: It's a REAL mess. Gomen(sorry).
int main() {
int numElem = 0;
int *array = (int*) malloc(numElem * sizeof(int));
float *array2 = (float*) malloc((int)array * sizeof(float));
int i;
int max;
int min;
float mean;
float median;
int mode;
float stdev;
int a = 0;
int temp = 0;
printf("Enter the number of elements: \n");
scanf("%d", &numElem);
printf("Now enter the elements one at a time.\n");
for (i = 0; i < numElem; i++) {
printf("Element %d: ", i);
scanf("%d", &array[i]);
}
//Sorts the array for easier use
qsort(array, numElem, sizeof(int), intComp);
//Makes a second array type float.
for(i = 0; i < numElem; i++){
array2[i] = array[i];
}
printf("Sorted Array:\n"); //test start
for(i = 0; i < numElem; i++){
temp = array[i];
printf("%d ", temp);
}
printf("\n---\n");
temp = 0;
printf("Float Array:\n");
for(i = 0; i < numElem; i++){
temp = array2[i];
printf("%d ", temp);
}
printf("\n---\n"); //test end
//Computes the max
max = array[numElem-1];
//Computes the min
min = array[0];
//TODO: Compute the mean
//computes the sum of array elements
for(i = 0; i < numElem; i++){
a += array2[i];
}
printf("sum: %d\n", a); //test
//sum of elements divided by the number of elements
mean = a/numElem;
//TODO: Compute the median
//if number of elements is even
if(numElem%2==0){
a = array[numElem/2] + array[numElem/2-1]/2;
median = array[a];
printf("num/2 = %d , num/2-1 = %d\n", array[numElem/2], array[numElem/2-1]);
}
//if number of elements is not even
else {
median = array[numElem/2];
}
//TODO: Compute the mode
for(i = 0; i < numElem; i++){
}
//TODO: Compute the standard deviation
//Prints the computed values next to appropriate lables
printf("Max: %d\n", max);
printf("Min: %d\n", min);
printf("Mean: %f\n", mean);
printf("Median: %f\n", median);
printf("Mode: %d\n", mode);
printf("StDev: %f\n", stdev);
free(array);
free(array2);
return 0;
}
Edit: Well heck....I just realized part of my reason now. In my print statements. I was using %d instead of %f.
So my problem now is. I get 4.00000 instead of 4.33333. I'm going to assume this has to do with casting or that the variables in the equation are of int instead of float? This hitch is also holding me up on calculating the median if the number of items are positive.