Thread: functions

  1. #1
    Registered User Cess's Avatar
    Join Date
    Sep 2011
    Posts
    55

    functions

    I'm trying to learn using more then one functions and plugin them into the main and print out values. I'm not really understanding it. I need to know where I can read up on stuff or explain the parts in the program my professor did that I don't get. That way once I undersand more I can learn how to have the program also print out the biggest and smallest value for me and the valance my code I did that is new is in small font (so to get it to work take that out) I don't know what I am doing so I did that part wrong.

    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 */
    
    /* starter code below */
    
    #include <stdio.h>
    #include <stdlib.h>
    
    /*a)~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
    double findAve(int numberPts, double vals[])
    {
        double sum = 0;
        int numpts, i;
        for (i = 0; i < numberPts; i++)
            sum = sum + vals[i];
        return sum/numberPts;
    }
    /*b)~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
    double findBiggest(int numberPts, double vals[])
    {
        double biggest = 0;
        int numpts, i;
        for (i = 0; i < numberPts; i++)
            sum = sum + vals[i];
        return biggest/numberPts;
    }
    /*c)~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
    double findSmallest(int numberPts, double vals[])
    {
        double smallest = 0;
        int numpts, i;
        for (i = 0; i < numberPts; i++)
            sum = sum + vals[i];
        return smallest/numberPts;
    }
    /*d)~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
    double CalcVariance(/*int numberPts, */ double vals[])
    {
        double sumsq=0, mean, variance, biggest;/*move these later */
        int numpts, i;
        for (i = 0; i < numpts; i++)    /* move to CalcVariance*/
        {
            double diff = vals[i] - mean;
            sumsq = sumsq + (diff * diff);
        }
        for (i = 0; i < numpts; i++)/* move to CalcVariance*/
        {
            if (biggest < vals[i]);
                printf("biggest number %d :\n", vals[]);
            if (biggest = vals[i]);
                printf("biggest number equals %d:\n", vals[]);
        }
        variance = sumsq/(numpts - 1);
        printf("Variance = %lf", variance);
        return 0.0;
    }
    int main()
    {
        double values[20];
        int numpts, i;
        double mean;
        double biggest = -1000000; /*really negative */
        printf("Enter the number of points: ");
        scanf("%d", &numpts);
        if (numpts <= 1)
        {
            printf("At least 2 points must be entered\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);
        
        
       
        return 0;
    thanks for any help you can give me in advance!!
    ~Cess~
    AKA : total newbie
    ....and totally frustrated
    thanks for any help given.....
    I feel like I"m going to fail this class....blah!

  2. #2
    Registered User
    Join Date
    Aug 2010
    Posts
    231
    Tell your professor, he's code is UB (undefined behaviour) on scanf("%lf",...).

  3. #3
    Registered User Cess's Avatar
    Join Date
    Sep 2011
    Posts
    55
    really? what part of it is UB, what doesn't have a real value? (didn't know what that meant lol looked it up)


    ~Cess~
    AKA : total newbie
    ....and totally frustrated
    thanks for any help given.....
    I feel like I"m going to fail this class....blah!

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by BillyTKid View Post
    Tell your professor, he's code is UB (undefined behaviour) on scanf("%lf",...).
    What are you talking about?
    Quote Originally Posted by man scanf
    l (ell) Indicates that the conversion will be one of dioux or n and the
    next pointer is a pointer to a long int (rather than int), that
    the conversion will be one of a, e, f, or g and the next pointer
    is a pointer to double (rather than float), or that the conver-
    sion will be one of c, s or [ and the next pointer is a pointer
    to an array of wchar_t (rather than char).
    Compared to:
    Quote Originally Posted by man printf
    fF The argument is printed in the style `[-]ddd.ddd' where the
    number of d's after the decimal point is equal to the preci-
    sion specification for the argument. If the precision is
    missing, 6 digits are given; if the precision is explicitly
    0, no digits and no decimal point are printed. The values
    infinity and NaN are printed as `inf' and `nan', respec-
    tively.
    scanf needs you to tell it if it's a double instead of a float. printf uses the same f specifier for both double and float. I'm not sure what you are seeing if you aren't talking about that.


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

  5. #5
    Registered User Cess's Avatar
    Join Date
    Sep 2011
    Posts
    55
    Ok been working on it some more... I can't seem to get the biggest value to print.... Its in different font

    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 */
    /* starter code below */
    
    #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 sum, biggest = 0;
        int i;
        for (i = 0; i < numberPts; i++)
            sum = sum  = vals[i];
        return sum/numberPts;
    }
    /*c)~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
    double findSmallest(int numberPts, double vals[])
    {
        double smallest = 0;
        int i;
        double sum;
        for (i = 0; i < numberPts; i++)
            sum = sum + vals[i];    /* Need code similar to findBiggest */
        return i,smallest,numberPts;
    }
    /*d)~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
    double CalcVariance(int numberPts, double vals[])
    {
        double sumsq=0, mean, variance;         /*move these later */
        int numpts, i;                  /* mean = findAve..... ****/
        for (i = 0; i < numpts; i++)    /* move to CalcVariance*/
        {
            double diff = vals[i] - mean;
            sumsq = sumsq + (diff * diff);
        }
        variance = sumsq/(numpts - 1);
        printf("Variance = %lf", variance);
        return 0.0;
    }
    int main()
    {
        double values[20];
        int numpts, i;
        double mean;
        double biggest = -1000000; /*really negative */
        printf("Enter the number of points: ");
        scanf("%d", &numpts);
        if (numpts <= 1)
        {
            printf("At least 2 points must be entered\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);
    
     
       
    
    
        return 0;
    }
    Only thing that does work is the advage trying to break it down in parts and get each part working till I move to the next, I"m hoping that if I get the bigest to work I can figure out the rest from that... however I have no clue about the valnce.

    this is one example of me runing the program using 2 and 4...

    Code:
    Enter the number of points: 2
    Enter a value: 2
    Enter a value: 4
    Average = 3.000000
    Biggest = 2.000000
    >Exit code: 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!

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Stop playing with fonts inside the code tags... if you let it post by itself you will have line numbers to refer to...


    Code:
    double findBiggest(int numberPts, double vals[])
    {
        double sum, biggest = 0;
        int i;
        for (i = 0; i < numberPts; i++)
            sum = sum  = vals[i];
        return sum/numberPts;
    }
    This is not returning the highest number... in fact it's returning the average of the numbers.

    Freebie... like this...
    Code:
    // highest value in an array
    float Biggest(float array[], int size)
      {  int i;
         float big = array[0];
        
         for (i = 0; i < size; i++)
            if array[i] > big)
              big =  array[i];
    
        return big;  }


    Code:
    /*c)~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
    double findSmallest(int numberPts, double vals[])
    {
        double smallest = 0;
        int i;
        double sum;
        for (i = 0; i < numberPts; i++)
            sum = sum + vals[i];    /* Need code similar to findBiggest */
        return i,smallest,numberPts;
    }
    This is also finding an average... but then it tries to return multiple values which C cannot do.


    Seriously... get your hands on some tutorials and textbooks and give them a real study...
    You aren't even close to accomplishing your goals this way.

  7. #7
    Registered User Cess's Avatar
    Join Date
    Sep 2011
    Posts
    55
    ok thanks but I can't seem to get that to work still gives me zero.....

    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 */
    /* starter code below */
    
    #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 sum, biggest = 0;
        int i;
        for (i = 0; i < numberPts; i++)
            if (vals[i] > biggest);
            biggest = vals[i];
        return sum/numberPts;
    }
    /*c)~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
    double findSmallest(int numberPts, double vals[])
    {
        double smallest = 0;
        int i;
        double sum;
        for (i = 0; i < numberPts; i++)
            sum = sum + vals[i];    /* Need code similar to findBiggest */
        return i,smallest,numberPts;
    }
    /*d)~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
    double CalcVariance(int numberPts, double vals[])
    {
        double sumsq=0, mean, variance;         /*move these later */
        int numpts, i;                  /* mean = findAve..... ****/
        for (i = 0; i < numpts; i++)    /* move to CalcVariance*/
        {
            double diff = vals[i] - mean;
            sumsq = sumsq + (diff * diff);
        }
        variance = sumsq/(numpts - 1);
        printf("Variance = %lf", variance);
        return 0.0;
    }
    int main()
    {
        double values[20];
        int numpts, i;
        double mean;
        double biggest = -1000000; /*really negative */
        printf("Enter the number of points: ");
        scanf("%d", &numpts);
        if (numpts <= 1)
        {
            printf("At least 2 points must be entered\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);
    
     
       
    
    
        return 0;
    }
    ~Cess~
    AKA : total newbie
    ....and totally frustrated
    thanks for any help given.....
    I feel like I"m going to fail this class....blah!

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Line 26 ... double biggest = vals[0];
    Line 26 ... remove sum, it's not used.
    Line 29 ... remove the semicolon after the if() statement.
    Line 31 ... return biggest;
    Last edited by CommonTater; 10-09-2011 at 09:24 PM.

  9. #9
    Registered User Cess's Avatar
    Join Date
    Sep 2011
    Posts
    55
    ok thanks but ya its doing this still


    Code:
    >ch -u "professior given"    
    Enter the number of points: 2
    Enter a value: 2
    Enter a value: 4
    Average = 3.000000
    Biggest = 0.000000
    >Exit code: 0


    Code:
    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;
    }
    Last edited by Cess; 10-09-2011 at 09:28 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. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Line 6 ... remove the semicolon at the end.

  11. #11
    Registered User Cess's Avatar
    Join Date
    Sep 2011
    Posts
    55
    ha ! yes!! thank you!
    ok now I'm going to work on the rest of it.
    thank you again
    ~Cess~
    AKA : total newbie
    ....and totally frustrated
    thanks for any help given.....
    I feel like I"m going to fail this class....blah!

  12. #12
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    It would be nice if you could also check that numpts is not more than 20. Right now, someone can enter 50 and your loop will happily go on about its merry way asking for 50 values to load into an array which can only safely store 20.
    "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

  13. #13
    Registered User Cess's Avatar
    Join Date
    Sep 2011
    Posts
    55
    ok fix that and now it ends if you enter more then 20 values.
    However I am still having trouble with the variance... it always shows 0 that would be great...I'm trying to figure it out using stuff online..but I'm not having much luck and I'm getin frustrated... thanks

    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 */
    /* starter code below */
    
    #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 = vals[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 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;
    }
    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: ");
        scanf("%d", &numpts);
        if (numpts <= 1 <= 20)
        {
            printf("At least 2 to 20 points must be entered be enter the correct amount of values\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);
        printf("variance = %lf\n", variance);
       
        return 0;
    }
    ~Cess~
    AKA : total newbie
    ....and totally frustrated
    thanks for any help given.....
    I feel like I"m going to fail this class....blah!

  14. #14
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    printf("Enter the number of points: ");
    scanf("%d", &numpts);
    if (numpts <= 1 <= 20)
    The highlighted part needs to be something like: numpts <= 1 || numpts > 20

    For the variance part, I see first off that sum needs to be initialized to 0. Secondly, your calculation of mean is only meaningful after you have all the values summed up. You appear to be calculating a new mean each iteration of the loop which is wrong. You probably are going to need a couple loops in this code. This first one should simply add everything up. After this first loop you can divide by numberPts to find the mean. You do this only once. When you have that done you can try and figure out the rest of the calculation for variance which likely will need another loop.
    "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

  15. #15
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Line 58; are you even reading the code you are posting; why did you think the function CalcVariance only returns zero?

    The only return I see in the function is this one.
    Code:
    return 0.0;
    Tim S.

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