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.
Related
I have an external Numpad that has numbers from 0-9 and letters from A-D.
I want to store numbers from 0-9 in x and y as coordinates. When I press A(which equ. 10) I want to draw a point.
The problem is I can not figure out how to store first key in x and second key in y. As soon as I press a key it assigns both x and y the same numbers.
I have tried many different if-statements but it always assigns both x and y same number.
int counter=0;
numPad[16]={1,4,7,14,2,5,8,0,3,6,9,15,10,11,12,13};
if(numPad[key]<10){
x=numPad[key];
x*=16;
counter++;
}
if(counter>0 && numPad[key]<10){
y=numPad[key];
y*=8;
}
if(numPad[key]==10){
LCD_DrawPoint(x,y,WHITE);
counter=0;
}
Your if-statements for setting x and y, and drawing the point are mutually exclusive. if(numPad[key] < 10) and if(numPad[key] == 10) cannot be true at the same time. I'm not sure why you're making that comparison, but I think it is to cap the max possible input value at 9 or something. In the example below, we cap the max possible value of numPad[key] at 9, assign it to X and Y, then draw the point. The counter is useless here, but I kept it in because you had it in your original code.
int counter = 0;
numPad[16] = { 1,4,7,14,2,5,8,0,3,6,9,15,10,11,12,13 };
int someValue = numPad[key];
if(someValue > 9) {
someValue = 9;
}
x = someValue * 16;
y = someValue * 8;
LCD_DrawPoint(x,y,WHITE);
counter=0;
You could provide a minimum verifiable example next time as this will allow us to help you better.
By not providing sufficient code, those who might try to help can/will misunderstand and not "fill in the blanks" appropriately. Below is my interpretation of your question.
int x = 0, y = 0, counter = 0; // is all this contained inside one function?
int numPad[16] = { // layout increases reader comprehension
1, 4, 7, 14,
2, 5, 8, 0,
3, 6, 9, 15,
10, 11, 12, 13
};
for( ;; ) {
int key = scan_keyboard() // Not provided by OP
int value = numPad[ key ]; // translate
if( value < 10 ) {
// alternately assign scaled low values to x or y
if( !counter )
x = 16 * value;
else
y = 8 * value;
counter = !counter;
}
else if( value == 10 ) {
LCD_DrawPoint( x, y, WHITE );
x = y = counter = 0; // reset (excessive caution)
}
}
Notice the use of if/else.
#include <stdio.h>
int main()
{
signed int x;
int x1 = 0, x2 = 10, final, loop = 1, y = 10, c;
printf("Enter the value of X.\n");
scanf("%d", &x);
printf("Value Scanned:%d\n", x);
again:
if (loop <= 32)
{
if (x >= x1 && x < x2)
{
final = x - x1;
printf("%d", final);
y = y * 10;
x1 = 0;
x2 = 0;
++loop;
goto again;
}
else
{
c = x2 - x1;
if (x1 == x2)
{
x2 += y;
goto again;
}
else if (c == y)
{
x1 += y;
x2 += y;
goto again;
}
else
{
printf("Error in Process");
goto ending;
}
}
}
else
{
printf("0+error, extra long input");
}
ending:
return 0;
}
Flowchart:
I am a beginner in C-language and only know how to use If-else, Switch, Goto statements, with basic knowledge of how to integrate basic level loops. So please tell me what/where I am wrong instead of telling me how to use arrays because I don't know them, etc. This is my most complex code until now.
Now for Explanation of Coding,
I wrote X1 as the lower value and X2 as the upper value while first keeping a difference = Y(initially 10) between them.
Continuously increasing the value of X1 and X2 by Y(10) together simultaneously, I will arrive in between an intersection where my x(input) lies.
Eg-
x=568
then X1 and X2 will keep on increasing until they reach X1 = 560 and X2 = 570, then they will do Final = X(568) - X1(560) and print it.
since it can only happen for 32-digits long, so I wrote loop = 0 and only processing my main statement till loop is smaller than or equal to 32, otherwise printing "0+error".
then I put Y = Y * 10 every time the value was within my specified range.
It should give me the values like Last digit, then last 2 digits, then last 3 digits,etc. but after scanning the value, it isn't exciting at all.
Evaluating what you are attempting to do, I reworked your code to make it a bit more structured without utilizing arrays at that seemed to be something you wanted to avoid at this time. However, these days, the use of the goto statement is usually avoided since functionality such as for loops, do/while loops, and while loops offer better clarity to coding. With that in mind, following is a snippet of code that provides the functionality it looks like you want.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int x, x1, x2, y = 10, counter = 0, last_digit;
printf("Please enter a number: ");
scanf("%d", &x);
if (x < 0) /* Just in case a negative integer is entered */
{
x = x * -1;
}
while (1) /* Use a while loop with associated break statements to avoid goto and label statements */
{
x1 = 0;
x2 = 10;
counter += 1;
while (1)
{
if (x >= x1 && x <= x2)
{
last_digit = x - x1;
if (counter == 1)
{
printf("The last digit is: %d\n", last_digit);
}
else
{
printf("The next digit is: %d\n", last_digit);
}
break;
}
x1 += y;
x2 += y;
}
x = x / 10; /* Perform integer division by ten to get to the next digit in the entered number */
if (x == 0) /* Once all digits have been processed the outer while loop can be exited */
{
break;
}
}
return 0;
}
Following are some key points.
As noted, the loop process using goto statements is replaced by two while loops; one while loop nested inside another while loop.
Utilizing integer division by ten, each digit can be ascertained and printed.
Utilizing the nested while loops with break statements allows for a more compact program.
Utilizing this code snippet, following is a sample test from the terminal.
#Dev:~/C_Programs/Console/LastDigit/bin/Release$ ./LastDigit
Please enter a number: 479824385
The last digit is: 5
The next digit is: 8
The next digit is: 3
The next digit is: 4
The next digit is: 2
The next digit is: 8
The next digit is: 9
The next digit is: 7
The next digit is: 4
Back in the day, the goto statement had its place in coding, but today it is pretty much an artifact.
Give that a try and see if it meets the spirit of your project.
I am desperately looking for a way to easily calculate the number of all possible execution paths in a C function.
For example, for the following function I would expect to get a result of 3 (if there is a chance based on the values that 'i' gets to enter any of the 'if' statements)
void test(void)
{
if (i>0)
x = x + 1;
else if (i>10)
x = x + 2;
else
x = x + 3;
}
Use comma operator as
int test(void)
{
int ways = 0;
if (++ways, i>0)
x = x + 1;
else if (++ways, i>10)
x = x + 2;
else
{
x = x + 3;
++ways;
}
return ways;
}
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.
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;
}