I need help with my coding I need to be able to take in at most 300 integers(0 to 100) and then fin the count of zeros and evens. After this i need to add all of the integers up and get the average. Find the maximum and minimum values and then get the first odd given and last odd given. Following this I need to get the standard deviation, and then sort the numbers and show the first 10. I get all of this done fine and then i go into making a frequency table and fining the mode of this table. I can get one number to show up for mode but if i use (ex: 1,2,3,4,4,4,5,6,6,6) it will only show me the 6 as the mode how can i make it show me the average of the two 4 and 6?

My code is for the mode and frequency table is

//freq = 0
    int freq[100];
    for(i = 0; i <= 100; i++)
    {
        freq[i] = 0;
        for(j = 1; j <= quant; j++)
        {
            if(data[j] == i)
                freq[i]++;
        }
    }

//mode
    int mode;
    mode = -100;
    for (i = 0; i <= quant; i++)
        cout << i <<" appears "<<freq[i]<<" times."<<endl;
    for(i = 0; i <= 100; i++)
    {
     if(freq[i] > mode)
        mode = i;
    }
    cout<<mode<<endl;

Please Help me.

The first thing I see is that the frequency code is extremely inefficient. It has a double loop, i.e. you loop over 100 times over all the quanties. If you had 300 numbers to read then you would have 30000 steps!!

There is also a horrible memory error. Your array is 100 units long. but you index freq[100]. Since array start from 0. You only have memory up to freq[99]. [Note that the array index is a short way of saying how many steps away from the start item

I suspect that you have made the same error with the array data. That may be effecting your result, because data[100] may contain corrupted data.

What about this:

int freq[100]; 
for(int i=0;i<100;i++)
   freq[i]=0;

for(int i=0;i<quant;i++)
  {
     if (data[i]>0 || data[i]<101)
       freq[data[i]-1]++;
     else
        {
           std::cout<<"Data out of range :"
               <<data[i]<<std::endl;
        }
  }

That gets your frequency a bit better...

Now to the actual question:

What you have their is a mess, if you have let use say this:
3,3,3,4,4,4,7,7,7. Then you would want the average of 3+4+7.
So you would need to (a) store the running total, (b) store the number of contributions to the running total, (c) store the number of items in the biggest mode value, so that if you then had this : 3,3,3,4,4,4,7,7,7,8,8,8,8, you would discard the running total of 3+4+7, in favour of just 8.

// Declare and initialize our three variables:
int modeCnt(0);   // Count of blocks contributing to sum.
int modeSum(0);   // Sum in the mode
int modeFreq(0);  // This is the freq number
for(int i=0;i<100;i++)
  {
    if (freq[i]>modeFreq)
      {
         modeCnt=1;
         modeSum=i+1; // note freq[0] refers to frequency of 1. 
         modeFreq=1;
      }
    else if (freq[i]==modeFreq)
      {
         modeCnt++;
         modeSum+=i+1;
      }
   }
 // Now just plot out the mode:

 // NOTE: Some care here because int/int will not give the
 // correct value. 
 std::cout<<"Mode == "<<modeSum/(1.0*modeCnt)<<std::endl;

Think that covers most of the problems..

now your way to find mod works with my program but when i try to use your freqncy table it crashes my program why is this?

I made a slight error : the line: if (data[i]>0 || data[i]<101)
should have read if (data[i]>0 && data[i]<101).

Obviously I just typed the code into the post, I didn't actually compile/run it -- I will learn... :-)

So I suspect that you have a data value that is above 100 or below zero....

If that doesn't work can you give the full program that you are testing and we can have a longer look..

I have it working now but I was just woundering why that was happening
If you would like to see my whole program tell me and ill show you it.

Edited 2 Years Ago by Brennan_1

If you have it working then there is no need. It's just easier for us to help you to see your new code. Telling us it doesn't work, is useless... It's like saying 'I'm ill help me.'

How can a doctor diagnose a problem without knowing your symptoms?

Edited 2 Years Ago by iamthwee

This question has already been answered. Start a new discussion instead.