This code keep giving me 0 for mode

  • Thread starter Thread starter Demon117
  • Start date Start date
  • Tags Tags
    Code Mode
Click For Summary

Discussion Overview

The discussion revolves around a code snippet intended to calculate the mode of an array in C++. Participants are examining the implementation details, potential errors, and coding practices related to the mode function, including array size handling and naming conventions.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Conceptual clarification

Main Points Raised

  • One participant points out that the first call to the mode function should use the correct size of the array, suggesting it should be mode(i, 10) instead of mode(i, 100).
  • Another participant questions how many times 0 appears in the array of 100 integers starting at the memory pointed to by i, implying a potential misunderstanding of the data being processed.
  • Several participants recommend using sizeof(i)/sizeof(i[0]) to determine the size of the array at compile time, arguing it is a better practice than hardcoding the size.
  • One participant notes that using the name 'i' for the array is not ideal, as it is commonly used as a loop counter in C and C++ programming.
  • Another participant mentions that using container classes would be more appropriate in C++, indicating a preference for modern C++ practices over raw arrays.

Areas of Agreement / Disagreement

Participants express differing views on the best practices for handling array sizes and naming conventions. There is no consensus on the best approach to take, and the discussion remains unresolved regarding the optimal implementation.

Contextual Notes

The discussion highlights potential pitfalls in passing array sizes and naming conventions, but does not resolve the implications of these practices on the functionality of the code.

Demon117
Messages
162
Reaction score
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
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.
 
Well, how many times does 0 appear in the array of 100 integers starting at the memory pointed to by i?
 
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.
 
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. :)
 

Similar threads

  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 15 ·
Replies
15
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 24 ·
Replies
24
Views
2K
Replies
8
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 10 ·
Replies
10
Views
2K