Functions with array arguments

This is a discussion on Functions with array arguments within the C Programming forums, part of the General Programming Boards category; I am trying to make some functions that take an array of n numbers and do certain things to them ...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    2

    Functions with array arguments

    I am trying to make some functions that take an array of n numbers and do certain things to them (like getting the sum, calculate statistical deviation, etc...)

    Right now, I have:
    Code:
    int n;
    int main(void)
    {
    	printf("How many numbers?:");
    	scanf("%f", n);
    	float numbers[n];
    	...
    	...
    	...
    	sum=sum(numbers[n]);
    	printf("Sum is:%f", sum);
    }
    
    float sum(float numbers[n])
    {
    	int x;
    	float sum;
    	for(x=0; x==n; ++x)
    	{
    		sum=numbers[x]+sum;
    	}
    	return sum;
    }
    But that isn't working. What's the problem?

  2. #2
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    Make that your function accepts a float pointer. That'll make it work.
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    2
    What do you mean?

  4. #4
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    Code:
    float sum(float* numbers)
    {
         int x;
         float sum;
         for(x = 0; x == n; ++x)
         {
    	sum = numbers[x] + sum;
         }
         return sum;
    }
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Variable length arrays are were only introducted in the C99 standard, as with mixed declarations.

    Naturally you'll have to include the length of the array as an argument to the function, since otherwise you'll have no way of knowing when it ends.

    [edit]
    Ah, I see you are using globals. Disregard the above then, and continue on with your bad programming habits.
    [/edit]


    Quzah.
    Last edited by quzah; 11-19-2006 at 07:26 PM.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Dec 2005
    Location
    Australia - Melbourne
    Posts
    63
    you need to initialize sum to zero aswell

  7. #7
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    better this way:
    Code:
    float sum(float* numbers, int count)
    {
         int x;
         float sum = 0.0;
         for(x = 0; x < count; ++x)
         {
    	sum += numbers[x];
         }
         return sum;
    }
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 12
    Last Post: 04-12-2009, 05:49 PM
  2. Replies: 7
    Last Post: 11-21-2008, 03:27 PM
  3. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  4. Homework help Array Arguments
    By VanillaSparkles in forum C Programming
    Replies: 3
    Last Post: 08-24-2007, 01:27 AM
  5. question about multidimensional arrays
    By richdb in forum C Programming
    Replies: 22
    Last Post: 02-26-2006, 08:51 AM

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