2

For example, if I have an array of 5 inputted elements, how would I count how many times a specific value was entered if that value has already been established in a variable.

INPUT:
4
4
4
1
2

If click is defined as 4 then how would I count how many times click is used in the array? Hopefully that makes sense. Thanks

animuson
  • 53,861
  • 28
  • 137
  • 147
willis0924
  • 67
  • 5
  • 8

4 Answers4

15

As you've tagged your question as C++, here is a proper C++ answer, using STL.

int num = std::count(&array[0], &array[5], click);

See http://en.cppreference.com/w/cpp/algorithm/count

Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680
  • 2
    Hm. Is `&array[5]` actually valid? I know that deferencing and using address-of can cause undefined behaviour (as in, `&(*(array + 5))`). – Konrad Rudolph May 11 '11 at 17:31
  • 2
    @Oli: Why don't you use `array+5` instead of `&array[5]`? i.e ... `int num = std::count(array, array+5, click);` – Nawaz May 11 '11 at 17:44
  • Yes, &array[5] should be valid for a 5-element array. For an array of size N there are elements numbered 0 to N-1 but the address of element N is a valid address, even although accessing that element gives undefined behaviour. – AAT May 11 '11 at 17:46
  • @AAT: If *the address of element N is a valid address*, then why accessing that element gives undefined behavior? Any explanation for that? – Nawaz May 11 '11 at 17:58
  • 1
    @Konrad: It's certainly idiomatic. The C99 standard quote (I don't have the C++ standard to hand) is: "*If the result points one past the last element of the array object, it shall not be used as the operand of a unary * operator that is evaluated.*" (6.5.6/8) In this context, I don't think it's evaluated. – Oliver Charlesworth May 11 '11 at 18:05
  • @Nawaz: it might, for instance, be a memory location being used by another variable. Or it might not. The C++ standard just guarantees that you can do pointer arithmetic between any 2 array elements **and** the notional element which is just off the end of the array. – AAT May 11 '11 at 18:05
  • 1
    @Oli: The address operator `&` is an unary operator, that means, according to the C99 quotation which you quoted, `&array[5]` invokes undefined-behavior. Also, `&array[5]` is equivalent to `&(*(array+5))` which uses another unary operator `*`. – Nawaz May 11 '11 at 18:13
  • 1
    @AAT I know this, but it’s not relevant here: Oli isn’t merely addressing the element, he’s dereferencing it using `[]`. I know for a fact that my code (`&*(a + 5)`) has caused the debugger of at least one recent (post 6.0) version of VC++ to complain about invalid access. So whether or not it’s legal, it doesn’t always work. Since it was my impression that `a[b]` for C arrays is strictly equivalent to `*(a + b)`, this would mean that Oli’s code would exhibit the same problem. – Konrad Rudolph May 11 '11 at 18:14
  • @konrad: I believe that the C++ committee wants this, and your example to be allowed. If you follow the link, you'll find a good bit of discussion. Of note is where they say that "p=0; *p;" is only an error if there is an lvalue-to-rvalue conversion. http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#232. Of course, the issue is still open so the standard doesn't yet clearly state this. – Richard Corden May 11 '11 at 18:18
  • @AAT: arithmetic on the pointer doesn't invoke undefined-behavior, but dereferencing it does. – Nawaz May 11 '11 at 18:20
  • 5
    I agree with Konrad, while doing `array + 5` is perfectly fine, doing `array[5]` is not, even if it is only to apply the address-of operator to obtain the equivalent. It will in most cases be optimized away by the compiler (`&*(array+5)` will be converted to `array+5`) by the compiler yielding a somehow expected undefined behavior, but it is still undefined behavior. For another example, if the array contained an user defined type `T`, instead of `int`s, it could well overload `T::operator&` over a region of memory that is not of a `T`. The standard does not make exceptions here, and it is UB. – David Rodríguez - dribeas May 11 '11 at 18:23
  • @David: The last point regarding user-defined type and overloaded `T::operator &` is good. – Nawaz May 11 '11 at 18:27
  • @David (and others claiming UB): At least according to the people actually writing the standard, they do not feel that this is definitely UB (http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#232). The important line being: "We agreed that the approach in the standard seems okay: p = 0; *p; is not inherently an error." – Richard Corden May 11 '11 at 18:32
  • I think when a bunch of high-rep SO users reading the standard and ongoing debates in the std committee can't agree whether a certain technique is actually valid, there's a good chance the code would be better off not using said technique. (Even if it should be safe, isn't it likely some compiler writers got that wrong?) – sbi May 11 '11 at 18:35
  • 1
    Oh, and here's a shameless plug: Use [these](http://stackoverflow.com/questions/2552839/2553081#2553081). They are doing it right for sure, and you can forget about the issue once and for all. (I do believe C++11 will have `std::begin()`/`std::end()` out of the box, BTW.) – sbi May 11 '11 at 18:36
  • 3
    @Konrad, @Nawaz, @AAT, @David, @sbi: To formalise this discussion, I've created a question on this topic: http://stackoverflow.com/questions/5975586/is-arrayarray-length-valid-c-or-c. Oh, no! Delete! That's a duplicate of http://stackoverflow.com/questions/988158, which appears to suggest that it *is* valid. – Oliver Charlesworth May 12 '11 at 08:53
  • @Oli - thanks for finding that other question, let's all go there and read it instead of extending the discussion here! @Konrad, @Nawaz -- &array[5] is most certainly **not** something I would advocate doing, but I think it is clearly legal. (@sbi -- I pretty much agree with your take on it.) – AAT May 12 '11 at 12:15
1

This is how you would do it with C style arrays.

int i;
int count = 0;
for(i = 0; i < ARRAYSIZE; ++i)
{
    if(array[i] == click)
        ++count;
}

ARRAYSIZE is the size of your statically allocated array, array your array variable and click the value you are looking for. In count the count of the variable is saved.

Constantinius
  • 34,183
  • 8
  • 77
  • 85
1

You could use count.

Something like this (sorry I'm out of practice with C++):

#include <algorithm>

void someFunction() {
    int input[5];
    // initialize input with some values
    int num = std::count(&input[0], &input[5], 4);
}
Brendan Long
  • 53,280
  • 21
  • 146
  • 188
0

If it isn't sorted, then linear search is your only choice.

atoMerz
  • 7,534
  • 16
  • 61
  • 101