Hello, everyone. I'm new to this whole programming game, and am taking an intro to programming class right now. I am supposed to be making a program that creates 100 random numbers, stores them into an array, calls the array in two different functions to calculate the standard deviation and average of the random values. I also need to print these values that were found and the values in the array, just because the teacher wants to see what we have. I haven't gotten to the last part, because I figured it wouldn't be terribly difficult. So, here's what I have so far:
Code:
#include <stdio.h>
#include <math.h>
void average(int [], int, float total_average);
void stddev(int [], int, float standard_deviation);
int main()
{
float main_average = 0;
float main_standard_deviation = 0;
int population[100];
int i;
for (i=0; i<100; i++)
{
population[i] = rand()%100;
printf("%d", population[i]);
average(population, 100, main_average);
};
printf("%f", main_average);
stddev(population, 100, main_standard_deviation);
printf("%f", main_standard_deviation);
};
void average(int population[], int i, float total_average)
{
float sum = 0;
float total = 100;
for(i=0; i<100; i++)
{
sum = sum + population[i];
}
average = sum / total;
return total_average;
};
void stddev(int population[], int i, float standard_deviation)
{
float difference = 0;
float sum_of_differences = 0;
float square = 0;
for(i=0; i<100; i++)
{
difference = average - population[i];
square = difference * difference;
sum_of_differences = sum_of_differences + square;
standard_deviation = sqrt(sum_of_differences / 100);
}
return standard_deviation;
};
I know that it's wrong, but could someone explain to me why it's wrong and how I could possibly go about changing it?
This class went from Hello World as the first assignment, and then to this as the third... >.>
Thanks for any help!