## confusedndazed

Hello all, I need to create a function call to calculate the average...Can anyone help with that?? I'm not sure how to create a function in my code to calculate the average. My code currently terminates after I enter the 50 inputs and does not compute and read the average.

``````#include <iostream>
using namespace std ;

void displayTitle ()
{
cout << "Active Duty Navy Personnel Program" << endl ;
}

int main ()
{

displayTitle () ;

int Age[50] ;    // age of personnel
int I ;          // number of ages entered
int sum ;        // sum of ages
float Average ;  // average of ages entered
sum = 0 ;
Average = 0 ;
for (I = 0 ; I < 50 ; I++)
{
cout << "Enter current age of AD Navy Personnel" << ( I + 1 ) << endl ;
cin >> Age[I] ;

if ( Age[I] >=18 ){
sum = sum + Age[I] ;
}
else {
I = I - 1 ; // the same element the Age array will be read
}}
Average = sum/50 ;
for ( I = 0 ; I < 50 ; I ++ )
{
cout << "Age of Personnel" << ( I + 1 ) << "is : "
<< Age[I] << endl ;
}
cout << "The current average age of AD Navy Personnel is : "
<< Average << endl ;

cin.get () ;
return (0) ; // terminate with success

}``````

## jonsca 1,059

What is the output you are getting currently?

There are a couple of options for a function, one is you can pass in Age[] and I

e.g.,

``````float average(int agearr[],int count)
{
float ave
int sum (over all agearr)
cast sum to a float
ave = sum divide by I
return ave
}``````

and then call it in main() `Average = average(Age,I);` or if you preferred you could pass in sum and I and have it do the division for you.

## mellowmike 9

I compiled your code and ran it, and it showed the average. I'm not sure why you aren't seeing it.

If you want to do a function call, the help jonsca provided should be sufficient, but from what I can tell, your code is working fine now.

## confusedndazed

Thanks alot Jonsca!!...Can you tell me where this code would need to go?

## jonsca 1,059

Two choices this time too, you can put a prototype `float average(int a[],int);` at the top (above void displayTitle)
and then put your function definition (what I posted above) after the closing brace of main:

``````int main()
{

}
float average(,)
{

}``````

or you can skip the prototype and put the whole thing above void displayTitle()

``````float average(int agearr[],int count)
{

}

void displayTitle()
{

}
int main()
{

}``````

## confusedndazed

OMG you are very helpful!!! You solved my thread, your help is greatly appreciated!! It finally executed properly.

## jonsca 1,059

You're very welcome. :)