#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?

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.