using arrays with functions

This is a discussion on using arrays with functions within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> float average(int values [], int array_size); int sum = 0; int i = 0; int main (void) ...

  1. #1
    Un Artiste Extraordinaire volk's Avatar
    Join Date
    Dec 2002
    Posts
    357

    using arrays with functions

    Code:
    #include <stdio.h>
    
    float average(int values [], int array_size);
    
    
    int sum = 0;
    int i = 0;
    
    int main (void)
    
    {
    	
    	float f;
    	
    	
    	
    
    	int integers[5] = {0};
    
    	printf("Enter five numbers\n");
    
    	while (scanf("%d", &integers[i])!=EOF)
    	{
    		i++;
    		sum += integers[i];
    		
    		if (i == 5)
    			break;
    	}
    
    
    	f = average(integers, i);
    
    	printf("The average of the numbers you entered is %.2f\n",f);
    
    	return (int) f;
    }
    
    float average(int values [], int array_size)
    
    {
    	return (float) sum/array_size;
    
    }
    Why is the average always coming out to be some crazy number?

  2. #2
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    You've got plenty of problems to play with :-)
    Code:
    #include <stdio.h>
    
    float average(float sum, int array_size);
    
    int main (void)
    {
      int i = 0;
      float f, sum = 0.0f;
      float integers[5] = {0.0f};
      
      printf("Enter five numbers\n");
      
      while (scanf("%f", &integers[i])!=EOF)
      {
        sum += integers[i++];
        
        if (i == 5)
          break;
      }
      
      f = average(sum, i);
      
      printf("The average of the numbers you entered is %.2f\n",f);
      
      return 0;
    }
    
    float average(float sum, int array_size)
    {
      return sum/array_size;
    }
    *Cela*

  3. #3
    Registered User Azuth's Avatar
    Join Date
    Feb 2002
    Posts
    236
    Without bothering to go through your code (since I'm both at work and very lazy), could I suggest that you write a short loop to print the contents of the array, this may give you a clue as to where your problem is. Either that, or wait until some magnanimous code god comes along and spots your issue straight away, and saves you the trouble of doing your own logical debugging.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. functions using arrays
    By trprince in forum C Programming
    Replies: 30
    Last Post: 11-17-2007, 05:10 PM
  2. Passing arrays of pointers into functions
    By ashley in forum C Programming
    Replies: 5
    Last Post: 01-13-2007, 05:48 PM
  3. storage class, arrays, functions and good layout
    By disruptivetech in forum C Programming
    Replies: 4
    Last Post: 12-02-2005, 01:34 PM
  4. functions and arrays
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 03-14-2002, 08:57 AM
  5. elements of arrays; functions
    By sballew in forum C Programming
    Replies: 6
    Last Post: 09-03-2001, 01:48 AM

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