In this code I want to have a function to show average of movies saw by students, how to define and declare and call it in this program:

#include <iostream>
#include <iomanip>

using namespace std;

/****************Function Prototypes******************/

void displayList(int [], int);         //function to display array

void getAverage (int [], int);        
void getMedian (int [] , int);
int getMode (int [], int);


/***************main***********************/

int main()
{
   int *movies, 
        total = 0, 
        average,
        numDays,
        count,
        numStudent;					// desired size of the array 'students'
	int* students = NULL;	// pointer to an int, initially to nothing
		
	cout << "  *** Student Movie Analysis ***\n\n";
	
	cout << "Enter the number of Students: ";
	cin >> numStudent;

	// ** Dynamic allocation of Array size 'n' **
	students = new int[numStudent];	// Allocate 'numStudent' ints and save in ptr 'students'

	for (int i=0; i<numStudent; i++)
    {
		students[i] = 0;		// initialize all elements to zero
	}
  Get the number of movies from the user
    cout<<"Enter the num. of movies  below.\n";
   
     for (count = 0; count < numStudent; count++)
     {
     cout << "Student # " << (count + 1) << " : ";
     cin >> students[count];
     }

   //Calculate the total movies
      for (count = 0; count < numStudent; count++)
          {
          total += students[count];
          }
//Calculate the average movies per month
             
             average = total / numStudent;  
     
//Display the results

cout << "\n\nTotal Movies: " << total << endl;
cout << "Average : "<< average << endl; */

delete [] students;	// When done, free memory pointed to by 'students'
	students = NULL;	// Clear 'students' to prevent invalid memory reference
	
	cout << "End of Program.\n";
   system("PAUSE");
	return 0;
}

here is my average Function but it shows overloading error why i dont know

void getAverage( int list[], int numStudent)
{
      for (count = 0; count < numStudent; count++)
          {
          total += students[count];
          }
     // Calculate the average movies in a month
             
             average = total / numStudent;

how to call it @ main

Count is not defined in your function as a function has its own scope. Redeclare int count in your function body. Also, you pass in an array called list and you are using the one called student (also not in scope). Within the body of your function you should change students[count] to list[count].

When calling the function use getAverage(student[],numStudent); note that the numStudent in main and the numStudent within your getAverage function are two totally different things. You could pass in an int called studentNum or anything else as long as it's an int.

Note also that your function does not return anything so you should either give it a return type (e.g., double) or display the value before your function exits.

will this work:::

void getAverage( int list[], int numStudent)
{
      for (count = 0; count < numStudent; count++)
          {
          total += list[count];
          }
     // Calculate the average movies in a month
             
             average = total / numStudent;  
cout<<"average is :"<<average;
     
}

Just need to declare count and total, count which you can do in your for statement: for(int count =0;count<numStudent;count++) and add int total = 0; (outside of your for loop, at the top of the function). Reread the stuff in your text on function scope to see how these variables are created and why they are isolated from the ones in your main function.

Thank you for correcting me
Now I have this function to find the median but to find median I have to sort the array. I did some coding but it shows error on function. What is wrong here

void getMedian (int list [], int numStudent )
{
/*median  is the value that lies in the middle when the values are
arranged in sorted order. If the set has an even number of values, then the median is taken to be
the average of the two middle values*/

//sort the values

          int temp;
          bool swap;
  
     do
     { swap = false;
            for (int count = 0; count < (numStudent - 1); count++)
            {
               if (list[count] > list[count + 1])
                  {
                    temp = list[count];
                   list[count] = list[count + 1];
                    list[count + 1] = temp;
                    swap = true;
                    }
            }
            
     } while (swap);
     
  cout <<"sorted List is "<< list[count];
     
}

I think you want the opposite of your current flag condition. Set the swap flag condition to true. If you're swapping the loop should continue until there are no swaps. So, inside your if clause of the for loop, have swap set to false.

Also, unless you have an overloaded operator << you can't just send an array to the output stream like you have it in the end of the function.

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.