Need help with finding the average and standard deviation of an array

This is a discussion on Need help with finding the average and standard deviation of an array within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> #include <math.h> #include <string.h> #include <stdlib.h> int average(int population[], int count) { int i = 0; int ...

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    12

    Need help with finding the average and standard deviation of an array

    Code:
    #include <stdio.h>
    #include <math.h>
    #include <string.h>
    #include <stdlib.h>
    
    
    
    int average(int population[], int count) {
    
        int i = 0;
        int sumofarray = 0;
    
        for (i=0; i<100; i++);{
    
            sumofarray += population[i];
        }
    
      return sumofarray / count ;
    }
    
    
    int stddev(int population[], int count, int averageofarray) {
    
    
    
        int difference = 0;
        int differencesquared = 0;
        int sumofdifferencesquared = 0;
        int averageofdifferencesquared = 0;
        int i = 0;
        for (i=0; i<100; i++);
    {
        difference = population[i] - averageofarray;
    
        differencesquared = difference * difference;
    
        sumofdifferencesquared += differencesquared;
    
        averageofdifferencesquared = sumofdifferencesquared / count;
    
        }
    return sqrt(averageofdifferencesquared);
    }
    
    
    
    main()
    {
    
    int count = 0;
    int i = 0;
    
    
    int averageofarray = 0;
    int standarddeviation = 0;
    
    
    int population[100];
    
    for (i=0; i<100; i++)
    {
    population[i] = rand()%100;
    
    count++;
    }
    
    
    averageofarray = average(population, count);
    
    
    standarddeviation = stddev(population, count, averageofarray);
    
    
    
    printf("The array you entered consists of these numbers:\n");
    for(i=0;i<100;printf("%d ",population[i]), i++);
    
    
    printf("\nThe average of the population is %d\n", averageofarray);
    printf("The standard deviation of the population is %d", standarddeviation);
    
    return 0;
    }
    I have to use a function for average and a function of standard deviation, which each call the randomly generated array and spit out their values to the prints. I'm not supposed to use pointers and am just getting stuck somewhere and this is due tomorrow morning. Any help would be greatly appreciated since I'm really new to C and programming in general.

    Not getting an accurate average now.
    Last edited by 001bob; 10-04-2011 at 09:57 PM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Just pass the name of the array. Not an array + index.


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

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    12
    Awesome now the program is running almost perfectly. I'm getting an average of 1 though, which can't be correct anything else you see involving the average resulting in 1 and not 50ish would be amazing. Thank you so much for your quick response you are saving me from a sleepless night.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    averageofarray = (int) average(population[100], count);
    Your function returns an int, and you are assigning it to an int, so you don't need a cast. (You almost never need to cast in C, if you do, you're probably doing something wrong.)
    Code:
    int average(int population[], int count) {
     
        int i = 0;
        int sumofarray = 0;
     
        for (i=0; i<100; i++);{
     
            sumofarray += population[i];
        }
     
      return sumofarray / count ;
    }
    I'm not really sure why you have count, since everything you do is based off of 100.


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

  5. #5
    Registered User
    Join Date
    Oct 2011
    Posts
    12
    Yea I'm not sure what I was messing with there, but I edited that out moments before you re-posted the average is all that is troubling me now. Oh and the count was just a specified part of the program I know its not necessary since there isn't any user input to the array. I was getting an accurate average when I put the sumofarray into main so I'm not sure why it isn't working in the average function.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by 001bob View Post
    I'm not supposed to use pointers
    Technically this:
    Code:
    void foo( int bar[] );
    Is a pointer. Any time you pass an array to a function it is actually passed as a pointer to the type of the first element. So that is actually:
    Code:
    void foo( int *bar );
    If you're bored: Arrays and Pointers


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

  7. #7
    Registered User
    Join Date
    Oct 2011
    Posts
    12
    Oh yea well calling by reference is okay for the arrays, which I understand I just meant for the rest of it besides the population array elements in case someone was suggesting making a pointer out of something else for convenience sake. No asterisks *ptr basically. I tinkered around with the count and sums to try and figure it out, but I'm still stumped with this average thing. Thanks for the literature on arrays and pointer its a good overview.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help with standard deviation
    By belkins in forum C Programming
    Replies: 3
    Last Post: 10-28-2008, 11:04 PM
  2. problems finding the average of an array column
    By mrgeoff in forum C Programming
    Replies: 4
    Last Post: 04-18-2005, 11:49 PM
  3. standard deviation in need help!!!
    By voltare in forum C Programming
    Replies: 2
    Last Post: 03-01-2004, 05:46 AM
  4. Standard Deviation of an array of numbers...
    By Xenofizz in forum C++ Programming
    Replies: 4
    Last Post: 11-19-2003, 09:48 PM
  5. Standard Deviation in C++
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 09-14-2001, 11:09 AM

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