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

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    26

    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. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    How does it not work?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    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. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    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 subscribe to a feed

Similar Threads

  1. Assignment 2
    By jpjpjpjp in forum C Programming
    Replies: 4
    Last Post: 02-10-2010, 07:04 AM
  2. Need help with Assignment 2
    By krazyxazn in forum C Programming
    Replies: 1
    Last Post: 02-09-2010, 10:36 PM
  3. Replies: 3
    Last Post: 04-26-2009, 08:54 AM
  4. assignment help
    By mb86 in forum C Programming
    Replies: 8
    Last Post: 04-20-2008, 03:24 PM
  5. Assignment
    By cYou in forum C++ Programming
    Replies: 4
    Last Post: 06-12-2003, 07:57 AM