# Thread: I need some help with an assignment..please.

1. ## I need some help with an assignment..please.

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!

2. How does it not work?

3. You have a void function returning your answer!

Code:
```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;
};```
you need to have that returned answer "caught" by a floating point variable, in the calling function. Remember, the variable you send as a parameter, is just a COPY of the one in the calling function - no lasting changes will be made (they are lost when the called function, ends).

So either "catch" the return value, OR send the function a pointer to that parameter, and then the variable can be changed, with a lasting effect.

Edit: Where did you declare the "average" variable, in the above function?

4. You call average() inside your for loop, when it should be called only once, outside of the loop, after all the array items have values because of how you wrote the average calculation. Your errors come from using uninitialized values in your averages.

Also, calculating the average about 100 times will probably introduce floating point errors nobody wants to care about.

Popular pages Recent additions