Alright, so... I've got a program that needs to find the mean and standard deviation of a number of scores that are inputted by the user.
Of course, for this assignment, I need to use arrays and reference parameters... I also need to use a bubble sorting method to sort the scores into decending order.
I have the bubble sorting and the arrays taken care of-- I'm just having problems with the reference parameters.
In a number of unncessesary functions, I have this:
Code:
#include <stdio.h>
#include <math.h>
void inputData(int list[], int* n),
bubbleSort(int list[], int n),
statistics(int list[], int n, double* mean, double* stdv),
display(int scores[], char grades[], int n, double mean, double stdv);
char letterGrade(int score, double mean, double stdv);
#define max_class_size 40
int main()
{
int list[max_class_size], classSize, exit, i;
double mean, stdv;
printf("How many scores?> ");
scanf("%d", &classSize);
printf("Enter scores>");
inputData(list, &classSize);
bubbleSort(list, classSize);
statistics(list, classSize, &mean, &stdv);
for(i=0 ; i < classSize ; i++)
{
printf("%d\n", list[i]);
}
printf("Exit program? (y/n)> ");
scanf("%d", &exit);
}
void inputData(int list[], int *n)
{
int i;
for(i=0 ; i < *n ; i++)
scanf("%d", &list[i]);
}
void bubbleSort(int list[], int n)
{
int a, b, i;
for(i=0 ; i < n-1 ; i++)
{
for(a = 0 ; a < n-i-1 ; a++)
{
if(list[a] < list[a+1])
{
b = list[a];
list[a] = list[a+1];
list[a+1] = b;
}
}
}
}
void statistics(int list[], int n, double* mean, double* stdv)
{
int sum, sum_sqr, i;
for(i=0 ; i < n ; i++)
{
sum += list[i];
sum_sqr += list[i] * list[i];
}
*mean = sum / n;
*stdv = sqrt(sum_sqr / n - *mean * *mean);
}
char letterGrade(int score, double mean, double stdv)
{
}
void display(int scores[], char grades[], int n, double mean, double stdv)
{
}
I know, the last two funtions at the end, I haven't filled in. Those are to be eventually filled in.
I'm having no problem with the *n reference parameter in the inputData function. That's working just fine. However, in the statistics function, I'm not getting what I want when I try to print mean and stdv.
When I try to print mean and stdv, this is what I input, before the for loop to print the scores in decending order, but after I call on the statistics function:
Code:
printf("\n\nmean: %.2f\nstdv: %.2f\n\n", mean, stdv);
When i use the & symbol infront of mean and stdv in the printf, I get values of 0 for whatever I input.
However, if I input 3 numbers, say; 98 88 77
I get this output:
mean: 66974062.00
stdv: -1.#J
...And that certainly isn't correct.
What did I do wrong?
Thanks!