By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,515 Members | 1,772 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,515 IT Pros & Developers. It's quick & easy.

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

P: n/a
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!

Nov 2 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
GRoll21 wrote:
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.

<snip>
void getMode(int[], int[]); //get mode of movies
<snip>
int main()
{
float total=0;
int count, surv, *movies;
<snip>
getMode(movies,surv);


<snip>

Gavin Deane

Nov 2 '05 #2

P: n/a
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?

Nov 2 '05 #3

P: n/a
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?

Nov 2 '05 #4

P: n/a
GRoll21 <mr*****@gmail.com> wrote:
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.

--
Marcus Kwok
Nov 2 '05 #5

P: n/a
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;
}

Nov 2 '05 #6

P: n/a
GRoll21 schrieb:
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
Nov 2 '05 #7

P: n/a
GRoll21 wrote:
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
Nov 2 '05 #8

P: n/a
John Harrison schrieb:
GRoll21 wrote:
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:

GRoll21 wrote: 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
Nov 2 '05 #9

P: n/a
Thomas J. Gritzan wrote:
John Harrison schrieb:
GRoll21 wrote:
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
Nov 2 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.