Thread: functions

  1. #16
    Registered User Cess's Avatar
    Join Date
    Sep 2011
    Posts
    55
    I tried different things.. so I returned that to what the professor put originally .... I know that won't work...
    ~Cess~
    AKA : total newbie
    ....and totally frustrated
    thanks for any help given.....
    I feel like I"m going to fail this class....blah!

  2. #17
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Actually if you are looking for variance ... the difference between the smallest and largest numbers you can simply do this...
    Code:
    double CalcVariance(int numberPts, double vals[]) 
      {    return FindBiggest(numberPts,vals) - FindSmallest(numberPts,vals); }
    OR... since you already have biggest and smallest stored away at that point you can to this...

    Code:
    variance = biggest - smallest;
    ... you don't even need the function.

    Surely you must have learned (retained?) something from writing those other functions???
    Last edited by CommonTater; 10-12-2011 at 10:21 AM.

  3. #18
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    I don't think that is what the user is talking about when they say variance. The calculation is a bit more involved than subtracting the smallest from the biggest.

    From Wikipedia entry on variance:
    The variance of a random variable or distribution is the expectation, or mean, of the squared deviation of that variable from its expected value or mean. Thus the variance is a measure of the amount of variation of the values of that variable, taking account of all possible values and their probabilities or weightings (not just the extremes which give the range).

    For example, a perfect die, when thrown, has expected value of (1 + 2 + 3 + 4 + 5 + 6) / 6 = 3.5. Its expected absolute deviation — the mean of the equally likely absolute deviations from the mean (3.5 − 1, 3.5 − 2, 3.5 − 3, 4 − 3.5, 5 − 3.5, 6 − 3.5) — is (2.5 + 1.5 + 0.5 + 0.5 + 1.5 + 2.5) / 6 = 1.5. But its expected squared deviation —its variance (the mean of the equally likely squared deviations) — is (2.52 + 1.52 + 0.52 + 0.52 + 1.52 + 2.52) / 6 = 17.5/6 ≈ 2.9.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #19
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Ah ok... not a mathematician but I thought I'd show her the tricks just in case...

    Like one particularly bad movie says: "As it turns out this is hard"....

  5. #20
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Code:
    double CalcVariance(int numberPts, double vals[])
    {
        double sumsq = vals[0], sum, mean, variance, findAve;         /*move these later */
        int numpts, i;                  /* mean = findAve..... ****/
        for (i = 0; i < numpts; i++)    /* move to CalcVariance*/
        {
            sum = sum + vals[i];
            findAve = sum/numberPts;
            mean = findAve;
            variance = sumsq/(numpts - 1);
            double diff = vals[i] - mean;
            sumsq = sumsq + (diff * diff);
        }
        return 0.0;
    }
    You have way too much going on in there, and it revolves around incorrect calculation of mean. On every iteration except the last one, mean will be incorrect (you've only summed part of the list, but divide by the total number). Pass mean into the funciton. You can also eliminate several local variables. You only need i, sumsq and diff, and you should initialize sumsq to 0, not vals[0]:
    Code:
    double CalcVariance(int numberPts, double vals[], double mean)
    {
        double diff, sumsq = 0;
        int i;
    
        for (i = 0; i < numberPts; i++) {
            // calculate the difference between mean and the current item in vals
            // add the square of the difference to sumsq
        }
        // divide the sum squared difference by the number of data points and return it
    }
    ...
    // in main
    variance = CalcVariance(numpts, values, mean);  // we already calculated mean for our data, just pass it in

  6. #21
    Registered User Cess's Avatar
    Join Date
    Sep 2011
    Posts
    55
    I know this isn't right... but I'm a little confused by what you said so if you can guide me on my errors it would be great. Its always says zero no matter what value I give it... This is what I have so far...
    /*d)~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

    Code:
    double CalcVariance(int numberPts, double vals[], double mean)
    {
        double diff, sumsq = vals [0];
        int i;
        for (i = 0; i < numberPts; i++) 
            {
            vals [i] = mean - vals [i];
            sumsq = diff*diff + sumsq;
            // calculate the difference between mean and the current item in vals
            // add the square of the difference to sumsq
            }
       return sumsq/i;
        // divide the sum squared difference by the number of data points and return it
    }
    in case you need it all the code I have so far...
    Code:
    /* Melissa Schwager Homework 4
    Write a program that will accept the number of points 
    and then read in the points that are to be stored in an array.
    The program will then call functions that
    a) find the average of the values in the array 
    b) find the largest value in the array 
    c) find the smallest value in the array 
    d) find the variance of the values */
    
    #include <stdio.h>
    #include <stdlib.h>
    
    /*a)~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
    double findAve(int numberPts, double vals[])
    {
        double sum = 0;
        int i;
        for (i = 0; i < numberPts; i++)
            sum = sum + vals[i];
        return sum/numberPts;
    }
    /*b)~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
    double findBiggest(int numberPts, double vals[])
    {
        double biggest = vals[0];
        int i;
        for (i = 0; i < numberPts; i++)
            if (vals[i] > biggest)
            biggest = vals[i];
        return biggest;
    }
    /*c)~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
    double findSmallest(int numberPts, double vals[])
    {
        double smallest = 0;
        int i;
        double sum;
        for (i = 0; i > numberPts; i++)
            if (vals[i] < smallest)
            smallest = vals[i]; /* Need code similar to findBiggest */
        return smallest;
    }
    /*d)~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
    
    double CalcVariance(int numberPts, double vals[], double mean)
    {
        double diff, sumsq = vals [0];
        int i;
        for (i = 0; i < numberPts; i++) 
            {
            vals [i] = mean - vals [i];
            sumsq = diff*diff + sumsq;
            // calculate the difference between mean and the current item in vals
            // add the square of the difference to sumsq
            }
       return sumsq/i;
        // divide the sum squared difference by the number of data points and return it
    }
    
    int main()
    {
        double values[20];
        int numpts, i;
        double mean;
        double biggest = -1000000; /*really negative */
        double smallest = -100000;
        double variance =  -100000;
        printf("Enter the number of points from 2 to 20: ");
        scanf("%d", &numpts);
        if (numpts <= 1 || numpts > 20)
        {
            printf("Only 2 to 20 points can be entered, please correct amount of values and retry\n");
            return 0;
        }
        for (i = 0; i < numpts; i++)
        {
            printf("Enter a value: ");
            scanf("%lf", &values[i]);
        }
        mean = findAve(numpts, values);
        printf("Average = %lf\n", mean);
    
        biggest = findBiggest(numpts, values);
        printf("Biggest = %lf\n", biggest);
    
        smallest = findSmallest(numpts, values);
        printf("Smallest = %lf\n", smallest);
     
        variance = CalcVariance(numpts, values, mean);
        printf("variance = %lf\n", variance);
       
        return 0;
    }
    thanks for any help!!!
    ~Cess~
    AKA : total newbie
    ....and totally frustrated
    thanks for any help given.....
    I feel like I"m going to fail this class....blah!

  7. #22
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
     double diff, sumsq = vals [0];
        int i;
        for (i = 0; i < numberPts; i++)
            {
            vals [i] = mean - vals [i];
            sumsq = diff*diff + sumsq;
    What does diff have for a value?


    Quzah.
    Hope is the first step on the road to disappointment.

  8. #23
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Code:
    double CalcVariance(int numberPts, double vals[], double mean)
    {
        double diff, sumsq = vals [0];
        int i;
        for (i = 0; i < numberPts; i++) 
            {
            vals [i] = mean - vals [i];
            sumsq = diff*diff + sumsq;
            // calculate the difference between mean and the current item in vals
            // add the square of the difference to sumsq
            }
       return sumsq/i;
        // divide the sum squared difference by the number of data points and return it
    }
    To follow up with Quzah: Why are you overwriting the data in vals?

    Also, I told you to initialize sumsq to 0, not vals[0].

  9. #24
    Registered User Cess's Avatar
    Join Date
    Sep 2011
    Posts
    55
    I really should post the 1st code, and not try and mess with it cuz I only seem to make it worst.
    this is what I had after he informed me of what to do, before I started to mess with it and screw it up. Sorry about that.

    Code:
    double CalcVariance(int numberPts, double vals[], double mean)
    {
        double diff, sumsq = 0;
    
        int i;
        for (i = 0; i < numberPts; i++) 
            {
    
            diff = mean - i;
    
            sumsq = diff*diff + sumsq;
            // calculate the difference between mean and the current item in vals
            // add the square of the difference to sumsq
            }
       return sumsq/i;
    
        // divide the sum squared difference by the number of data points and return it
    }
    
    
    
    Last edited by Cess; 10-12-2011 at 04:55 PM.
    ~Cess~
    AKA : total newbie
    ....and totally frustrated
    thanks for any help given.....
    I feel like I"m going to fail this class....blah!

  10. #25
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Read up on what variance is. It's very hard to code a solution when you don't even understand what the problem is asking. Here is a good, simple explanation and example.

    You're really close. You just need to figure out what's wrong with this line:
    Code:
    diff = mean - i;
    True, i will have the right value when you use it in your return statement, but it would probably be considered "better form" to use numberPts instead. numberPts always represents the number of data points you have (and the name makes more sense when you read the formula), where as i is just a counter variable:
    Code:
    return sumsq / numberPts;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating Functions & passing information to other functions
    By RyanLeonard in forum C Programming
    Replies: 4
    Last Post: 10-28-2010, 12:17 PM
  2. Replies: 7
    Last Post: 04-19-2006, 11:17 AM
  3. Calling functions within functions
    By saahmed in forum C Programming
    Replies: 1
    Last Post: 03-09-2006, 02:03 AM
  4. Replies: 6
    Last Post: 05-06-2003, 03:08 PM
  5. Replies: 1
    Last Post: 01-20-2002, 11:50 AM