I'm trying to compute the abstraction of the following C code fragment, with the predicate: b: { x >= 0 }
1. if( x > 5 )
2. x = x - 2;
3. else
4. x = abs( x ) + 6;
5. assert( x >= 0 );
so far I abstracted:
1. if( * ) // not sure if I should put if( b ) here
2. assume( b ); b = true;
3. else
4. assume( true ); // ? don't know how to abstract further
5. assert( b )
Any ideas how to do this ?
I dont know whether I am understanding you correct or not, but for the set of input predicate {x>=0} or b (used alternatively).It should be:-
{x>=0}=unknown() //unknown function is used to generate true or false non-deterministically
if(*)
{
assume({x>=0});
{x>=0}=true;
}
else
{
assume(!{x>=0});
{x>=0}=false;
}
Related
I should write a C while statement that continues while x is both larger than 10 and a multiple of either 6 or 7. I only need to include the actual while statement, not the entire loop!
I know it may look funny to you, but this is what I have so far.
#include<stdio.h>
int main(){
int x;
while (x > 10, x % 7 = 0 || x % 6 = 0) {
printf("%d\n", x);
}
}
Logical and is &&. As an operator, , is "ignore the result of the thing to the left".
Your Boolean logic and comparison both use the wrong syntax. It should be like this:
while (x > 10 && (x % 7 == 0 || x % 6 == 0)) {
The comma (,) is not a valid Boolean operator. What you wanted is && (logical AND). You need parentheses around the other two expressions because AND (&&) has higher precedence than OR (||).
And = is for setting values; == is for comparing them.
Finally, as others pointed out, you don't set x anywhere in the code that you have posted. So, your loop will not run. And you don't modify x in the loop, so, if you ever get into the loop, you will never get out.
The program can look like
#include <stdio.h>
int main( void )
{
int x;
printf( "Enter an integer number: " );
scanf( "%d", &x );
while ( x > 10 && ( x % 7 == 0 || x % 6 == 0 ) )
{
printf( "%d\n", x );
printf( "Enter next integer number: " );
scanf( "%d", &x );
}
}
Or you could enlarge the condition in the while loop the following way (to check that the input is correct)
#include <stdio.h>
int main( void )
{
int x;
printf( "Enter an integer number: " );
while ( scanf( "%d", &x ) == 1 && x > 10 && ( x % 7 == 0 || x % 6 == 0 ) )
{
printf( "%d\n", x );
printf( "Enter next integer number: " );
}
}
As for your condition in the while loop then it has two errors
while (x > 10, x % 7 = 0 || x % 6 = 0)
It uses the comma operator instead of logical operator && and it uses the assignment operator as for example x % 7 = 0 instead of the comparison operator x % 7 == 0. Also variable x was not initialized.
Your while statement should read
while (x > 10 && (x % 7 == 0 || x % 6 == 0))
Note the use of && for "boolean and", and == for "equality comparison".
I should Write a C while statement that continues while x is both
larger than 10 and a multiple of either 6 or 7. I Only need to include
the actual while statement, not the entire loop!
Let's analyze the condition that will go in the while() statement:
x is both larger than 10 and a multiple of either 6 or 7
Can be expressed as
x is greater than 10 AND a multiple of 6 OR multiple of 7
So let's code that in C:
x is greater than 10
x>10
AND
&&
x multiple of 6 OR x multiple of 7
(x%6)==0 || (x%7)==0
Then, all together
x>10 && ( (x%6)==0 || (x%7)==0 )
So your while sentence is like this:
while (x>10 && ( (x%6)==0 || (x%7)==0 ))
{
stuff...
}
Here can be the Solution Ava
while (x > 10 && (x % 7 == 0 || x % 6 == 0)) {
I'm trying to convert following C code snippets to assembly, but the problem is I can't even understand these simple C code. They are written in abnormal way I think. so I just can't transfer to assembly. please help me
a = (a >= c);
b = (c < d) || (b > d);
a = (a != d) && (b != c)
Look if a>=c then it return true. So a=1 otherwise a=0.
Now in second case if c<d(c is less than d ) or b>d(b is greater than d) any one of them is true b=1. If both are false b=0.
If a is not equal to d and b is not equal to c then a=1 otherwise it is false(0).
A && B = 1 if A!=0 and B!=0
= 0 if A=0 or B=0 or (A=0 and B=0)
A || B = 1 if A!=0 or B!=0
= 0 if A=0 and B=0
Note: if (A && B) and A is found to be 0 then B will not be checked.
if (A || B) and A is found to be 1 then B will not be checked.
if B is an expression then that will not be executed as per the information stated above. This is called Short circuit evaluation.
Statements like a>=c return true ( 1 ) or false ( 0 ).
So in your case, a=(a>=c); will assign the value 1 to a if a >= c, otherwise, it will assign 0 to a.
This can also be read as
if( a >= c )
a = 1;
else
a = 0;
Next is b=(c<d)||(b>d);. Since we have ||, if any one of the two conditions return true then b is assigned 1, otherwise it is assigned 0. That is if ( c < d ) or ( b > d ), b will be assigned the value 1, and if both of the conditions return false, then b is assigned 0 .
Now this is similar to the if statement
if( ( c < d ) || ( b > d ) )
b = 1;
else
b = 0;
And for a=(a!=d)&&(b!=c), both the conditions have to return true for a to get the value 1. That is, both ( a != d ) and ( b != c ) must return true for a to be assigned the value 1. If anyone of those two conditions is not satisfied, then a is assigned the value 0.
This is equivalent to
if ( ( a != d ) && ( b != c ) )
a = 1;
else
a = 0;
Hope you understand it now. :)
They are boolean expressions and return a value of 1 when true.
(a >= c) returns 1 if a >= c, and 0 if not.
Also a && b will return 1 if both a and b are non-zero
Can someone explain why the output of this program is false??
x && y gives 1. Still the output is false.
#include <stdio.h>
int main()
{
int x = 1, y = 2;
if(x && y == 1)
{
printf("true.");
}
else
{
printf("false.");
}
return 0;
}
Because == has a higher precedence than && So first this get's evaluated:
x && (y == 1)
y == 1 // 2 == 1
//Result: false
Which is false and then second:
x && false //1 && false
//Result: false
So the if statement will be false
For more information about operator precedence see here: http://en.cppreference.com/w/cpp/language/operator_precedence
if(x && y == 1)
Is the same as
if( ( x != 0 ) && ( y == 1 ) )
Here,x != 0 is true, but y == 1 is false. And since at least one of the operands of && is false, the condition evaluates to false and the else part executes.
It clearly stated X = 1 & Y = 2;
Now with your expression
X && Y == 1
The expression is evaluated as
Y == 1 (Precedence Rule, Also output is False)
X != 0 (Its True)
Now && is Logical And Operator, so it evaluates to True only if both the parts in expression evaluates to True!!!
It's okay to false, then 2 and 2 and it is different from one.
What you're asking is whether both x and y both are worth 1. If this happens say true but false
Ok so relatively prime means that two numbers have no common factors greater than 1. It can also be viewed as two numbers that have gcd = 1.
So along those lines, this is the code i wrote to find two relatively prime numbers e,z :
for(e = 0,flag=0; (flag==1); e++){
if( gcd( e, z ) == 1){ // z in this example is 60
flag = 1;
}
}
printf("e = %d\n",e);
and the gcd function is defined as :
int gcd(int i, int x){
if(x % i == 0) return( i );
return( gcd( x % i, x ) );
}
when I set z = 60, the e I get is e= 0 ... Actually I keep getting the same e with which I initialize the for loop
What am I doing wrong? Is there any other way of finding if two numbers are relatively prime?
EDIT:
Ok as per the suggestion from minitech here is the modified code:
for(e = 2,flag=0; !flag; e++){
if( gcd( e, z ) == 1){
flag = 1;
}
}
now when I set my z=60 , my e is coming out to be e = 60 which is again wrong. Correct answer should be e = 7
You shouldn’t start at zero
Your flag condition should be !flag
After fixing that, you will always get 1, because it’s relatively prime to everything. Try starting at z - 1 and decrementing if you want the biggest one. You should also just break; instead of keeping a flag.
This is a little fragile, since it can't handle a zero argument; e.g.,
gcd(z, z) = gcd(z, 0) = gcd(0, z) = |z|.
I'd go with something like:
unsigned gcd (unsigned u, unsigned v)
{
unsigned t;
for (; (t = v) != 0; u = t)
v = u % v;
return u;
}
I use unsigned types, because there's no reason to use negative arguments - they don't affect the result of a gcd, which is always non-negative.
X, Y, Z, T are different jobs. Ex, X = Multiplexer( ... )
if ( empty1 )
if ( empty2 )
if ( empty3 )
if ( empty4 )
// Do nothing
else
X
else
Y
else
Z
else
T
EDIT :
emptyA is a number 1 or 0, and A is member of the set { 1, 2, 3, 4 }
How can I rewrite that program segment so as to get minimum comparison cost
You have Verilog/HDL tags so assuming this is for synthesizable logic:
//This may be more readable
wire [3:0] empties = {empty4,empty3,empty2,empty1};
casex (empties)
4'xxx0: blah = T;
4'xx01: blah = Z;
4'x011: blah = Y;
4'0111: blah = X;
default: blah = something_else; //Shouldn't do nothing
endcase
The logic cost is going to depend on other factors than just this code.
if(!empty1)
T
else if (!empty2)
Z
else if (!empty3)
Y
else if (!empty4)
X
You can't reduce the comparisions as you have an action that is executed if the return value is false for all cases.