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 == ?
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 == ?
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
subtract the two values and if the result is 0 they are equal. Hello asm class
In glorious non-technicolor, howsabouthisthen
Spot the clever use of the "%sequal" format specifier (short for string equal) to do the actual workCode:#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; }
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
Neat Jimmy!
But I can't find the %sequal modifier in my manual
When all else fails, read the instructions.
If you're posting code, use code tags: [code] /* insert code here */ [/code]
>In glorious non-technicolor, howsabouthisthen
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.
My best code is written with the delete key.
Yeah, the %sequal one of the more powerful modifiers, but keep it secret or it loses its power
Help populate a c/c++ help irc channel
server: irc://irc.efnet.net
channel: #c
Originally Posted by Salem
But run it with:
I got this output:Code:char t1[] = "SsSs1234"; char t2[] = "ssXsssss";
All chars in SsSs1234 are NOT equal
All chars in ssXsssss are equal
Dave
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) )
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
So I was right, it is broken?
Try:
#define C1(a,i) ( (!a[i] || !a[i+1]) || ( !(a[i]^a[i+1]) && ++i ) )
Code:#include <cmath> #include <complex> bool euler_flip(bool value) { return std::pow ( std::complex<float>(std::exp(1.0)), std::complex<float>(0, 1) * std::complex<float>(std::atan(1.0) *(1 << (value + 2))) ).real() < 0; }
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.
Thanx for all your helps.
If pointers have made you suicidal, you're not alone. But there is no need to hurt yourself. There are people who are willing to help. Just call your local C Crisis Centre and talk to the professtionals there. If you don't have a C3 in your neighborhood, go to the global C3 at www.cprogramming.com . If you're still suicidal, don't lose hope. Gently close your book on C and throw it in the fireplace. If you live in a tower, you can throw it out the window. There, doesn't that feel better?
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; }
>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.
My best code is written with the delete key.
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
>beat this
What? No spaces? Slacker.
My best code is written with the delete key.