473,396 Members | 1,767 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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

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
9 2433
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
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: Guyon Morée | last post by:
what is the difference? if I open a text file in binary (rb) mode, it doesn't matter... the read() output is the same.
6
by: Dimitris Mandalidis | last post by:
Hello all, Suppose we have the following struct : struct foo { int a; int b; char *c; } And :
6
by: Tamir Khason | last post by:
How to prevent the selected cell from being editable (visual) at DataGrid? Once click on cell (even readonly) there are cursor inside it and select text appears. How to prevent it Thankx
9
by: Edward Diener | last post by:
I received no answers about this the first time I posted, so I will try again. My inability to decipher an MSDN topic may find others who have the same inability and someone who can decipher and...
1
by: Sluggoman | last post by:
Cannot seem to wrap my head around this, could someone help me with the logic? I need to pull the value which occurs most frequently from array - am writing a mini program to get logic sorted out,...
6
by: Megantsu | last post by:
What is the easiest way to program a way to figure mode? I am just having to do it with some simple Int.... Any suggestions?
0
by: Vincent | last post by:
Dear all, I have implemented a class to export the content of RichTextBox to image in WYSISYG mode so that line breaks on the screen are the same as exported. C# Code: public struct...
1
by: Sathyaish | last post by:
I have the following scenario: Algorithm: 3DES Cipher Mode: CBC Key Size: 128-bit Block Size: 64 bit IV: 0x0000000000000000 (an eight byte array of zeros) The results I get using .NET with...
10
by: albert.neu | last post by:
To what extent does Microsoft Visual C++ also called: VC++ also called: Microsoft (R) 32-bit C/C++ Optimizing Compiler support C99? VC++ compiles either in C mode (which is def. *not*...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.