# (Im)possible Algorithm

This is a discussion on (Im)possible Algorithm within the C Programming forums, part of the General Programming Boards category; Heh, I've just "improved" it by also removing all == as well as all loops I wonder how many know ...

1. Heh, I've just "improved" it by also removing all == as well as all loops
I wonder how many know how to compare for equality without using == ?

2. subtract the two values and if the result is 0 they are equal. Hello asm class

Code:
```#include <stdio.h>

#define C1(a,i)   ( !a[i] || ( !(a[i]^a[i+1]) && ++i ) )
#define C5(a,i)   ( C1(a,i) || C1(a,i) || C1(a,i) || C1(a,i) || C1(a,i) )
#define C25(a,i)  ( C5(a,i) || C5(a,i) || C5(a,i) || C5(a,i) || C5(a,i) )
#define C(a,i)    ( C25(a,i) )

int main()
{
int     i;
char    t1[] = "SsSs1234";
char    t2[] = "ssssssssss";

i=0;printf("All chars in %s are %sequal\n", t1, C(t1,i) ? "" : "NOT " );
i=0;printf("All chars in %s are %sequal\n", t2, C(t2,i) ? "" : "NOT " );
return 0;
}```
Spot the clever use of the "%sequal" format specifier (short for string equal) to do the actual work

4. Neat Jimmy!

But I can't find the %sequal modifier in my manual

It's quite beautiful in a Lon Chaney, Phantom of the Opera, hideously deformed but still brilliant underneath, way. That was close to what I was thinking of, along with the unrolled loop nasty.

>But I can't find the %sequal modifier in my manual
It's an undocumented modifier. Manuals don't show you the most powerful features.

6. Yeah, the %sequal one of the more powerful modifiers, but keep it secret or it loses its power

7. Originally Posted by Salem

Code:
```#include <stdio.h>

#define C1(a,i)   ( !a[i] || ( !(a[i]^a[i+1]) && ++i ) )
#define C5(a,i)   ( C1(a,i) || C1(a,i) || C1(a,i) || C1(a,i) || C1(a,i) )
#define C25(a,i)  ( C5(a,i) || C5(a,i) || C5(a,i) || C5(a,i) || C5(a,i) )
#define C(a,i)    ( C25(a,i) )

int main()
{
int     i;
char    t1[] = "SsSs1234";
char    t2[] = "ssssssssss";

i=0;printf("All chars in %s are %sequal\n", t1, C(t1,i) ? "" : "NOT " );
i=0;printf("All chars in %s are %sequal\n", t2, C(t2,i) ? "" : "NOT " );
return 0;
}```
Spot the clever use of the "%sequal" format specifier (short for string equal) to do the actual work

But run it with:
Code:
```    char    t1[] = "SsSs1234";
char    t2[] = "ssXsssss";```
I got this output:
All chars in SsSs1234 are NOT equal
All chars in ssXsssss are equal

Dave

8. Bleh!!!!
Does this make it better?
Code:
```#define C5(a,i)   ( C1(a,i) && C1(a,i) && C1(a,i) && C1(a,i) && C1(a,i) )
#define C25(a,i)  ( C5(a,i) && C5(a,i) && C5(a,i) && C5(a,i) && C5(a,i) )```

9. So I was right, it is broken?

Try:

#define C1(a,i) ( (!a[i] || !a[i+1]) || ( !(a[i]^a[i+1]) && ++i ) )

10. Thanx for all your responses. Sorry, I didn't check in earlier.
The programme was supposed to check if all characters in one string are the same, which I think, Salem's programme solved. Today, in class, we thought that if we get the first char of a string, and then concat it to the end of the string and drop the first one, it'd work:
"abbb" get the value of 'a'
concat a to the end of string (allocating enough space)
"abbba", then drop the first char
"bbba", then compare the strings. (we are allowed to used string functions provided).

It's gonna take me a while before I can figure out Salem's solution, but it seems very interesting.

11. Would it be cheating if I simply used a goto?

Code:
```#include <stdio.h>
#include <stdlib.h>

bool allSame( char* str )  {
int  i = 0;
char prev = str[i++] ;
label:
if ( str[i] && prev == str[i++] )
goto label;

return str[i] == 0;
}

int main(int argc, char *argv[])
{
char    t1[] = "ssSs1234";
char    t2[] = "ssssssssss";

printf("All chars in %s are %sequal\n", t1, allSame(t1) ? "" : "NOT " );
printf("All chars in %s are %sequal\n", t2, allSame(t2) ? "" : "NOT " );

getchar();
return 0;
}```

12. >Would it be cheating if I simply used a goto?
It depends on whether you see a goto loop as a real loop. I see it as a conditional repetition of a block of code, so goto would break the rules as that is pretty much the definition of a loop.

13. Prelude...All that work to figure out what your sig says. I hate you beat this

01010000011100100110010101101100011101010110010001 100101
01100010011001010110000101110100011101000110100001 10100101110011

0x48,0x65,0x78, 0x72, 0x6f, 0x63, 0x6b, 0x73, 0x62,0x6a, 0x6e, 0x73,
0x73, 0x6f, 0x63, 0x6b, 0x73

Just really bored actually

14. >beat this
What? No spaces? Slacker.

Page 2 of 2 First 12