Adding numbers Conditionally - c

This seems basic but I need to add numbers whether or not they are the condition is "on" (i'll probably change this to boolean). So my question is how to do this in C code if it is possible. I tried something of this sort and various renditions:
dfTotalTaxOwed[nIndex] = dfFedTaxOwed[nIndex] + if(arrNYStateTaxStatus[nIndex] == 1){dfNYStateTaxOwed[nIndex];}
+ if(arrNDStateTaxStatus[nIndex] == 1){dfNDStateTaxOwed[nIndex];}
+ if(arrNHStateTaxStatus[nIndex] == 1){dfNHStateTaxOwed[nIndex];}
+ if(arrOHStateTaxStatus[nIndex] == 1){dfOHStateTaxOwed[nIndex];}
+ if(arrPAStateTaxStatus[nIndex] == 1){dfPAStateTaxOwed[nIndex];}
+ if(arrNJStateTaxStatus[nIndex] == 1){dfNJStateTaxOwed[nIndex];}
+ dfFicaTaxOwed[nIndex];
thanks

You can use the ternary operator.
expr ? true value : false value
I.e. replace if(arrNYStateTaxStatus[nIndex] == 1){dfNYStateTaxOwed[nIndex];} with (arrNYStateTaxStatus[nIndex] == 1) ? dfNYStateTaxOwed[nIndex] : 0.
On a side note, you might want to consider redesigning your program to use a dictionary instead of having an array for each state.

C control statements don't have return values so this approach won't work. Is there a reason you don't want to do a series of if statements like
if(arrNYStateTaxStatus[nIndex] == 1) {
dfTotalTaxOwed[nIndex] += dfNYStateTaxOwed[nIndex];
}
?

Related

how do I define c style code blocks in lua?

is there a way to define {} and blankspace as a lua code block?
something like this..
function()
{
local x = 3
if (x == 1) { print("hi1") }
elseif (x == 2) print("hi2")
else (x == 3) print("hi3")
}
it would also be nice to define things like ++ and += too
Just use do..end. += operator and friends aren't conformant with spirit of Lua. Your code will not run. First of all, you need to understand basic Lua syntax. Example of corrected code:
function f()
local x = 3
if x == 1 then
print("hi1")
elseif x == 2 then
print("hi2")
elseif x == 3 then
print("hi3")
end
end
To create block simply use
do
print('Hello, world!')
end
You can check out Lua manual here, whenever you run to trouble.

Multiple logical operator || (OR) conditions in for loop in C

I started studying C a week ago and decided to write my own tic-tac-toe game for practise.
I have a game loop in main:
for(int i = 1; player1.isWinner!=1 || player2.isWinner!=1 || noWinner!=1; i++){...}
Where i - counts turns and condition of end of the game is one of players has won, or no one has won (draw).
For now, it quits executing only if all conditions are 1.
How can I make it work right?
Is a value of 1 where someone won?
If so, then you would need check any of those conditions is true and loop if they are not:
!(player1.isWinner==1 || player2.isWinner==1 || noWinner==1)
Or using AND, check and loop when none are set:
(player1.isWinner!=1 && player2.isWinner!=1 && noWinner!=1)
Consider extracting the condition to a well-named function in order to aid readability and maintanability:
int hasWinner(/*...*/)
{
return player1.isWinner == 1 || player2.isWinner == 1 || noWinner == 1;
}
It then becomes obvious what the condition should be:
for(int i = 1; !hasWinner(/*...*/); i++){ /*...*/ }
You seem to be using some sort of backwards boolean logic. If 1 represents the boolean value true, then the condition should be
!(player1.isWinner || player2.isWinner || noWinner)
This assumes that you set player1.isWinner to 1 when player1 has won.
It would probably be easier to use bool with values true or false from stdbool.h.

Arrays in Processing

I'm quite new to programming and have recently started in Processing.
In my code, the collide function sets the touch boolean to be true but by arraying it, it only tests true for the final array and not the ones before it. Where am I going wrong here? I hope my question is clear enough.
edit:
Sorry, let me try again.
I guess my problem is finding out how to array the collide function properly. I cant seem to add a [i] for the collide in the array.
At the moment, the code works but it only tests true for the last array and not for the ones before it.
The array code:
for(int i = 0 ; i < lineDiv; i++){
collide(xPts[i], yPts[i], vecPoints.xPos, vecPoints.yPos, myDeflector.Thk, vecPoints.d);
The collide function:
void collide(float pt1x, float pt1y, float pt2x, float pt2y, int size1, int size2){
if (pt1x + size1/2 >= pt2x - size2/2 &&
pt1x - size1/2 <= pt2x + size2/2 &&
pt1y + size1/2 >= pt2y - size2/2 &&
pt1y - size1/2 <= pt2y + size2/2) {
touch = true;
}
else{
touch=false;
}
Your "touch" variable is global. Every time you call the collide() function, it overwrites whatever it was set to before. Perhaps you just want to test if touch is true after calling collide(), then exit the for loop?
Alternatively, you may want to make collide() return the touch boolean, avoiding the global.
It looks like what you want to do is to run through a loop, run the function on that element of the array and return a value if any of them are true. This is my best guess, you might want to edit your question to clarify what you are looking to do. So assuming this:
1) change you method to a function
boolean collide(float pt1x, float pt1y, float pt2x, float pt2y, int size1, int size2){
if (pt1x + size1/2 >= pt2x - size2/2 &&
pt1x - size1/2 <= pt2x + size2/2 &&
pt1y + size1/2 >= pt2y - size2/2 &&
pt1y - size1/2 <= pt2y + size2/2) {
return true;
}
else{
return false;
}
2) change your loop and how you are calling it
touch = false; // if you don't set this to false before the loop, it will be the last value taken
for(int i = 0 ; i < lineDiv; i++){
if (collide(xPts[i], yPts[i], vecPoints.xPos, vecPoints.yPos, myDeflector.Thk, vecPoints.d)) touch = true;
Before the action would be that touch might cycle between true and false as you iterate through the array and in processing (because you would likely draw out the data) it is unlikely that you would want this behavior because you wouldn't be able to do anything with it unless you packed that data in another structure like an array.
So now, the "touch" is set to false and will change to true if any function calls return a true. If all are false, it will stay false.
note: you might consider using either xPts.length() or yPts.length() vs lineDiv. This would reduce the possibility of a array out of bounds exception assuming xPts and yPts have the same # of elements.

Comparing chars

for my word game i'm comparing chars in words, 6 in the first word, 6 in the random word.
Now I want it to do things when certain letters are on there place.
for example if
(aChar1 == aChar7 && aChar2 == aChar8){
//do something
}
but later in my code there is
(aChar1 == aChar7 && aChar2 == aChar8 && aChar3 == aChar9){
//do something
}
Now I just want the second code line to happen and not the first since that is only if the first 2 are in place. I need to add code like :
(aChar1 == aChar7 && aChar2 == aChar8 && aChar3 isnotequalto aChar9){
//do something
}
What code should I use for saying not equal?
use this and tell me if it works
achar3!=achar9

A better way to properly implement string 'in' array

suppose you have an array with a number of strings in ActionScript3 and you want to test if a test string is "in" that array. "in" only works against the index with Arrays in AS3 (which is totally retardo if you ask me), though it does work with ojects, but we're not talking about objects.
Can someone improve (reduce) on this code I'm using now? I'm hoping to avoid defining a utility function - I'd like a nice elegant one-liner.
myArray.filter(function(item:*, i:int, a:Array) { return (item == testString); }).length
Since 0 == false we can use it in a test. Do note that testString's scope is defined in the containing function, encapsulated by the closure.
if (allowedProfiles.filter(function(item:*, i:int, a:Array) { return (item == name); }).length){ // yay! }
Use the Array.indexOf() method to see that the index of the string in the array is not -1 (not found):
var myArray:Array = ["hello", "world"];
trace(myArray.indexOf("hello")); // == 0;
trace(myArray.indexOf("goodbye")); // == -1
Why not just use indexOf()?
if(myArray.indexOf("testString") != -1) { // it's in there

Resources