I need some help with an assignment..please.

This is a discussion on I need some help with an assignment..please. within the C Programming forums, part of the General Programming Boards category; Hello, everyone. I'm new to this whole programming game, and am taking an intro to programming class right now. I ...

  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
    22,161
    How does it not work?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,752
    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, 09:54 AM
  4. assignment help
    By mb86 in forum C Programming
    Replies: 8
    Last Post: 04-20-2008, 04:24 PM
  5. Assignment
    By cYou in forum C++ Programming
    Replies: 4
    Last Post: 06-12-2003, 08:57 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21