This code keep giving me 0 for mode

  • Thread starter Demon117
  • Start date
  • Tags
    Code Mode
In summary, the code has two arrays, one of 100 integers and the other of char. When main is run, the code prints the mode of both arrays. The mode of the char array is "lame question", while the mode of the integer array is "0".
  • #1
Demon117
165
1
This is the code I have:

Code:
#include <iostream>
#include <cstring>
using namespace std;


template <class X> X mode(X *data, int size)
{
  register int t, w;
  X md, oldmd;
  int count, oldcount;

  oldmd = 0;
  oldcount = 0;
  for(t=0; t<size; t++) {
    md = data[t];
    count = 1;
    for(w = t+1; w < size; w++) 
      if(md==data[w]) count++;
    if(count > oldcount) {
      oldmd = md;
      oldcount = count;
    }
  }
  system("pause");
  return oldmd;
}

int main()
{
  int i[] = { 2, 3, 4, 2, 1, 2, 2, 5, 6, 7};
  char *p = "lame question";

  cout << "mode of i: " << mode(i, 100) << endl;
  cout << "mode of p: " << mode(p, (int) strlen(p))<<endl;
  system("pause");
  return 0;
}

Why does my mode keep coming back for i?
 
Physics news on Phys.org
  • #2
Shouldn't your first call to mode be mode(i, 10)? Your array has 10 elements in it, not 100.

Also, it's not the best style to name your array i. C or C++ programmers typically use i as a loop counter variable of type int.
 
  • #3
Well, how many times does 0 appear in the array of 100 integers starting at the memory pointed to by i?
 
  • #4
When you have a function that takes an array as a parameter, as your mode function does, the usual practice is to pass the address of the array and the size of the array (which you are doing). However, it's better to let the compiler calculate the size of the array at compile time, like this:
Code:
cout << "mode of i: " << mode(i, sizeof (i)/sizeof(i[0])) << endl;
Assuming four-byte ints, sizeof(i) evaluates to 40, and sizeof(i[0]) evaluates to 4. The quotient is 10, the number of elements in your array.
 
  • #5
Mark44 said:
When you have a function that takes an array as a parameter, as your mode function does, the usual practice is to pass the address of the array and the size of the array (which you are doing). However, it's better to let the compiler calculate the size of the array at compile time, like this:
Code:
cout << "mode of i: " << mode(i, sizeof (i)/sizeof(i[0])) << endl;
Assuming four-byte ints, sizeof(i) evaluates to 40, and sizeof(i[0]) evaluates to 4. The quotient is 10, the number of elements in your array.
Alas this practice is prone to error when you decide to make i dynamically allocated.

Really, since he's using C++, he should be using container classes. :)
 

FAQ: This code keep giving me 0 for mode

What does a mode value of 0 mean in this code?

A mode value of 0 means that there is no most frequently occurring value in the data set. This could be due to a lack of repetition or an even distribution of values.

How can I fix the code to give me a correct mode value?

To fix the code, you will need to review the algorithm or logic used to calculate the mode and make sure it is correctly identifying and counting the most frequently occurring value.

Does the data need to be in a specific format for the mode to be calculated?

No, the mode can be calculated for any type of data, as long as the algorithm used is suitable for the data type. However, some data formats may require different algorithms or preprocessing steps to accurately calculate the mode.

Could there be any errors in the data that are causing the mode to be 0?

Yes, if there are missing or incorrect values in the data, it could result in the mode being 0. It is important to clean and validate the data before calculating the mode.

Is there a way to find the mode without using a coding language?

Yes, you can find the mode manually by arranging the data in ascending or descending order and identifying the value that appears most frequently. However, coding languages can make this process faster and more efficient for larger data sets.

Similar threads

Replies
3
Views
970
Replies
8
Views
1K
Replies
2
Views
3K
Replies
15
Views
2K
Replies
2
Views
1K
Replies
7
Views
1K
Back
Top