Thread: Passing array right

  1. #1
    Registered User
    Join Date
    Jun 2006
    Posts
    24

    Passing array right

    I'm trying to pass an array...and add up it's total, however i'm always getting 0 as my answer. Someone tell me what i'm doing wrong...thanks...eventually it's going to calculate standard deviation...but i need that dang array to be passed

    Code:
    #include <stdio.h>
    #include <math.h>
    
    int st_dev(float a[]);
    
    int main()
    {
    	float numbers[20];
    	int count;
    
    	printf("Enter 20 Numbers: ");
    
    	for (count = 0; count < 20; count++)
    	{
    		scanf( "%d", (numbers + count));
    	}
    
    	st_dev(numbers);
    
    }
    
    int st_dev(float a[])
    {
    	float total;
    
    	for (int i = 0; i < 20; i++ )
    	{
    		total = total + a[i];
    	}
    	
    	printf("%4.2f\n", total);
    
    	return total;
    
    }

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    You're passing the array just fine. The problem is that you're passing scanf() the %d modifier instead of %f. If it was an int array then %d would be the right choice, but since it's a float array you should be using %f.

    Also:
    Code:
    for (int i = 0; i < 20; i++ )
    That is not allowed in C. Declare your variables at the top of the function instead of inside the for loop statement like that.

    And don't forget to inititalize total when you declare it. If you don't specifically set it to something then it will start off with some random garbage value.

    You might want to consider rewriting total = total + a[i]; as total += a[i];. The way you have it will work fine, but I find the += method easier and clearer.
    Last edited by itsme86; 06-16-2006 at 11:11 AM.
    If you understand what you're doing, you're not learning anything.

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    And initialize total.
    Code:
    float total = 0;
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    itsme mentioned that.
    And don't forget to inititalize total when you declare it. If you don't specifically set it to something then it will start off with some random garbage value.
    But the post was edited, so it probably wasn't there when you posted.

    Since total is a float, you can't really return it as an int. Actually, you don't have to return a value at all, because the function prints the value and you discard it anyway.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Registered User
    Join Date
    Jun 2006
    Posts
    24
    Wicked thanks alot for the help guys...it's always the small stuff that i miss

    Appreciate it

  6. #6
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Quote Originally Posted by itsme86
    Code:
    for (int i = 0; i < 20; i++ )
    That is not allowed in C.
    It's allowed in C99.

  7. #7
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Quote Originally Posted by cwr
    It's allowed in C99.
    Code:
    itsme@itsme:~/C$ cat c99test.c
    #include <stdio.h>
    
    int main(void)
    {
      for(int i = 0;i < 10;++i)
        printf("%d\n", i);
    
      return 0;
    }
    itsme@itsme:~/C$ gcc -Wall c99test.c -o c99test
    c99test.c: In function `main':
    c99test.c:5: error: 'for' loop initial declaration used outside C99 mode
    itsme@itsme:~/C$ gcc -Wall --std=c99 c99test.c -o c99test
    itsme@itsme:~/C$
    So it is...my bad.
    If you understand what you're doing, you're not learning anything.

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    No, C99 bad.

    Personally I hate that addition to the language. But I suppose I'm just being old.


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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  2. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  3. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  4. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 06:58 PM
  5. Type and nontype parameters w/overloading
    By Mr_LJ in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 01:01 AM