943,603 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1972
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Apr 23rd, 2007
0

Multimode//No mode?

Expand Post »
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;
Last edited by Ancient Dragon; Apr 23rd, 2007 at 11:07 pm. Reason: corrected code tags
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Ortal is offline Offline
15 posts
since Apr 2007
Apr 23rd, 2007
0

Re: Multimode//No mode?

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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,947 posts
since Aug 2005
Apr 23rd, 2007
0

Re: Multimode//No mode?

the formatting of the c++ is horrible, as in the xyntax and logic? or how it copy/pasted?

i just opened up the .cpp file and copy/pasted. visual studio automatically colors my tags like that.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Ortal is offline Offline
15 posts
since Apr 2007
Apr 23rd, 2007
0

Re: Multimode//No mode?

are you sure ? I've done it hundreds of times without that problem. Probably some setting you have made in the editor.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,947 posts
since Aug 2005
Apr 23rd, 2007
0

Re: Multimode//No mode?

can you please specify if you are talking about my actual C++ or how it appears on the firum. cause i dont know what you mean...
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Ortal is offline Offline
15 posts
since Apr 2007
Apr 23rd, 2007
0

Re: Multimode//No mode?

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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,947 posts
since Aug 2005
Apr 23rd, 2007
0

Re: Multimode//No mode?

  1.  
  2. //MODE HERE
  3. int modeIndex=0;
  4. int modeFrequency=0;
  5. int maxModeIndex=0;
  6. int maxModeFrequency=0;
  7. for(int i=0 ; i<length ; i++)
  8. {
  9. if(list[i]!=list[modeIndex])
  10. {
  11. if(modeFrequency>1)
  12. {
  13. cout<< list[modeIndex] << " occurs " << modeFrequency << " number of times" << endl;
  14. }
  15. if(modeFrequency>maxModeFrequency)
  16. {
  17. maxModeIndex = modeIndex;
  18. maxModeFrequency = modeFrequency;
  19. }
  20. modeIndex = i;
  21. modeFrequency=1;
  22. }
  23. else
  24. {
  25. modeFrequency++;
  26. }
  27. }
  28. // validate the last set of elements in the list.
  29. if(modeFrequency>1)
  30. {
  31. cout<< list[modeIndex] << " occurs " << modeFrequency << " number of times" << endl;
  32. }
  33. if(modeFrequency>maxModeFrequency)
  34. {
  35. maxModeIndex = modeIndex;
  36. maxModeFrequency = modeFrequency;
  37. }
  38. cout<< "Mode is: " << list[maxModeIndex] << endl;
  39.  
  40. return 0;
Last edited by Ancient Dragon; Apr 23rd, 2007 at 11:42 pm. Reason: corrected code tags
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Ortal is offline Offline
15 posts
since Apr 2007
Apr 23rd, 2007
0

Re: Multimode//No mode?

better? its still showing up in colors. i pasted it into notepad first this time.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Ortal is offline Offline
15 posts
since Apr 2007
Apr 23rd, 2007
0

Re: Multimode//No mode?

great work The green color is a result of code tags. Now we have something to study.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,947 posts
since Aug 2005
Apr 23rd, 2007
0

Re: Multimode//No mode?

>>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 ?
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,947 posts
since Aug 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: hex code
Next Thread in C++ Forum Timeline: Can't figure out whats wrong





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC