3

I am trying to write a program which checks if all the values in an array are equal using a for loop but I cannot figure out a way for the if statement to check if each value in the array are equal other than constantly repeating "if a[i] == a[1] && a[i] == a[0]" and so on. I do not want to do that as i want it to work for any array of any size.

for (unsigned i = 0; i < val; i++){
    if (a[i] == a[0])
        return true;
    else
        return false;
}
Jan Schultke
  • 17,446
  • 6
  • 47
  • 96
Beezy
  • 121
  • 2
  • 5
  • 15
  • Assuming this is a function, this will return on the first cycle. It doesn't check for all the other items in the array – Astinog Sep 04 '15 at 06:04
  • 1
    I would suggest to check this LINK. http://stackoverflow.com/questions/14120346/c-fastest-method-to-check-if-all-array-elements-are-equal Good Luck. – RIO Sep 04 '15 at 06:08
  • Equality is transitive. If a == b and b == c then a == c. Your relation doesn't satisfy that then it's not a true equality relation. – David Heffernan Sep 04 '15 at 06:11
  • It's a possible duplicate of http://stackoverflow.com/questions/15531258/test-if-all-elements-of-a-vector-are-equal . Just use pointers instead of interators. – pzelasko Sep 04 '15 at 06:12
  • @DavidHeffernan, what if this is, for example, a floating-point epsilon-equality, which is not transitive? What then? :) – Don Reba Sep 04 '15 at 11:00
  • @DonReba That's not a property equality then. The issue with floating point is with nan for which nan != nan. Exclude nan though and floating point equality is a true equivalence relation. – David Heffernan Sep 04 '15 at 11:06

5 Answers5

12
for (unsigned i = 0; i < val; i++) {
    if (a[i] != a[0]) {
        return false;
    }
}
return true;

That ought to do it.

In this case, the code will instantly fail on a non-matching value. However, on a matching value it simply continues checking (as we know we need to test EVERY element of the array no matter what). Once that's done, it knows everything went well (since we didn't return early) and returns true.

Jonathan Potter
  • 36,172
  • 4
  • 64
  • 79
CollinD
  • 7,304
  • 2
  • 22
  • 45
1
#include <algorithm>
#include <vector>
#include <iostream>

int main(int argc, char** argv)
{
    std::vector<int> eq{ 1, 1, 1, 1 };
    std::vector<int> nq{ 1, 2, 1, 1 };

    bool eq_res = std::all_of(std::begin(eq), std::end(eq),
        [&eq](int c) -> bool
    {
        return eq[0] == c;
    });
    bool nq_res = std::all_of(std::begin(nq), std::end(nq),
        [&nq](int c) -> bool
    {
        return nq[0] == c;
    });

    std::cout << "eq: " << eq_res << std::endl;
    std::cout << "nq: " << nq_res << std::endl;
}

Compiled with g++ --std=c++11 main.cpp

schorsch_76
  • 794
  • 5
  • 19
1

Just for fun, using lambda expression

#include <algorithm>
using namespace std;

template<size_t N>
bool func(int (&arr)[N])
{
    int* pOddValue = std::find_if(begin(arr), end(arr), 
        [&] (int val){ return val != arr[0];});
    return pOddValue != end(arr);
}
Viet Nguyen
  • 406
  • 3
  • 12
0

Using divide and conquer approach, we can reduce the no of comparison to n-1 if n = 2^k like this:

bool divide(int arr[],int size)
{
    if( size == 2 ) return arr[0] == arr[1];

    if( divide(arr,size/2) && divide(arr+size/2,size/2) )
        return arr[0] == arr[size/2];

    return false;
}

Another similar way to do it:

for (unsigned i = 1; i < val; i++) {
    if (a[i] != a[i-1]) {
        return false;
    }
}
return true;
mchouhan_google
  • 1,775
  • 1
  • 20
  • 28
0

It seems that you don't need to handle val = 0. You can do it in 1 line.

#include <functional>
#include <algorithm>
using namespace std;
return all_of(
    a+1, a+val,
    bind(equal_to<remove_pointer<decltype(a)>::type>(), a[0], placeholders::_1));
johnjohnlys
  • 394
  • 1
  • 9