my nested if statement, need to make an uninterruptible condition - c

I've been trying to program an Radio Control car to follow a line depending on the readings of five line-following sensors.
The line-following sensors give 1 when they are on the line and 0 when they are not.
For example: If the sensors reads 00100, the car will go forward.
I faced a problem, when the sensors read 11111 (dead end).
The car should make a uturn. While it is doing the uturn the condition of the sensors change.
Say it becomes 11000.
That new value takes me to another if statement which is if 11000 turn left and the uturn order is lost.
Question: How can I make the if statement for uturn uninterruptible by other if statements when the condition changes?

Use a boolean.
if (sensors == "11111") makingUturn = true
then
if (sensors == "00100") makingUturn = false
Now for all other conditions, check if makingUturn is false before continuing with the code for that condition
Note
In case it wasn't obvious, this wasn't meant as actual code to compile and execute. What I mean by if (sensors == "ddddd"), is to check that all 5 sensors have the value at each position of the string. And when I say makingUturn = bool is to replace the bool with a 1 or 0 depending on if you want true or false

Related

Is it possible to have two statements in an if statement?

Hello I'm trying to code a tic-tac-toe game and wanted to create an AI that plays randomly, except when the human player is about to win. However, I've run into a small issue here. Suppose the index(?) of the 3x3 grid goes like this:
123
456
789
If a and b are filled by the opponent's shape, then I want to check if:
i) the two numbers are in a row/column/diagonal
ii) the winning position (that the AI will block) is empty or filled already
in ONE if statement.
I tried splitting these questions into two if statements, but because there are so many cases, if I only do one then I can only really check for one throughout. The 'or' action also doesn't work because only one statement is a boolean.
def preventXwin(start): #win conditions
#rows alone
if start[1] == start[2] == 'X' and checkfree(a) == True:
a = 3
print(start.update({a:'O'}))
return True
elif start[1] == start[3] == 'X' and checkfree(a) == True:
a = 2
print(start.update({a:'O'}))
return True
elif start[2] == start[3] == 'X' and checkfree(a) == True:
a = 1
print(start.update({a:'O'}))
return True
etc.
I want it to perform what the above would in theory, and I tried this but it didn't work.
If anyone has any solutions that would be incredibly helpful :)

Can any one explain why we need if (prime[2]!=1) to assign the values to the array while all the values of the array are initially set to be zero?

enter image description here
I have this code as an assignment where I should store values for each number from 0 to 100. Prime numbers are assigned the value 1 while non-prime are assigned the value 0. The code without the If statement and simply a return statement at the end makes more sense especially that the If condition is always true since all values of the array are initiated with zero, however, without the If, the code doesn't function properly. It usually returns 0 for all numbers. So the If condition here is crucial but can anybody explain why and what does it do here to make the code work?

Designing pseudocode for loop assignment

I'm in an intro to java class (first exposure to programming ever). My professor assigned this
"Input positive numbers and display how many are in between 10 and 20"
We've learned loops and if else statements so far and can use both, either or neither of those.
My pseudocode looks like this so far. Please let me know if I'm on the right track.
BEGIN
Input number from user
WHILE number is positive
IF (number >=10 && number >= 20)
Add 1 to count
END IF
Input another number if user wants
Display amount of numbers between 10 and 20
END WHILE
END
Am I on the right track? I've been wrestling with this for a couple hours
If you are trying to count numbers between 10 and 20 then.
This statement is wrong
IF (number >=10 && number >= 20)
Correct statement is
IF (number >=10 && number <= 20)
I would say you're on the right track for sure. Here are my few suggestions:
BEGIN / END may or may not be necessary. Honestly, I wouldn't use them. But some people do. Pseudocode is kind of - different for everybody.
Use variables consistently, and use assigment operations still in your code.
By this I mean, number = input from user instead of Input number from user or Input another number if user wants.
You should define counter = 0 before the while loop starts. Otherwise, you're incrementing a variable that isn't there.
IF / END IF might be a way to do it, but you could also use brackets or indentation to make the code more readable and mean the same thing.
Finally, about code correctness:
Did you want >= 10 and >= 20? You probably meant <= 20 because otherwise only values greater or equal to 20 would work. But aside from just the one sign, do you want them to be greater/less than or equal to the number? "In between" is vague, and might mean 11-19 or 10-20. It depends on how you frame the problem.
My final bit of input is, do you want to display the number every time the loop runs through? Or just after you enter a negative number and end the loop. If you want to display it only once, move it a line down. Otherwise, no big deal.
You're definitely on the right track and I wouldn't sweat it so much. Think of pseudocode as logical instructions. Flow through it very literally when you read it and when you write it, it's not about how it looks as to how it works.
I wish you a lot of luck, all the best :)
Pseudocode is always hard to correct but I think this would be much better
1) is positive is not a reasonable command, given that whether the input is numeric is unknown
2) initialize count to 0 before the while loop.
3) number <= 20 not number >= 20
4) Move display out of the while loop
5) Display count because your current statement is literally the problem
6) count += 1 is shorter and easier to read
7) we should get number in the while loop to ensure we stop when there is no input. something along the lines of while input number from user would stop in some languages once the input is blank. It will throw an error if the input cannot be coerced into a double. And it will stop when a negative is entered.
BEGIN
Initialize count = 0
WHILE number = input from user as double && number > 0
IF (number >=10 && number >= 20)
count += 1
END IF
END WHILE
Display count
END

Display data in Stata loop

I have a loop in Stata 12 that looks at each record in a file and if it finds a flag equal to 1 it generates five imputed values. My code looks like this:
forvalues i=1/5 {
gen y3`i' = y2
gen double h`i' = (uniform()*(1-a)+a) if flag==1
replace y3`i' = 1.6*(invibeta(7.2,2.6,h`i')/(1-invibeta(7.2,2.6,h`i')))^(1/1.7) if
flag==1
}
a is defined elsewhere. I want to check the individual imputations. Thus, I need to display the imputed variable preferably only for those cases where flag=1. I also would like to display another value, s, alongside. I need help in figuring out the syntax. I've tried every combination of quotes and subscripts that I can think of, but I keep getting error messages.
One other useful modification occurs to me. Suppose I have three concatenated files on which I want to perform this routine. Let them have a variable file equal to 1, 2 or 3. I'd like to set a separate seed for each and do it in my program so I have a record. I envision something like:
forvalues j=1/3 {
set seed=12345 if file=1
set seed=56789 if file=2
set seed=98765 if file=3
insert code above
}
Will this work?
No comment is possible on code you don't show, but the word "display" may be misleading you.
list y3`i' if flag == 1
or some variation may be what you seek. Note that display is geared to showing at most one line of output at a time.
P.S. As you are William Shakespeare, know that the mug http://www.stata.com/giftshop/much-ado-mug/ was inspired by your work.
A subsidiary question asks about choosing a different seed each time around a loop. That is easy:
forval j = 1/3 {
local seed : word `j' of 12345 56789 98765
set seed `seed'
...
}
or
tokenize 12345 56789 98765
forval j = 1/3 {
set seed ``j''
...
}

Optimization using look up table

I have made some c code for a program, which does some psycho-acoustics on sound data.
There is a piece of code which runs very slowly.
I think it would be best to use a look up table. How would one go about implementing it?
Any pointers or help would be appreciated! :)
Your values are not equidistant so it is not that easy. But its still possible: take your greatest common divisor of all your condition-values (thats here 50) and then make your table
byteout = lut[difference/50 + 12];
And in the lookup table you can just use your values in the posted order, where you duplicate the entries in case your stepping is 100.
Btw it just see, there is a mistake, all your negative cases are catched by your first <=0 (my example assumes that you want to omit the first case).
Firstly, take a look at where you want that first check against 0, as it makes all of your negative checks pointless.
Secondly, I would probably construct a lookup table as an array of 1300 elements, offset by 500 (your lowest negative value). Each element would be the result you want when you look up that number. If you are looking for something less than -500, don't check the array.
So it would look something like this:
table[0] = 0b0110; // -500 through -599
table[1] = 0b0110;
...
table[100] = 0b0101; // -400 through -499
table[101] = 0b0101;
...
Lookup would be:
if (value <= -600) {
return 0b0111;
}
else {
return table[value + 600];
}
It's a small enough number of values that the size of the array is not prohibitive. Initialize with a loop at the beginning of your program.
Binary search for the win.
Store all possible values in an array, and be sure to sort them.
Start in the middle and see if difference is less than that value. If so, move to the middle of what's left of your cursor and try again. If not, move to the right. Keep going until you find the value you want, and then use that.
Your array could be of structs that have the minimum value and the corresponding byteout value.
EDIT: To clear up possible misunderstandings, with "every possible value" I don't mean every number between -1400 and 1400, just values you check against in your original code.
Let's look at the first bit:
if (difference <= 0)
byteout = 0b0000;
else if (difference <= -600)
byteout = 0b0111;
Let's say you have a value of -601.
Is it <= 0? Yes, so byteout = 0b0000;
you never get to the -600. So effectively, ALL negative values are 0b0000. This may or may not be by design, but if it is you can get rid of all other negative values.
Otherwise, I would consider reducing this to a formula (with as few branches as possible) or going with #Ebomike's solution of a precomputed lookup table and binary search.

Resources