Thread: Standard Deviation, etc.

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    4

    Standard Deviation, etc.

    The following functions should return the sum, average, and standard deviation of an array of integers. What's wrong with this code? (unfortunately I cannot test it myself right now). This is my first program in C, very exciting, hope you can help me. Thanks!

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    int main(int argc, char **argv)
    {
    
    
    int sum(int *numbers, unsigned short count){
        int i;
        int sum = 0;
        for(i=0, i < count, i ++)
        {   sum = sum + numbers[i];
        }
        return sum;
    }
    
    double average(int *numbers, unsigned short count){
       float avg;
        int sum;
         sum = sum(numbers, count);
         avg = sum/count;
         return avg;
    }
    
    
    double sd(int *numbers, unsigned short count){
            float avg;
    	int i;
    	float var;
            int sum	= 0;
    
    	avg = average(numbers, count);
    	for(i = 0, i < count, i ++)
    	{	sum = sum + pow((numbers[i] - avg), 2);
    	}
    	var = sum/count;
    	return sqrt(var);
    }
    
    
    
      int numbers[] = {15, 0, -3, 7, 27743, 6, 8, 1, -5732, 1, 9};
    
      printf("The sum is %d\n", sum(numbers, 11));
      printf("The average is %7.2f\n", average(numbers, 11));
      printf("The standard deviation is %7.2f\n", sd(numbers, 11));
    
      return 0;
    }
    }
    Last edited by wovenhead; 02-15-2011 at 08:10 PM.

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Please read the forum guidelines and << !! Posting Code? Read this First !! >>.

    Then, come back and edit your post to use code tags and proper formatting. You'll find us much more responsive that way. Thanks.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    If you haven't run the program, how do you know there IS something wrong with it?

    You test your own software -- and we'll test ours.

    Come back if you have a problem with your program that you can't fix, OK?

    No sense having a dozen people all testing a program that you can test, just as easily.

  4. #4
    Registered User
    Join Date
    Feb 2011
    Posts
    4
    Quote Originally Posted by Adak View Post
    If you haven't run the program, how do you know there IS something wrong with it?

    You test your own software -- and we'll test ours.

    Come back if you have a problem with your program that you can't fix, OK?

    No sense having a dozen people all testing a program that you can test, just as easily.
    I would test it myself if I could! Unfortunately I can't, as I am without my own computer! I get what you're saying, but I assumed I made some visible mistakes, as this is my first program.

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Yes, but many of those would be fixed by listening to compiler warnings. If you can't compile right now, not much point in it being fixed right now. When you get to your computer, fix all the compiler errors and warnings, then come back if you have actual problems with the logic/flow or don't understand the compiler messages. I will say, you need to stop being so messy with types. Use doubles only where you need decimal point data (they're different than floats) and ints where you only need whole numbers of something.

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by wovenhead View Post
    I would test it myself if I could! Unfortunately I can't, as I am without my own computer! I get what you're saying, but I assumed I made some visible mistakes, as this is my first program.
    Beg borrow or steal some computer time....
    You won't learn programming by writing phantom code then begging for help on the web.

    Where is your main() function?
    Where are your declarations?

  7. #7
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Check out codepad.org for free, online compiling. Not as good as your system, but it'll have to do for now.

  8. #8
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Hint: 3/4 equals 0

    Tim S.

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by anduril462 View Post
    Check out codepad.org for free, online compiling. Not as good as your system, but it'll have to do for now.
    I just know I'm stepping in it... but...
    How is he here if he doesn't have a computer?
    Why can't he compile it on the computer he's using?

  10. #10
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    463
    Quote Originally Posted by CommonTater View Post
    I just know I'm stepping in it... but...
    How is he here if he doesn't have a computer?
    Why can't he compile it on the computer he's using?
    he could use an iPod touch like me to browse this forum. But writing code on it is a bit painful.
    "All that we see or seem
    Is but a dream within a dream." - Poe

  11. #11
    Registered User
    Join Date
    Feb 2011
    Posts
    4
    Quote Originally Posted by CommonTater View Post
    I just know I'm stepping in it... but...
    How is he here if he doesn't have a computer?
    Why can't he compile it on the computer he's using?
    Library!

    Quote Originally Posted by anduril462 View Post
    Check out codepad.org for free, online compiling. Not as good as your system, but it'll have to do for now.
    Awesome.

    Results:

    http://codepad.org/awH5dba7

    Line 21: error: called object 'sum' is not a function
    Why isn't sum a function?

    12: for(i=0, i < count, i ++)

    Line 12: error: expected ';' before ')' token
    Line 12: error: expected expression before ')' token

    what's wrong with for(i=0, i < count, i ++)
    Last edited by wovenhead; 02-15-2011 at 08:03 PM.

  12. #12
    Registered User
    Join Date
    Feb 2011
    Posts
    4
    Quote Originally Posted by stahta01 View Post
    Hint: 3/4 equals 0
    So do I need to make one of my ints into a float? any guidance here?

  13. #13
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Don't put your functions inside main.

    Quote Originally Posted by wovenhead View Post
    Line 21: error: called object 'sum' is not a function
    Why isn't sum a function?
    It is, but your local variable overrides the global symbol, so inside that function, sum is just a plain int. Rename it to total or some such.

    12: for(i=0, i < count, i ++)

    Line 12: error: expected ';' before ')' token
    Line 12: error: expected expression before ')' token

    what's wrong with for(i=0, i < count, i ++)
    You need a semicolons not commas.

    Quote Originally Posted by wovenhead View Post
    So do I need to make one of my ints into a float? any guidance here?
    Yes, since you're dividing it by count, which is another int, you end up with integer division, which is very inaccurate. Basically it divides and gives you the whole number part, discarding the remainder. You could change it to a float/double or just cast one of the two operands to a float or double.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C/C++ standard
    By confuted in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 07-06-2003, 03:22 AM
  2. Standard Deviation in C++
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 09-14-2001, 11:09 AM
  3. loops and I/O file.
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 09-09-2001, 09:41 AM
  4. standard language, standard compiler?
    By doubleanti in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 09-03-2001, 04:21 AM