How to calculate mean using calling function in C++?

  c++

The functions are getInput(N), calculateMean() & displayData. There’s an error in displaying
the mean. I can’t figure out what are the other errors. If i run the code, it will display
Average = inf. I hope someone will help me.details about the questions ………………………………………………………………..

#include <iostream>
using namespace std;

int getInputN(int n);
float calculateMean (int n, float sum);
float displayData(double mean);

int i,n;
float sum = 0.0, num[50];
double mean;

int main()
{
    getInputN(n);
    calculateMean (n, sum);
    displayData(mean);

    return 0;
}

int getInputN(int n)
{
    int i;
    float num[50];

    //User enter the number of value
    cout << "Enter the numbers of data: ";
    cin >> n;
    
    //if user input more than 50 numbers
    while (n > 50 || n <= 0)
    {
        cout << "Invalid! Enter the number in range of (1 to 50)." << endl;
        cout << "Enter the number of data: ";
        cin >> n;
    }

    for(i = 0; i < n; ++i)
    {
        cout << i + 1 << ". Enter number: ";
        cin >> num[i];
        sum += num[i];
    }

    return n;
}

    //function to calculate the mean
    float calculateMean (int n, float sum)
    {
    
        mean = sum/n;
    
        return mean;
    }

    //function to display the mean
    float displayData (double mean)
    {
        cout << "Average = " << mean;
    }

Source: Windows Questions C++

LEAVE A COMMENT