I wanna compare 3 images at 3 different buttons, Xcode is allowing me to compare 2 images at a time but when i write code for 3 it gives a warning
"comparison between pointer and integer"
-(void)compare
{
if (b1.currentImage==b2.currentImage==b3.currentImage)
{
b1.enabled=NO;
b2.enabled=NO;
b3.enabled=NO;
NSLog(#"%#",b1.currentImage);
NSLog(#"%#",b2.currentImage);
}
else
{
UIImage *btnImage = [UIImage imageNamed:#"card.png"];
[b1 setImage:btnImage forState:UIControlStateNormal];
}
}
Generally spoken when you write :
a == b == c
that actually boils down to
(a == b) == c
thus you are comparing the result of the comparision of a and b (which is of type bool regardless of the types of a and b) to c which is of whatever the type of c is.
So in your case you compare b1.currentImage==b2.currentImage (which is of type int) to b3.currentImage which is a pointer type.
If yo want to compare three values for equality you have to write:
(a == b) && (a == c) instead of a == b == c
Try this small program and watch what it prints out:
int main()
{
int a = 2, b = 2 , c = 2 ;
if ((a == b) && (a == c ))
{
printf ("(a == b) && (a == c ) is true") ;
}
if ((a == b == c))
{
printf ("(a == b == c) is true") ;
}
return 0 ;
}
it will print
(a == b) && (a == c ) is true
because (a == b == c) is evaluated like this
1. (a == b) == c
2. (2 == 2) == 2
3. 1 == 2
4. false
Related
I have written a stack based interpreted language in C. The interpreter works with reading a file line by line and executing the line depending on the operation. print(1 + 1) in python would become 1 1 + print.
Here is the function to check what the operation is and push it to the stack or do an operation on it:
if (strncmp(op, "+", 1) == 0 && is_definition == 0)
{
float a = pop(stack);
float b = pop(stack);
push(stack, a + b);
}
else if (strncmp(op, "-", 1) == 0 && is_definition == 0)
{
float a = pop(stack);
float b = pop(stack);
push(stack, b - a);
}
else if (strncmp(op, "*", 1) == 0 && is_definition == 0)
{
float a = pop(stack);
float b = pop(stack);
push(stack, a * b);
}
else if (strncmp(op, "/", 1) == 0 && is_definition == 0)
{
float a = pop(stack);
float b = pop(stack);
push(stack, b / a);
}
else if (strncmp(op, "print", 5) == 0 && is_definition == 0)
{
float a = pop(stack);
if(a == (int)a)
printf("%d\n", (int)a);
else
printf("%f\n", a);
}
else if (strncmp(op, "define", 6) == 0 && is_definition == 0)
{
is_definition = 1;
}
else if (is_definition == 1)
{
}
else if (strncmp(op, "end", 3) == 0 && is_definition == 1)
{
is_definition = 0;
}
else
{
push(stack, atoi(op));
}
This is inside a loop that iterates over every space separated operation in the code.
I want to add a definition system a bit like the one in C.
This is the syntax I would like to have
define TEST 10 end
I would like to use this a bit like a variable system where you can use TEST.
In pseudo code, you should do the following:
Read a line of source
If it is a definition, parse+store it and skip the rest
(it is not a definition) execute, much like the code you posted
About "parse+store" the definitions, you need - for example - a couple of arrays, or an array of structs. You need to store each "name" (the alias, or the name of the definition) and, along with each name, its value.
Then, in the code you posted, you should implement the push() instruction (you only mention pop()). The push() instruction reads an operand and determines if it is an alias (definition) or not:
(push pseudo code)
Get the operand
Determine if it is a definition. Basically, you iterate on all the stored definitions to find a correspondence
Got the final value, put it on the stack
There are several things that could be said... a couple of them, in sparse order:
The pushed operand is a number? In this case you can skip the definition(s) checking, assuming that it is illegal to say "define 10 20"
Would you allow to (re)define operators?
Would you allow a definition to refer to other definitions?
...
So to change my question. It refuses to recognize that there are four twos in a row. It recognizes that there are four ones in a row but that happens after the four twos. Why is this happening?
0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 1 1 1 1 0 0
1 2 2 2 2 0 0
int checkFour(int a, int b, int c, int d){
if (a == b == c == d){
return 1;
}
else{
return 0;
}
return 0;
}
//check for the horizontal win
int checkHorizontal(){
for(int i=0; i < rows; i++){
for(int j=0; j < column - 3; j++){
if ((board[i][j] != 0) && (board[i][j+1] != 0) && (board[i][j+2]!= 0) && (board[i][j+3] != 0)){
if (checkFour(board[i][j],board[i][j+1],board[i][j+2],board[i][j+3]) == 1){
printf("Game Over\n");
exit(0);
}
}
}
}
}
What am I doing wrong?
if (a == b == c == d){ does not work the way you might think. The result of a comparison in C is a boolean value of 0 or 1. Given that == operator has left-to-right associativity, your statement can be re-written as:
if ((((a == b) == c) == d)
This appears to give correct results when they are all 1. This is because it ends up comparing the values (1) to the result of the comparison operation, also (1).
(((a == b) == c) == d) a == b -> 1
((1 == c) == d) 1 == c -> 1
(1 == d) 1 == d -> 1
The correct way is to use logical AND.
if (a == b && a == c && a == d)
All three comparisons need to evaluate to true for the entire statement to be true.
Note that there are other combinations that work. Ex:
if (a == b && b == c && c == d)
By the way, you can shorten the entire function to
int checkFour(int a, int b, int c, int d){
return a == b && b == c && c == d;
}
The problem is that you misunderstood the mechanism of C. the code if (a == b == c == d) wouldn't take if abcd are all values equal then return 1. because C computes from left to right(same priority), so it would compute a == b first, the result is 1 or 0, then take this result to compare with c, the second result also is 1 or 0, finally take second result to compare with d and the final result is come out.
The right code is like this:
if ((a == b) && (d == c) && (b == c))
return 1;
else
return 0;
Why does this following program print "Yes" instead of "No"?
None of the variables is initialized to 2.
bool hello = 0;
int a = 1;
int b = 3;
int c = 4;
int d = 5;
if (a || b || c || d == 2) {
hello = 1;
}
if (hello == 1) {
printf("Yes");
}
if (hello == 0) {
printf("No");
}
return 0;
}
The statement
if (a || b || c || d == 2)
is equivalent to:
if (a != 0 || b != 0 || c != 0 || d == 2)
The equality comparison does not automatically distribute across all the variables. If you want to do that, you need to perform all the comparisons explicitly:
if (a == 2 || b == 2 || c ==2 || d == 2)
The expression (a || b || c || d == 2) evalutates to true because it treats a, b, c as booleans, and any non-zero integer is true.
You have given logical operator in the expression It means that if non zero value came then the expression is true. Then hello=1 is set and in next f statement it prints YES
You just meet the short circuit behavior of logical expressions OR.
The order of evaluation of logical OR || is left to right.
So in the following expression:
left || right
if left = true then right will never going to be executed (short circuit). In your code exactly same happened.
As you know, any non zero value treated as true in C, hence, a which is 1 is true. So, take a look:
if (a || b || c || d == 2)
if (true || bla bla bla) //rights are not even checked!
if (true)
hello = 1;
Tada! So the program print "Yes"!
None of the variables is initialized to 2.
Yes of course! But your if condition is not going to check that. To do so, try this:
if (a == 2 || b == 2 || c ==2 || d == 2) {
//...
because if judge num is not zero , if think this is true. so your code
if (a || b || c || d == 2)
like
if ( true || true || true || false)
the result is true, programe print "YES"
I was wondering if we could get rid of all the "if" statements only by using boolean logic.
int main() {
int a,b,c,d;
char e;
scanf("%d %d %d", &a, &b, &c);
scanf("%d", &d);
if (d == 0)
{
e = 'O'*((a+b == c) || (a+c == b) || (b+c == a));
e += (e == 0)*'X';
printf("%c\n",e);
}
if (d == 1)
{
e = 'O'*((a*b == c) || (a*c == b) || (b*c == a));
e += (e == 0)*'X';
printf("%c\n",e);
}
}
So far I've been able to replace
if ((a+b == c) || (a+c == b) || (b+c == a))
{
e = '0';
}
else
{
e = 'X';
}
by
e = 'O'*((a+b == c) || (a+c == b) || (b+c == a));
e += (e == 0)*'X';
is there any way to get rid of the lines
if (d == 0)
and
if (d == 1)
using the same logic?
As you wish, no if-statement left:
!d && (
(e = 'O'*((a+b == c) || (a+c == b) || (b+c == a))),
(e += (e == 0)*'X'),
printf("%c\n",e)
);
d-1 || (
(e = 'O'*((a*b == c) || (a*c == b) || (b*c == a))),
(e += (e == 0)*'X'),
printf("%c\n",e)
);
I abused short-circuiting of ||, && and the comma-operator ,.
Anyway, if you want to see the masters in obfuscation, look at
The International Obfuscated C Code Contest .
Speaking about the motivation of this, it is a good thing if you are concerned about efficiency, as conditional branches are very time-consuming (this is the reason there are some complicated mechanisms for branch prediction). But it is not a good practice in usual code, as it might be very hard to understand for your reader, so your code becomes very hard to maintain. Also, as you are a beginner, it is a very good exercise.
Now, keep in mind that there is always a way. It is worth mentioning that you have a combination of logical, bitwise and arithmetic operation. It could be done purely with bitwise operations.
Let's try to make it using just bitwise operations. Assume your code is:
if (d == 0)
e = A;
if (d == 1)
e = B;
, where A and B are those 2 values you compute for e.
Firstly, extend the last significant bit to all of the d's bits (so if d is 1, it should be 0xFFFFFFFF and if it is 0, it should be 0x00000000). Then, do the operation. I splitted them into multiple lines, but it could be done more compact.
d = d << 1 + d;
d = d << 2 + d;
d = d << 4 + d;
d = d << 8 + d;
d = d << 16 + d;
e = (B & d) || (A & ~d);
Note that here I assume an int is 32 bits, which is not very portable. But, it is just an exercise.
I have a problem in C.
#include<stdio.h>
int main()
{
int a = 10, b = 0, c = 7;
if (a ? b : c == 0)
printf("1");
else if (c = c || a && b)
printf("2");
return 0;
}
This code prints 2 but I think a?b:c returns b=0 and 0==0 returns 1. Can you explain the code?
The conditional operator (?:) has one of the lowest precedences. In particular it is lower than ==. Your statement means this:
if(a ? b : (c == 0)) { ... }
Not this:
if((a ? b : c) == 0) { ... }
Your conditions are not properly written.
In the first if-statement:
if (a ? b : c == 0)
if you put the values, then it becomes
if(10 ? 0 : 7 == 0)
means, it will always return 0.
That's why control goes to the else part and there, it becomes
else if (7 = 7 || 10 && 0)
since you used the "=" operator here (c = c), it will be always true, therefore it prints "2".
Now you want that code should return "1", then change your if statement in this way.
if( (a ? b:c) == 0){...}
because "==" operator has higher precedence than ternary operator.