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
    
}

Dani AI

Generated

— good troubleshooting so far. was right to suggest isolating the averaging work into its own routine, and confirmed the program can run, so the remaining issues are usually environmental or subtle bugs rather than a missing function definition.

A few practical points that will fix or harden this kind of input/average program:

  • Avoid manipulating the loop index inside the loop to "retry" an entry; that pattern is brittle. Instead loop until you collect the required number of valid entries (use a separate counter or push valid values into a std::vector). That makes the logic clearer and avoids off-by-one surprises.
  • Watch integer division: dividing two integers drops the fraction. Make the accumulation or the divisor a floating type (double) so the returned average is accurate.
  • Guard against bad input: if the user types non-numeric text, std::cin goes into fail state. Check the read (e.g., if (!(std::cin >> value))) and clear/ignore bad input before retrying.
  • If the program seems to print the average but the console closes immediately (common when double-clicking a compiled EXE), run the program from a terminal or explicitly flush the input buffer before waiting for Enter.

A compact, robust averaging function uses a container and a standard algorithm to avoid manual summing. For example:

double average(const std::vector<int>& ages) {
    if (ages.empty()) return 0.0;
    double total = std::accumulate(ages.begin(), ages.end(), 0.0);
    return total / ages.size();
}

Collecting inputs in a loop that pushes only valid ages into the vector keeps the rest of the code simple and makes the denominator explicit (the number of valid entries). For input-pausing and buffer handling consult std::basic_istream::ignore and for portable accumulation see std::accumulate:

Recommended Answers

All 6 Replies

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.

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.

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

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()
{


}

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

You're very welcome. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.