-1

I have a set of numbers:

23690 24009 23976 23827 23751 23787 23932 23914 23903 23956 23937 23942 23909 23952 

and I'm trying to find the amount of times that the numbers go below 24000.

I have tried this:

void findBelow(const int arr[], int count)
{
  int n = sizeof&(arr) / sizeof(arr[0]);
  count = 0;
  const int threshold = 24000;

  for (int i = 0; i < n; i++)
  {
    if (arr[i] < threshold)
    {
      count++;
    }
  }
  cout << "Count = "<< count;
}

But it doesn't output the correct amount of numbers.

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
Piklez
  • 13
  • 5
  • 2
    Debug your code. Print intermediate variables and compare them with what they should be, until you find the source of the problem. – user31264 Sep 11 '20 at 02:30
  • 2
    You made an error when calculating the size of the array.You can ouput `n` to view.And the `count` parameter is of no use to you, you can try to explain the reason for passing it, maybe you want to pass the array size? – Zeus Sep 11 '20 at 02:30
  • 2
    Store the numbers in a `std::vector`, and use an ``. Also, does `count` need to be a parameter? – cigien Sep 11 '20 at 02:32
  • 1
    Helpful reading: [What is array to pointer decay?](https://stackoverflow.com/questions/1461432/what-is-array-to-pointer-decay) – user4581301 Sep 11 '20 at 02:35
  • The `sizeof(arr) / sizeof(arr[0])` technique does not work on arrays passed as arguments to functions, since the passed array is converted to a pointer - and a pointer provides no information about the size of the array. You either need to pass the array size as an extra argument, or use a standard container (e.g. `std::vector`) which DOES carry information about its number of elements. – Peter Sep 11 '20 at 04:03
  • Incidentally, `sizeof&(arr)/sizeof(arr[0])` is incorrect usage of the technique, since `&arr` and `arr` have different types. – Peter Sep 11 '20 at 04:04

2 Answers2

1

When arrays are passed as parameters to functions, type information about the amount of elements on it is lost. This declaration void f(int a[]) is equivalent to this one void f(int* a)

So, in this case, you can't know the number of elements in the array just getting it's size. You should use the count parameter for that. Maybe in a loop like this:

for (int i = 0; i != count; ++i)
{
}

Another (almost always better) option is to use a standard container. Check: std::vector, std::array for example.

DeltA
  • 564
  • 4
  • 12
0

Ok thank you all for helping me, im still learning so i corrected some of my errors.

void findBelow(const int arr[], int n)
{
  int count = 0;
  const int threshold = 23650;

  for (int i = 0; i < n; i++)
  {
    if (arr[i] < threshold)
    {
      count++;
    }
  }
  cout << "Count = "<< count;
}

I counted the number of elements in the array in another function which gave me a more accurate number of elements to compare.

Piklez
  • 13
  • 5
  • Tactical note: A function that returns `count` rather than printing it is much more useful in the long run. If you give the caller the result, they can print it, use it in a computation, pass it to another function, or whatever possibilities present themselves. If you give the user nothing and print the result in the function, there is exactly one use for the function. This leads toward the old Unix maxim of "Do one thing and do it well." This function does two things, counts and prints, and is less useful as a result. – user4581301 Sep 11 '20 at 03:19
  • Another small note: The function will be even more useful if it accepts the threshold as a parameter. That way you can keep using the same function over and over with different thresholds. – user4581301 Sep 11 '20 at 03:20