| | |
Multimode//No mode?
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Apr 2007
Posts: 15
Reputation:
Solved Threads: 0
after hours of trying (im really new to C++), i got a program to run that will find the mean, median, mode, and standard deviation of an array of numbers. However, if there is no mode (every number only appears once), it just returns the first number for mode. if there is more then one mode, it just returns the first mode. ive tried using a few if statements, but it screwed up everything and wouldnt run. ::points down:: this way will run. any suggestions on what i can do to make it happen?
//MODE HERE
int modeIndex=0;
int modeFrequency=0;
int maxModeIndex=0;
int maxModeFrequency=0;
for(int i=0 ; i<length ; i++)
{
if(list[i]!=list[modeIndex])
{
if(modeFrequency>1)
{
cout<< list[modeIndex] << " occurs " << modeFrequency << " number of times" << endl;
}
if(modeFrequency>maxModeFrequency)
{
maxModeIndex = modeIndex;
maxModeFrequency = modeFrequency;
}
modeIndex = i;
modeFrequency=1;
}
else
{
modeFrequency++;
}
}
// validate the last set of elements in the list.
if(modeFrequency>1)
{
cout<< list[modeIndex] << " occurs " << modeFrequency << " number of times" << endl;
}
if(modeFrequency>maxModeFrequency)
{
maxModeIndex = modeIndex;
maxModeFrequency = modeFrequency;
}
cout<< "Mode is: " << list[maxModeIndex] << endl;
return 0;
//MODE HERE
int modeIndex=0;
int modeFrequency=0;
int maxModeIndex=0;
int maxModeFrequency=0;
for(int i=0 ; i<length ; i++)
{
if(list[i]!=list[modeIndex])
{
if(modeFrequency>1)
{
cout<< list[modeIndex] << " occurs " << modeFrequency << " number of times" << endl;
}
if(modeFrequency>maxModeFrequency)
{
maxModeIndex = modeIndex;
maxModeFrequency = modeFrequency;
}
modeIndex = i;
modeFrequency=1;
}
else
{
modeFrequency++;
}
}
// validate the last set of elements in the list.
if(modeFrequency>1)
{
cout<< list[modeIndex] << " occurs " << modeFrequency << " number of times" << endl;
}
if(modeFrequency>maxModeFrequency)
{
maxModeIndex = modeIndex;
maxModeFrequency = modeFrequency;
}
cout<< "Mode is: " << list[maxModeIndex] << endl;
return 0;
Last edited by Ancient Dragon; Apr 23rd, 2007 at 11:07 pm. Reason: corrected code tags
the formatting is really horrible -- how did you get all those color code tags there? I just simply removed the code tags to make your code readable.
Last edited by Ancient Dragon; Apr 23rd, 2007 at 11:08 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Oh certainly. It is how it appears in the forum -- no formatting and everything in the left-hand margin. Makes it really difficult to read and most people will probably just not take the time to fiture it out.
What version of Visual Studio are you using? I don't have that problem with 2005.
What version of Visual Studio are you using? I don't have that problem with 2005.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
•
•
Join Date: Apr 2007
Posts: 15
Reputation:
Solved Threads: 0
c Syntax (Toggle Plain Text)
//MODE HERE int modeIndex=0; int modeFrequency=0; int maxModeIndex=0; int maxModeFrequency=0; for(int i=0 ; i<length ; i++) { if(list[i]!=list[modeIndex]) { if(modeFrequency>1) { cout<< list[modeIndex] << " occurs " << modeFrequency << " number of times" << endl; } if(modeFrequency>maxModeFrequency) { maxModeIndex = modeIndex; maxModeFrequency = modeFrequency; } modeIndex = i; modeFrequency=1; } else { modeFrequency++; } } // validate the last set of elements in the list. if(modeFrequency>1) { cout<< list[modeIndex] << " occurs " << modeFrequency << " number of times" << endl; } if(modeFrequency>maxModeFrequency) { maxModeIndex = modeIndex; maxModeFrequency = modeFrequency; } cout<< "Mode is: " << list[maxModeIndex] << endl; return 0;
Last edited by Ancient Dragon; Apr 23rd, 2007 at 11:42 pm. Reason: corrected code tags
>>However, if there is no mode (every number only appears once), it just returns the first number for mode. if there is more then one mode, it just returns the first mode.
What behavior do you want it to do ?
What behavior do you want it to do ?
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
![]() |
Other Threads in the C++ Forum
- Previous Thread: hex code
- Next Thread: Can't figure out whats wrong
| Thread Tools | Search this Thread |
api array arrays based binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete deploy developer dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game generator getline givemetehcodez graph gui homeworkhelp iamthwee ifstream input int java lib linkedlist linker list loop looping loops map math matrix memory multiple news node number numbertoword output pointer problem program programming project python random read recursion recursive reference rpg sorting string strings temperature template test text text-file tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets







The green color is a result of code tags. Now we have something to study.