Get Mode. Can't convert int to int[]

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!
 
D

deane_gavin

GRoll21 said:
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 []'

That's a very clear and precise error message. Look at your getMode
prototype and look what types of parameters you try and pass to it.

void getMode(int[], int[]); //get mode of movies

int main()
{
float total=0;
int count, surv, *movies;

getMode(movies,surv);

<snip>

Gavin Deane
 
G

GRoll21

stupid error on my part. i have it returning the mode, but it returns a
memory address. here is what i have printing out.

here is the function:

void mode(int dubers[], int elems)
{
int index;
int count=0;
int max =0;
for(index=0; index<elems; index++)
{
if(dubers[index]==dubers[index+1])
count++;
else
{
if(count>max)
max=count;
count=0;
}
}

}

and here is what im printing out.

cout << fixed << showpoint << setprecision(2);
cout << "\nTotal Movies: " << total <<endl;
cout << "Average Movies: " << average << endl;
cout << "Median Movies: " << medianMain <<endl;
cout << "Mode Movies: " <<*getMode <<endl;

uder the *getMode, its printing out the memory address. Anyone know why
its doing that and how i can make it print out the value?
 
G

GRoll21

stupid error on my part. i have it returning the mode, but it returns a

memory address. here is what i have printing out.


here is the function:


void getMode(int movies[], int amt)
{
int index;
int count=0;
int theMode =0;
for(index=0; index<amt; index++)
{
if(movies[index]==movies[index+1])
count++;
else
{
if(count>theMode)
theMode=count;
count=0;
}
}
}


and here is what im printing out.

cout << fixed << showpoint << setprecision(2);
cout << "\nTotal Movies: " << total <<endl;
cout << "Average Movies: " << average << endl;
cout << "Median Movies: " << medianMain <<endl;
cout << "Mode Movies: " <<*getMode <<endl;


the *getMode, its printing out the memory address. Anyone know why
its doing that and how i can make it print out the value?
 
M

Marcus Kwok

GRoll21 said:
stupid error on my part. i have it returning the mode, but it returns a

memory address. here is what i have printing out.


here is the function:


void getMode(int movies[], int amt)
{
int index;
int count=0;
int theMode =0;
for(index=0; index<amt; index++)
{
if(movies[index]==movies[index+1])
count++;
else
{
if(count>theMode)
theMode=count;
count=0;
}
}
}


and here is what im printing out.

cout << fixed << showpoint << setprecision(2);
cout << "\nTotal Movies: " << total <<endl;
cout << "Average Movies: " << average << endl;
cout << "Median Movies: " << medianMain <<endl;
cout << "Mode Movies: " <<*getMode <<endl;


the *getMode, its printing out the memory address. Anyone know why
its doing that and how i can make it print out the value?

Your getMode() function does not return anything (you declared it as
returning void).

Once you fix this, when you print it, you need to call the function (by
passing in the appropriate parameters), like:

cout << "Mode Movies: " << getMode(something, somethingElse) << endl;

I believe the way you have it written, you are just printing the memory
address of the function.
 
G

GRoll21

i've changed my function to return theMode. it returns the value of 1
every time. here is my code now, it runs but like i said prints 1.00
for the mode. any ideas?

//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
float getMode(int[], int); //get mode of movies
float average;



int main()
{
float total=0;
int count, surv, *movies;
float medianMain;
float thisMode;


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);
thisMode = 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;
cout << "Mode Movies: " <<thisMode <<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;
}

float getMode(int movies[], int amt)
{
int index;
int count=0;
int theMode =0;
for(index=0; index<amt; index++)
{
if(movies[index]==movies[index+1])
count++;
else
{
if(count>theMode)
theMode=count;
count=0;
}
}
return theMode;
}
 
T

Thomas J. Gritzan

GRoll21 said:
i've changed my function to return theMode. it returns the value of 1
every time. here is my code now, it runs but like i said prints 1.00
for the mode. any ideas?
[...]
float getMode(int movies[], int amt)
{
int index;
int count=0;
int theMode =0;
for(index=0; index<amt; index++)
{
if(movies[index]==movies[index+1])
count++;
else
{
if(count>theMode)
theMode=count;
count=0;
}
}
return theMode;
}

It prints 1 because the function does not calculate what you want. It
returns how many times the most frequent occurred. When entered 10
different numbers, it return 1.

But this:
if(movies[index]==movies[index+1])

Is a bad idea, you are accessing out of bounds of the array.

Thomas
 
J

John Harrison

GRoll21 said:
i've changed my function to return theMode. it returns the value of 1
every time. here is my code now, it runs but like i said prints 1.00
for the mode. any ideas?
[snip]

float getMode(int movies[], int amt)
{
int index;
int count=0;
int theMode =0;
for(index=0; index<amt; index++)
{
if(movies[index]==movies[index+1])
count++;
else
{
if(count>theMode)
theMode=count;
count=0;
}
}
return theMode;
}

Why float, theMode is an int, so why is the return a float? Just curious.

Now look at the logic in your loop. It doesn't return anything like the
modal value. You're counting something (I have no idea what) and
returning the count. A count cannot be a modal value.

You seem to be assuming that the movies are already sorted. And trying
to count the longest sequence. You have to realise that the longest
sequence is not the modal value. It's the value of one of the _members_
of the longest sequence that is the modal value.

john
 
T

Thomas J. Gritzan

John said:
GRoll21 said:
float getMode(int movies[], int amt)
[...]

You seem to be assuming that the movies are already sorted. And trying
to count the longest sequence.

He counts the longest sequence and he assumes, that the values are
sorted, because getMedian does a bubble sort:
float getMedian(int movies[], int amt)
{ [...]
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);
[...]

@GRoll21: You should rethink about your commenting style. And you should
export the sorting into another function. Or use a std:: algorithm.

Thomas
 
J

John Harrison

Thomas said:
John said:
GRoll21 said:
float getMode(int movies[], int amt)
[...]

You seem to be assuming that the movies are already sorted. And trying
to count the longest sequence.


He counts the longest sequence and he assumes, that the values are
sorted, because getMedian does a bubble sort:

Yes I read the code. I just wasn't sure whether the OP was conscious
that he was making that assumption. I hope he is now.

OP - think about what you've written. You have a function getMode that
can only work if getMedian has been called first. Do you think that is a
good state of affairs? What can you do about it?

Functions should be independent of each other as far as possible. The
way you've written getMode it is dependent on getMedian. This is
obviously an assignment, you are going to lose marks for this issue.

But get the getMode function fixed first.

john
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

I can't get this program to run properly 2
int to char problem ? 4
conversion from float to int& 2
I rea......lly need your help ( C++ ) 4
sorting 1
sorting char array 13
error 7
Using centinel 3

Members online

No members online now.

Forum statistics

Threads
473,999
Messages
2,570,243
Members
46,835
Latest member
lila30

Latest Threads

Top