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!=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;

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.

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.

are you sure ? I've done it hundreds of times without that problem. Probably some setting you have made in the editor.

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...

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.

//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;

better? its still showing up in colors. i pasted it into notepad first this time.

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

>>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 ?

>>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 ?

cout << "no mode" or "null" (doesnt really matter)

and if there is multiple modes, list them

what I would do is create another list of all the modes. The way you have it programmed it can print only one mode. Create a 2d array, the first dimension will have the actual value and the second dimension the number of times that value occurs in the original array. Something like this (untested)

int array[2][length] = {0};

// populate array with values
int nItemsInList = 1;
array[0][0] = list[0];
array[0][1] = 1;
for(int i = 1; i < length; i++)
{
   // find list[i] in array[0];
  int found = 0;
   for(int j = 0; j < nItemsInList; j++)
   {
         // search the array for list[i].  If found, then just increment
         // its count.  If not found, then add it to the array.
         if( array[0][j] == list[i] )
         {
              array[1][j]++;
              found = 1;
               break;
          }
     }       
      if( found == 0)
      {
               // not found in the array, so we must add it here.
               array[0][nItemsInList] = list[i];
               array[1][nItemsInList] = 1;
                nItemsInList++;
       }
}

Now all that is left is to run through the array and find the items(s) with the largest quantity.

would i need to declare any more variables? sorry, i kind of need things spelled out for me. thanks for the insight, ill test it soon

see line 1

thanks... still triyng

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.