#include <iostream>
#include <conio.h>  //needed to allow getch()  
#include <iomanip> 

using namespace std;

int mean(const int [ ], int );//function prototype

int main() // initializing the main program
{
    char ch1;
    cout<<"Please enter the letter M for the mean function or the letter F for the Frequency function"<<endl;
	cin>>ch1;
    if(ch1 = 'M'||'m')
           {
             cout<<"The average of the array is:"<<avg<<endl;
           getch();
           return 0;
            
           }
           


	
    
}

float mean()
{
  int sum=0;
  int avg=0;
  int data[40] = {2, 3, 4, 5, 4, 6, 8 ,9, 3, 2,
	         7, 8, 7, 8, 9, 4, 5, 3, 6, 1,
	          3, 4, 5, 2, 7, 8, 9, 5, 8, 9,	
	         4, 5, 6, 8, 9, 8, 7, 8, 9, 4};
	         for(int i = 0; i < 40; ++i)
	         sum+=data[i];
	         avg=sum/40;
}

How do i display the average found in float mean() in the main() function?

Dani AI

Generated

Common problems in the thread: the prototype and definition for mean do not match, the mean function never returns a value, the if uses assignment instead of comparison, and integer division will truncate the average. and were right to point out the missing return, but the function should also accept the array and its size (per the assignment) and compute a floating-point result. conio.h/getch() is platform-specific; std::cin.get() or reading a line is more portable.

Required fixes, concisely:

  • Make the prototype and definition identical: float mean(const int[], int).
  • Sum the elements, then return static_cast<float>(sum) / size to avoid integer truncation.
  • In main pass the array and its length: float avg = mean(data, N); then print avg.
  • Use if (ch == 'M' || ch == 'm') (comparison), not =.
  • Handle edge cases: check size > 0 before dividing, use a wider integer type for sum if numbers can be large, and validate input characters (handle Q/q to quit).

A minimal correct example (different sample data from the posts) showing the key pieces:

#include <iostream>
#include <iomanip>

float mean(const int arr[], int n) {
    long sum = 0;
    for (int i = 0; i < n; ++i) sum += arr[i];
    return n ? static_cast<float>(sum) / n : 0.0f;
}

int main() {
    const int N = 5;
    int data[N] = {2, 4, 6, 8, 10};
    char ch = 'M';
    if (ch == 'M' || ch == 'm') {
        float avg = mean(data, N);
        std::cout << std::fixed << std::setprecision(2) << "Average: " << avg << '\n';
    }
}

Troubleshooting tips: enable compiler warnings (e.g., -Wall), check for prototype mismatches and missing return statements, and inspect any integer divisions. The frequency/histogram function should similarly accept the array and size and iterate to build counts.

Recommended Answers

All 3 Replies

This is my assignment
Write a C++ program to prompt the user with the following selections:

M[Mean] F[Frequency] Q[Quit]

• If the user selects "M or m" then your program should pass the following one-dimensional array from main ( ) to a function float mean (const int [ ], int ) to find the mean of the array and print the results in function main ( ).

Data [ 40] = {2, 3, 4, 5, 4, 6, 8 ,9, 3, 2,
7, 8, 7, 8, 9, 4, 5, 3, 6, 1,
3, 4, 5, 2, 7, 8, 9, 5, 8, 9,
4, 5, 6, 8, 9, 8, 7, 8, 9, 4,2}

• If the user selects “F or f” then call a user-defined function frequency ( ) that counts the number of responses (frequency) of each number, then pints the frequency along with its histogram as follows:

Response Frequency Histogram
1 1 *
2 3 ***
3 4 ****


• Finally, your program should quit by pressing "Q or q".

Report:

1. On due date, please submit HW4.CPP through Webct.

2. Your program MUST BE virus FREE (Zero will be given if your disk is infected).

3. Your program must be fully documented.

4. Your program must run well and catch all illegal inputs such as entering other characters than the ones specified.

#include <iostream>
#include <conio.h>  //needed to allow getch()  
#include <iomanip> 

using namespace std;

int mean(const int [ ], int );//function prototype

int main() // initializing the main program
{
    char ch1;
    cout<<"Please enter the letter M for the mean function or the letter F for the Frequency function"<<endl;
	cin>>ch1;
    if(ch1 = 'M'||'m')
           {
             
           getch();
           return 0;
            }
     else if(ch1 = 'F'||'f')
          {
            const int data = 40;
            int n[data] = {2, 3, 4, 5, 4, 6, 8 ,9, 3, 2,
	         7, 8, 7, 8, 9, 4, 5, 3, 6, 1,
	          3, 4, 5, 2, 7, 8, 9, 5, 8, 9,	
	         4, 5, 6, 8, 9, 8, 7, 8, 9, 4};
             cout<<"Element"<<setw(13)<<"Value";
              cout<<setw(17)<<"Histogram"<<endl;
                for ( int i = 0; i < data; i++ ) {
                 cout << setw( 7 ) << i << setw( 13 ) << n[ i ] << setw( 9 );
                  for ( int j = 0; j < n[ i ]; j++ )   // print one bar
                   cout << '*';
                   cout << endl; 
                    getch();
           return 0; 
             }


	
}
}

float mean()
{
  int sum=0;
  int avg=0;
  int data[40] = {2, 3, 4, 5, 4, 6, 8 ,9, 3, 2,
	         7, 8, 7, 8, 9, 4, 5, 3, 6, 1,
	          3, 4, 5, 2, 7, 8, 9, 5, 8, 9,	
	         4, 5, 6, 8, 9, 8, 7, 8, 9, 4};
	         for(int i = 0; i < 40; ++i)
	         sum+=data[i];
	         avg=sum/40;
	           cout<<"The average of the array is:"<<avg<<endl;
}

and that is the code i have, and it's just not working so far any suggestions?

first you need to have a return statement at the end of your mean function to return avg. Also your function prototype and your function definition don't match. Your prototype has a array and a int but you definition has no variables.

float mean()
{
    //...
    return avg;
}

Then in your cout statement you can do:

cout<< "The average of the array is:" << mean() <<endl;

I dont think I got it right but simply return the avg from the float mean() and display the result.

cout << mean(); //Like this ? :=)

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.