Reputation: 411
Lets say I have an array short int check[10] = {1,1,1,1,1,1,1,1,1};
.
I want to check if all the element are the same.
I can't find the answer in stackoverflow
nor google
, but I've come across this code in C++
.
bool aresame(int a[], int n)
{
int i;
unordered_map<int, int> m;
for (i = 0; i < n; i++)
{
m[a[i]]++;
}
if (m.size() == 1)
{
return true;
}
else
{
return false;
}
}
Adjust a bit, and the result is huge error.
My attempt is usingif's
but that's very un-professional.
Might as well know, is there are any other ways to do it?
Upvotes: 4
Views: 2384
Reputation: 23792
if
is perfectly fine, there is nothing unprofessional about it.
I should note that in short int check[10] = {1,1,1,1,1,1,1,1,1};
only 9 elements are 1, the last element will be initialized to 0, so this check will always be false
, if you omit the size i.e. check[] = {1,1,1...
you won't have this problem because the size of the array will be deduced by the number of elements in the initializer.
#include <stdio.h>
#include <stdbool.h>
bool aresame(short int a[], size_t n) // added value to check
{
for (size_t i = 1; i < n; i++)
{
if(a[i] != a[0])
return false; // if a different value is found return false
}
return true; // if it reaches this line, all the values are the same
}
int main()
{
short int check[]={1,1,1,1,1,1,1,1,1};
printf("%s", aresame(check, sizeof check / sizeof *check) ? "true" : "false");
}
Upvotes: 3
Reputation: 28829
Just for completeness here's a recursive version (no explicit if
s) :
bool aresame(int a[],int n){
return (n <= 1) || (a[0] == a[n-1] && aresame(a, n-1));
}
Upvotes: 2
Reputation: 1420
As Gerhardh pointed out in the comments, there is nothing unprofessional about using if
. This code should work:
#include <stdbool.h>
bool are_same(int *arr, unsigned int len)
{
for (int i = 1; i < len; ++i)
if (arr[0] != arr[i])
return false;
return true;
}
You can call the function are_same
like this:
int arr[] = {1, 1, 1, 1, 1};
unsigned int len = sizeof(arr) / sizeof(int);
printf("The elements in the array are %s.\n",
are_same(arr, len) ? "all the same" : "not all the same");
Upvotes: 3
Reputation: 144550
Here is a quick and dirty if
-less implementation assuming two's complement without padding bits:
#include <stdbool.h>
#include <string.h>
bool are_same(const int *arr, size_t n) {
return n == 0 || !memcmp(arr, arr + 1, (n - 1) * sizeof(*arr));
}
You can generalize this method to check if the array contains a repeating sequence of length r
:
#include <stdbool.h>
#include <string.h>
bool is_repeating(const int *arr, size_t n, size_t r) {
return n <= r || !memcmp(arr, arr + r, (n - r) * sizeof(*arr));
}
Upvotes: 2
Reputation: 134
If you do not like if statements then try this:
bool aresame(int a[], int n) {
int i = 0;
while(i<n && a[i]==a[0])
i++;
return i == n;
}
No need to use extra local storage, just loop until you see an element that is not the same. If you reach the end, everything is fine. Otherwise not.
See here: https://godbolt.org/z/8r6YK6W34
Upvotes: 2