Hi. I am creating a program for class that can be used to gather statisctical data about the number of movies college students watch in a certain time frame. I have to find the average, median and mode and show this in the output. I am able to get my program to compile and run, but the output is not correct. Can anyone help with this please?

Here is the code:

#include <iostream>
#include <iomanip>
using namespace std;


int main()
{
int *students; //dynamic allocator
int stdSurveyed; //amount of students surveyed, user input
int total = 0;
int *frequency;

double average = 0, median = 0, mode = 0;

// USER INPUT
cout << "How many students are being surveyed? ";
cin >> stdSurveyed;


students = new int[stdSurveyed]; //allocated memory.


//EACH STUDENT INPUT
cout << "How many movies did each student see? Enter below: " << endl;
for (int count = 0; count < stdSurveyed; count++)
{
cout << "Student " << (count + 1) << ": ";
cin >> students[count];
}


//AVERAGE
for (int count = 0; count < stdSurveyed; count++)
{
total += students[count];
}
average = total / stdSurveyed;
cout << "The average is: " << average << endl;


//MEDIAN
if (total % 2 == 0)
{
median = ((students[total/2-1])+(students[total/2])/2.0);
}
else if (total % 2 == 1)
{
median = students[total/2];
}
cout<< "The median is: " << median << endl;

//MODE
int x;
int freq = 0;

frequency = new int[freq];

for (int count = 0; count < stdSurveyed; count++)
{
x=students[total];
frequency[x]++;
}
cout << "The mode is: " << mode << endl;


delete [] students;
students = 0;

system ("pause");
return 0;

}

And here is what the output looks like:

How many students are being surveyed? 5
How many movies did each student see? Enter below:
Student 1: 1
Student 2: 2
Student 3: 2
Student 4: 2
Student 5: 3
The average is: 2
The median is: 3
The mode is: 0
Press any key to continue . . .

Well here in the MODE:

//MODE
int x;
int freq = 0;

frequency = new int[freq];

for (int count = 0; count < stdSurveyed; count++)
{
x=students[total];
frequency[x]++;
}
cout << "The mode is: " << mode << endl;

You are outputting mode without doing anything to modify it where you are supposedly computing it.

In your MEDIAN section:

//MEDIAN
if (total % 2 == 0)
{
median = ((students[total/2-1])+(students[total/2])/2.0);
}
else if (total % 2 == 1)
{
median = students[total/2];
}
cout<< "The median is: " << median << endl;

You're using "total" to reference your students[], which is going to give you some ugly results. You want to use stdSurveyed and then use a floor function to get integer values (thats when you'll see errors about "integral type" )

Hope this helps

Thanks for the help. I fixed the median one. It gives me the median now, but it is for the middle student, not the middle number for the amount of movies per student. I'm thinking that I need to put in a sort function so that all the numbers go in descending order before it gets the median. I wrote a sort function, but when I input it and run the program, it stops after the sort function so any other output is not shown. Also, in the mode function, I made mode = x, but I am pretty sure that is wrong. Still working on it.

Also just realized that when I use an even number for students surveyed, it crashes the program.

Been working on my program and still having a few issues that I can't figure out. When I enter an even number of students surveyed, the program crashes. The median is finding the middle number for students, not movies and mode is only pulling from the first number entered. Here is my code and output.

Code:

#include <iostream>
#include <iomanip>
using namespace std;



int main()
{
    int *movie; //dynamic allocator
    int stdSurveyed;  //amount of students surveyed, user input
    int total = 0;
    int *frequency;

  double  average = 0, median = 0, mode = 0;

  // USER INPUT
  cout << "How many students are being surveyed? ";
  cin >> stdSurveyed;

  movie = new int[stdSurveyed]; //allocated memory.

  //EACH STUDENT INPUT
  cout << "How many movies did each student see? Enter below: " << endl;
  for (int count = 0; count < stdSurveyed; count++)
  {
  cout << "Student " << (count + 1) << ": ";
  cin >> movie[count];
  }

  //AVERAGE
  for (int count = 0; count < stdSurveyed; count++)
  {
   total += *(movie + count);
  }
  average = total / stdSurveyed;
  cout << "The average is: " << average << endl;

    //MEDIAN

    if ((stdSurveyed % 2) == 0)
    {
    median = ((movie[(stdSurveyed/2)-1])+(movie[stdSurveyed/2])/2.0);
    }
    else if ((stdSurveyed % 2) == 1)
    {
    median = movie[stdSurveyed/2];
    }
    cout<< "The median is: " << median << endl;

  //MODE
    int x;

    for (int count = 0; count < stdSurveyed; count++)
    {
        x = movie[stdSurveyed];
        frequency[x]++;
        total += movie[stdSurveyed];
        average = total;
        mode = movie[x];
    }
     cout << "The mode is: " << mode << endl;

  delete [] movie;
  movie = 0;

  system ("pause");
  return 0;

} 

Output:

How many students are being surveyed? 5
How many movies did each student see? Enter below:
Student 1: 1
Student 2: 2
Student 3: 3
Student 4: 4
Student 5: 4
The average is: 2
The median is: 3
The mode is: 1
Press any key to continue . . .

I am at a loss and really don't know where I am going wrong.

I found some of your bug.
First of all you mist a set of parenthesis where you calculate the median for even number.

2. This line is wrong

x = movie[stdSurveyed];

Youre accessing an element of youre array that doesn't exist.
Let's say you enter 5 for the number of student surveyed. The line above try to access the movie[5] element.
Remember array indexing start at 0.

Try working the rest from these correction

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.