# Adding an average func to array

Page 1 of 1

## 7 Replies - 1500 Views - Last Post: 25 February 2007 - 11:40 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=24405&amp;s=4b5e772c17d780d5cceebd090c1e5a68&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 2late2fail

Reputation: 0
• Posts: 5
• Joined: 25-February 07

# Adding an average func to array

Posted 25 February 2007 - 10:47 AM

Hello all!
Well I seem to have reached a boiling point -- I programmed my array to take 10 numbers and print them out but i dont know how to have them print in ascending order and then take those same 10 numbers and print the average .. yeah i've been doin this for so long i've confused myself!

```# include  <stdio.h>

int   ask_for_numbers ( float [ ], int );
void  display_float ( float *, int );

void main( void )
{

int elements_count;
float F_array[10];

elements_count = ask_for_numbers ( F_array, 10 );
display_float ( F_array, elements_count );

}

int ask_for_numbers ( float ar[ ], int size )
{
float number = 1.1;
int count = 0;
printf ("\n-->Input: Max of 10 numbers\n");
printf ("\nEnter 10 Numbers or '0' to exit program.\n");
while ( number != 0.0 && count < size )
{
printf ( "%d: ", count + 1 );
scanf ( "%f", &number);
if ( number != 0.0 )
ar[count++] = number;
}
return ( count );
}
void display_float ( float *ar, int size )
{
int index;
for ( index=0; index < size; ++index )
printf ("[%.1f] ", *ar++ );
printf ("\n");
return;
}

```

and yeah it works now -- i just need some, no wait, ALOT of help
--> so go ahead and chop it up if you need to, whatever helps will b good - thanks

Is This A Good Question/Topic? 0

## Replies To: Adding an average func to array

• g+ + -o drink whiskey.cpp

Reputation: 250
• Posts: 13,507
• Joined: 12-July 02

## Re: Adding an average func to array

Posted 25 February 2007 - 10:52 AM

To average all the values in an array, you can simply add them together and divide by the number of elements. Assuming that you know the size of the array, and are passing it to the function:
```float avg(float arr[],int size)
{
float total=0;
for(int i=0;i<size;i++)
{
total += arr[i];
}
}

```

### #3 2late2fail

Reputation: 0
• Posts: 5
• Joined: 25-February 07

## Re: Adding an average func to array

Posted 25 February 2007 - 10:56 AM

Hmm seems easy enough. ty

I'll play around with it, and see what I can do, then post the results.

The max numbers it takes is 10, so its fairly easy - just a simple project im doing with a friend for programming.

The only thing I cannot get is the sorting, I was thinking bubblesort or qsort, but after reading posts - im back at [square]-1, I'm sure ill end up using a quick comparison and printf func. to return my results.

Any other insight would be nice !

### #4 2late2fail

Reputation: 0
• Posts: 5
• Joined: 25-February 07

## Re: Adding an average func to array

Posted 25 February 2007 - 11:02 AM

```float avg(float arr[],int size)
{
float total=0;
for(int i=0;i<size;i++)
{
total += arr[i];
}
}

```

Should i throw this in after sorts or before? I don't think it will matter much, I just have so many { } brackets hahahaha i'm losing my mind.

I just need visuals!!!

Rest i can work on my own

thanks

• g+ + -o drink whiskey.cpp

Reputation: 250
• Posts: 13,507
• Joined: 12-July 02

## Re: Adding an average func to array

Posted 25 February 2007 - 11:05 AM

The sorting can go anywhere, it will not matter to calculating the average.

### #6 2late2fail

Reputation: 0
• Posts: 5
• Joined: 25-February 07

## Re: Adding an average func to array

Posted 25 February 2007 - 11:08 AM

alright ill work on it

thanks

### #7 2late2fail

Reputation: 0
• Posts: 5
• Joined: 25-February 07

## Re: Adding an average func to array

Posted 25 February 2007 - 11:35 AM

hmmmmmmm

```# include  <stdio.h>

int   ask_for_numbers ( float [ ], int );
void  display_float ( float *, int );

void main( void )
{

int elements_count;
float F_array[10];

elements_count = ask_for_numbers ( F_array, 10 );

display_float ( F_array, elements_count );

}
int ask_for_numbers ( float ar[ ], int size )
{
float number = 1.1;
int count = 0;
printf ("\n<<< Array Initialization - Max 10 numbers >>>\n");
printf ("\nEnter floating point numbers or 0 to exit.\n");
while ( number != 0.0 && count < size )
{
printf ( "%d: ", count + 1 );
scanf ( "%f", &number);
if ( number != 0.0 )
ar[count++] = number;
}

return ( count );
}
{
float avg(float arr[],int size);
{
float total=0;
for(int i=0;i<size;i++);
{
total += arr[i];
}
}
}

```

Roughly to display the avg correct? Yet i still have to define arr[] and size i do believe --> but i am getting two errors one on the syntax { after return total and on float avg (missing header?)

any ideas?

• g+ + -o drink whiskey.cpp

Reputation: 250
• Posts: 13,507
• Joined: 12-July 02

## Re: Adding an average func to array

Posted 25 February 2007 - 11:40 AM

The avg function appears to be inside another piece of code, it should be removed so it's on its own, as so:

```int main(void)
{
}
//avg function
float avg(float arr[],int size);
{
float total=0;
for(int i=0;i<size;i++);
{
total += arr[i];
}
}
//other functions as required
var1 func1(param)
{
}

```

When defining your functions after the main function, you need to use a function prototype to let the compiler know what to expect as far as usage goes:
```float avg(int arr[],int size);
int main(void)
{