G
GRoll21
I have a program here that asks the number of students surveyed. then
it will ask how many movies each student has watched. After thats been
collected it does functions to find the average, median, and mode.
The problem I am having is getting it to return the mode. I have a
function set up but I'm not sure if it will correctly return the mode.
I am getting 1 error.
c:\C++\math\math.cpp(49): error C2664: 'getMode' : cannot convert
parameter 2 from 'int' to 'int []'
this is the line the error says its on: getMode(movies,surv);
Here is the full code:
//math assign
#include <iostream>
#include <iomanip>
using namespace std;
//prototypes
float getAverage(int, float); // get average of movies
float getMedian(int [], int); // get median ove movies
void getMode(int[], int[]); //get mode of movies
float average;
int main()
{
float total=0;
int count, surv, *movies;
float medianMain;
cout << "How many students were surveyed?: ";
cin >> surv;
while(surv < 0)
{
cout <<"\nPlease enter a non-negitive number: ";
cin >> surv;
}
movies = new int[surv]; //allocate memory
//get the movie ammount
cout << "Enter the number of movies each student saw.\n";
for (count = 0; count < surv; count++)
{
cout << "Student " << (count +1) << ": ";
cin >> movies[count];
while (movies[count] < 0)
{
cout <<"\nPlease enter a non-negitive number: ";
cin >> movies[count];
}
}
//calc the total movies
for (count =0; count < surv; count++)
{
total += movies[count];
}
getAverage(surv, total);
medianMain = getMedian(movies,surv);
getMode(movies,surv);
//display the results
cout << fixed << showpoint << setprecision(2);
cout << "\nTotal Movies: " << total <<endl;
cout << "Average Movies: " << average << endl;
cout << "Median Movies: " << medianMain <<endl;
//free dynamically alocated meory
delete []movies;
return 0;
}
float getAverage(int surv, float total)
{
average = total/surv;
return average;
}
float getMedian(int movies[], int amt)
{
bool swap;
int temp;
int type;
float value1;
float value2;
float median;
do
{
swap = false;
for (int count = 0; count < (amt - 1); count++)
{
if (movies[count] > movies[count + 1])
{
temp = movies[count];
movies[count] = movies[count + 1];
movies[count + 1] = temp;
swap = true;
}
}
} while (swap);
type = amt % 2;
if ( type == 1)
{
median = movies[amt / 2];
}
else
{
value1 = movies[amt / 2];
value2 =(movies[amt / 2] - 1);
median = (value1 + value2) / 2;
}
return median;
}
void getMode(int movies[], int amt)
{
int index;
int count=0;
int max =0;
for(index=0; index<amt; index++)
{
if(movies[index]==movies[index+1])
count++;
else
{
if(count>max)
max=count;
count=0;
}
}
}
Any help or suggestions would be great. Thank you very much!
it will ask how many movies each student has watched. After thats been
collected it does functions to find the average, median, and mode.
The problem I am having is getting it to return the mode. I have a
function set up but I'm not sure if it will correctly return the mode.
I am getting 1 error.
c:\C++\math\math.cpp(49): error C2664: 'getMode' : cannot convert
parameter 2 from 'int' to 'int []'
this is the line the error says its on: getMode(movies,surv);
Here is the full code:
//math assign
#include <iostream>
#include <iomanip>
using namespace std;
//prototypes
float getAverage(int, float); // get average of movies
float getMedian(int [], int); // get median ove movies
void getMode(int[], int[]); //get mode of movies
float average;
int main()
{
float total=0;
int count, surv, *movies;
float medianMain;
cout << "How many students were surveyed?: ";
cin >> surv;
while(surv < 0)
{
cout <<"\nPlease enter a non-negitive number: ";
cin >> surv;
}
movies = new int[surv]; //allocate memory
//get the movie ammount
cout << "Enter the number of movies each student saw.\n";
for (count = 0; count < surv; count++)
{
cout << "Student " << (count +1) << ": ";
cin >> movies[count];
while (movies[count] < 0)
{
cout <<"\nPlease enter a non-negitive number: ";
cin >> movies[count];
}
}
//calc the total movies
for (count =0; count < surv; count++)
{
total += movies[count];
}
getAverage(surv, total);
medianMain = getMedian(movies,surv);
getMode(movies,surv);
//display the results
cout << fixed << showpoint << setprecision(2);
cout << "\nTotal Movies: " << total <<endl;
cout << "Average Movies: " << average << endl;
cout << "Median Movies: " << medianMain <<endl;
//free dynamically alocated meory
delete []movies;
return 0;
}
float getAverage(int surv, float total)
{
average = total/surv;
return average;
}
float getMedian(int movies[], int amt)
{
bool swap;
int temp;
int type;
float value1;
float value2;
float median;
do
{
swap = false;
for (int count = 0; count < (amt - 1); count++)
{
if (movies[count] > movies[count + 1])
{
temp = movies[count];
movies[count] = movies[count + 1];
movies[count + 1] = temp;
swap = true;
}
}
} while (swap);
type = amt % 2;
if ( type == 1)
{
median = movies[amt / 2];
}
else
{
value1 = movies[amt / 2];
value2 =(movies[amt / 2] - 1);
median = (value1 + value2) / 2;
}
return median;
}
void getMode(int movies[], int amt)
{
int index;
int count=0;
int max =0;
for(index=0; index<amt; index++)
{
if(movies[index]==movies[index+1])
count++;
else
{
if(count>max)
max=count;
count=0;
}
}
}
Any help or suggestions would be great. Thank you very much!