Objective C - Only part of an if condition working - ios

I have an if statement and the statement is only being run on the fist third of the condition (before the fist 'or' (||).
if (((day == 1) && (period == 1)) || ((day == 3) && (period == 3)) || ((day = 5) && (period == 1)))
{
NSLog(#"Return Line 1");
}
The condition is only met when day = 1 and period = 1. Could someone help me get the other arrangements working in the condition. Thank you in advance.

if (((day == 1) && (period == 1)) || ((day == 3) && (period == 3)) || ((day == 5) && (period == 1)))
{
NSLog(#"Return Line 1");
}
In your code there is only one = instead of ==
The error in your code is ((day = 5) && (period == 1))

Related

Combining coverpoints to create an aggregate

`define A 'd1
`define B 'd2
`define C 'd3
`define D 'd4
`define E 'd5
`define F 'd6
`define I 'd7
`define J 'd8
module testModule(input clk,
input CReset,
input[4;0] Opcode_P0I1,
input[4;0] Opcode_P0I2,
input[4;0] Opcode_P0I3,
input[4;0] Opcode_P0I4,
input[4;0] Opcode_P1I1,
input[4;0] Opcode_P1I2,
input[4;0] Opcode_P1I3,
input[4;0] Opcode_P1I4,
input P0V,
input P1V,
input P0IsFe,
input P1IsFe,
input P0I1PAllow,
input P0I2PAllow,
input P0I3PAllow,
input P0I4PAllow,
input P1I1PAllow,
input P1I2PAllow,
input P1I3PAllow,
input P1I4PAllow,
input I1VT0,
input I1VT1,
input I1VT2,
input I1VT4,
input I2VT0,
input I2VT1,
input I2VT2,
input I2VT3,
input I3VT0,
input I3VT1,
input I3VT2,
input I3VT3,
input I4VT0,
input I4VT1,
input I4VT2,
input I4VT3,
input[3:0] P0TId,
input[3:0] P1TId);
covergroup cg_Abc #(posedge clk);
option.per_instance = 1;
cp_P0I1_IsSquare: coverpoint Opcode_P0I1 iff (P0V == 1 && P0IsFe == 1 && P0I1PAllow == 1 && (I1VT0 && P0TId == 2**0 || I1VT1 && P0TdId == 2**1 || I1VT2 && P0TId == 2**2 || I1VT3 && P0TId == 2**3)) {option.weight = 0;type_option.weight = 0; bins derp = {`A,`B,`C};}
cp_P0I2_IsSquare: coverpoint Opcode_P0I2 iff (P0V == 1 && P0IsFe == 1 && P0I2PAllow == 1 && (I2VT0 && P0TId == 2**0 || I2VT1 && P0TdId == 2**1 || I2VT2 && P0TId == 2**2 || I2VT3 && P0TId == 2**3)) {option.weight = 0;type_option.weight = 0; bins derp = {`A,`B,`C};}
cp_P0I3_IsSquare: coverpoint Opcode_P0I3 iff (P0V == 1 && P0IsFe == 1 && P0I3PAllow == 1 && (I3VT0 && P0TId == 2**0 || I3VT1 && P0TdId == 2**1 || I3VT2 && P0TId == 2**2 || I3VT3 && P0TId == 2**3)) {option.weight = 0;type_option.weight = 0; bins derp = {`A,`B,`C};}
cp_P0I4_IsSquare: coverpoint Opcode_P0I4 iff (P0V == 1 && P0IsFe == 1 && P0I4PAllow == 1 && (I4VT0 && P0TId == 2**0 || I4VT1 && P0TdId == 2**1 || I4VT2 && P0TId == 2**2 || I4VT3 && P0TId == 2**3)) {option.weight = 0;type_option.weight = 0; bins derp = {`A,`B,`C};}
cp_P1I1_IsSquare: coverpoint Opcode_P1I1 iff (P1V == 1 && P1IsFe == 1 && P1I1PAllow == 1 && (I1VT0 && P1TId == 2**0 || I1VT1 && P1TdId == 2**1 || I1VT2 && P1TId == 2**2 || I1VT3 && P1TId == 2**3)) {option.weight = 0;type_option.weight = 0; bins derp = {`A,`B,`C};}
cp_P1I2_IsSquare: coverpoint Opcode_P1I2 iff (P1V == 1 && P1IsFe == 1 && P1I2PAllow == 1 && (I2VT0 && P1TId == 2**0 || I2VT1 && P1TdId == 2**1 || I2VT2 && P1TId == 2**2 || I2VT3 && P1TId == 2**3)) {option.weight = 0;type_option.weight = 0; bins derp = {`A,`B,`C};}
cp_P1I3_IsSquare: coverpoint Opcode_P1I3 iff (P1V == 1 && P1IsFe == 1 && P1I3PAllow == 1 && (I3VT0 && P1TId == 2**0 || I3VT1 && P1TdId == 2**1 || I3VT2 && P1TId == 2**2 || I3VT3 && P1TId == 2**3)) {option.weight = 0;type_option.weight = 0; bins derp = {`A,`B,`C};}
cp_P1I4_IsSquare: coverpoint Opcode_P1I4 iff (P1V == 1 && P1IsFe == 1 && P1I4PAllow == 1 && (I4VT0 && P1TId == 2**0 || I4VT1 && P1TdId == 2**1 || I4VT2 && P1TId == 2**2 || I4VT3 && P1TId == 2**3)) {option.weight = 0;type_option.weight = 0; bins derp = {`A,`B,`C};}
cp_P0I1_IsCircle: coverpoint Opcode_P0I1 iff (P0V == 1 && P0I1PAllow == 0 && (I1VT0 && P0TId == 2**0 || I1VT1 && P0TdId == 2**1 || I1VT2_DE2 && P0TId == 2**2 || I1VT3 && P0TId == 2**3)) {option.weight = 0;type_option.weight = 0; bins bxx = {`D,`E,`F,`G,`H,`I,`J};}
cp_P0I2_IsCircle: coverpoint Opcode_P0I2 iff (P0V == 1 && P0I2PAllow == 0 && (I2VT0 && P0TId == 2**0 || I2VT1 && P0TdId == 2**1 || I2VT2_DE2 && P0TId == 2**2 || I2VT3 && P0TId == 2**3)) {option.weight = 0;type_option.weight = 0; bins bxx = {`D,`E,`F,`G,`H,`I,`J};}
cp_P0I3_IsCircle: coverpoint Opcode_P0I3 iff (P0V == 1 && P0I3PAllow == 0 && (I3VT0 && P0TId == 2**0 || I3VT1 && P0TdId == 2**1 || I3VT2_DE2 && P0TId == 2**2 || I3VT3 && P0TId == 2**3)) {option.weight = 0;type_option.weight = 0; bins bxx = {`D,`E,`F,`G,`H,`I,`J};}
cp_P0I4_IsCircle: coverpoint Opcode_P0I4 iff (P0V == 1 && P0I4PAllow == 0 && (I4VT0 && P0TId == 2**0 || I4VT1 && P0TdId == 2**1 || I4VT2_DE2 && P0TId == 2**2 || I4VT3 && P0TId == 2**3)) {option.weight = 0;type_option.weight = 0; bins bxx = {`D,`E,`F,`G,`H,`I,`J};}
cp_P1I1_IsCircle: coverpoint Opcode_P1I1 iff (P1V == 1 && P1I1PAllow == 0 && (I1VT0 && P1TId == 2**0 || I1VT1 && P1TdId == 2**1 || I1VT2 && P1TId == 2**2 || I1VT3 && P1TId == 2**3)) {option.weight = 0;type_option.weight = 0; bins bxx = {`D,`E,`F,`G,`H,`I,`J};}
cp_P1I2_IsCircle: coverpoint Opcode_P1I2 iff (P1V == 1 && P1I2PAllow == 0 && (I2VT0 && P1TId == 2**0 || I2VT1 && P1TdId == 2**1 || I2VT2 && P1TId == 2**2 || I2VT3 && P1TId == 2**3)) {option.weight = 0;type_option.weight = 0; bins bxx = {`D,`E,`F,`G,`H,`I,`J};}
cp_P1I3_IsCircle: coverpoint Opcode_P1I3 iff (P1V == 1 && P1I3PAllow == 0 && (I3VT0 && P1TId == 2**0 || I3VT1 && P1TdId == 2**1 || I3VT2 && P1TId == 2**2 || I3VT3 && P1TId == 2**3)) {option.weight = 0;type_option.weight = 0; bins bxx = {`D,`E,`F,`G,`H,`I,`J};}
cp_P1I4_IsCircle: coverpoint Opcode_P1I4 iff (P1V == 1 && P1I4PAllow == 0 && (I4VT0 && P1TId == 2**0 || I4VT1 && P1TdId == 2**1 || I4VT2 && P1TId == 2**2 || I4VT3 && P1TId == 2**3)) {option.weight = 0;type_option.weight = 0; bins bxx = {`D,`E,`F,`G,`H,`I,`J};}
cr_P0I1I2_IsSqauarexIsCircle : cross cp_P0I1_IsSquare,cp_P0I2_IsCircle{option.weight = 0;type_option.weight = 0;}
cr_P0I2I3_IsSqauarexIsCircle : cross cp_P0I2_IsSquare,cp_P0I3_IsCircle{option.weight = 0;type_option.weight = 0;}
cr_P0I3I4_IsSqauarexIsCircle : cross cp_P0I3_IsSquare,cp_P0I4_IsCircle{option.weight = 0;type_option.weight = 0;}
AggregateCov1 : ?
cr_P1I1I2_IsSqauarexIsCircle : cross cp_P1I1_IsSquare,cp_P1I2_IsCircle{option.weight = 0;type_option.weight = 0;}
cr_P1I2I3_IsSqauarexIsCircle : cross cp_P1I2_IsSquare,cp_P1I3_IsCircle{option.weight = 0;type_option.weight = 0;}
cr_P1I3I4_IsSqauarexIsCircle : cross cp_P1I3_IsSquare,cp_P1I14_IsCircle{option.weight = 0;type_option.weight = 0;}
AggregateCov2 : ?
endgroup
cg_Abc cg_Abc_inst = new();
endmodule
I am looking for AggregateCov1 which tells if any of cr_P0I1I2_IsSqauarexIsCircle,cr_P0I2I3_IsSqauarexIsCircle,cr_P0I3I4_IsSqauarexIsCircle is a hit. Essentially meaning I have at least one square and a circle adjacent to each other in the group [Opcode_P0I1,Opcode_P0I2,Opcode_P0I3,Opcode_P0I4].
Also looking for AggregateCov2 which tells if I have at least one square and a circle adjacent to each other in the group [Opcode_P0I1,Opcode_P0I2,Opcode_P0I3,Opcode_P0I4] or [Opcode_P1I1,Opcode_P1I2,Opcode_P1I3,Opcode_P1I4]
There is no need to do this as you need to hit the AND of all coverpoints a,b,c to get 100% coverage of this covergroup.
But if you insist, you can use a coverpoint expression
AggregateCoverpoint: coverpoint v_a | v_b | v_c;
Using assign statements to create new wires, makes this problem go away.

How to make a general method to check for winners in Tic-Tac-Toe

I made a Tic-Tac-Toe game in Ruby. The method below checks for a winner in the vertical columns.
How do I make it so that this method can be applied to boards of different sizes, like 4x4, 6x6?
def vertical_check(array)
result = nil
if (array[0][0] == "X" && array[1][0] == "X" && array[2][0] == "X") ||
(array[0][1] == "X" && array[1][1] == "X" && array[2][1] == "X") ||
(array[0][2] == "X" && array[1][2] == "X" && array[2][2] == "X")
result = "X"
elsif (array[0][0] == "O" && array[1][0] == "O" && array[2][0] == "O") ||
(array[0][1] == "O" && array[1][1] == "O" && array[2][1] == "O") ||
(array[0][2] == "O" && array[1][2] == "O" && array[2][2] == "O")
result = "O"
else
result = nil
end
return result
end
The following is a failed attempt:
def vertical_check_x(array)
result = nil
index = 0
index2 = 0
until result != nil || index == array.length
while array[index][index2] == "X"
index += 1
end
if index == array.length
result = "X"
else
result = nil
index = array.length
end
index2 += 1
end
return result
end
def vertical_check_o(array)
result = nil
index = 0
index2 = 0
until result != nil || index == array.length
while array[index][index2] == "O"
index += 1
end
if index -1 == array.length
result = "O"
else
result = nil
index = array.length
end
index2 += 1
end
return result
end
def vertical_check(array)
result = vertical_check_x(array)
if result == nil
result = vertical_check_o(array)
end
return result
end
To quickly find a winner in given array, count the number of unique elements, confirm that there is only one unique element and if it is only X or O:
def winner arr
return arr[0] if arr.uniq.length == 1 && ['X', 'O'].include?(arr[0])
nil
end
The next problem is selecting the rows, columns and diagonals for an nxn array.
Rows are easy:
rows = arr.map {|row| row}
Columns are as follows - you select elements with the same index for each row:
cols = n.times.collect {|i| arr.map {|row| row[i]}}
Next are diagonals. There are two diagonals, one starts from leftmost corner and the other from the rightmost.
The leftmost diagonal has the sequence as:
(0, 0) -> (1, 1) -> (2, 2) ....
See the pattern?
diag = n.times.collect {|i| arr[i][i]}
The rightmost diagonal has pattern that goes like this (for a 3x3):
(0, 2) -> (1, 1) -> (2, 0)
For a 4x4, it's like this:
(0, 3) -> (1, 2) -> (2, 1) -> (3, 0)
So, the pattern for an nxn is:
(0, n-1-0) -> (1, n-1-1) -> (2, n-1-2) -> ... (i, n-1-i) ... -> (n-1, 0)
So:
diag = n.times.collect {|i| arr[i][n - 1 - i]}
Now, you can just do something like:
w = rows.map {|r| winner r}.compact[0]
for each array to get the winner.

Playground execution failed: <EXPR>:15:33: error: type 'Int' does not conform to protocol 'BooleanType'

I am creating a simple Fizz Buzz Generator with Swift.
I receive the following error:
Playground execution failed: <EXPR>:15:33: error: type 'Int' does not conform to protocol 'BooleanType'
} else if (number % 3 == 0) && (number % 5) {
Here is what I have:
for number in 1...100 {
if (number % 3 == 0) {
println("Fizz")
} else if (number % 5 == 0) {
println("Buzz")
} else if (number % 3 == 0) && (number % 5) {
println("Fizz Buzz")
} else {
println(number)
}
}
You're missing some of your logic. Change
else if (number % 3 == 0) && (number % 5)
to
else if (number % 3 == 0) && (number % 5 == 0)

Making an app that uses a PickerView to drop pins

Me and a group of friends are creating an iPhone app for a project in our technology class. Our app is going to be used for navigation to different high school sporting event locations in our area. We are currently using a PickerView to control all the combinations of school and sport. The picker has two components, one for schools and one for sports. We already have the MapView set up as well.
We are wondering how we can place the pins for all the different school/sport combinations using the outputs from the Picker View. As you can see in the code below, we currently have a ton of if/then statements to receive the outputs from the PickerView, but eventually want to make that less bulky as well.
Here is the code for the picker outputs:
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
int x = [pickerView selectedRowInComponent:0]; NSLog(#"Row 1: %i", x);
int y = [pickerView selectedRowInComponent:1]; NSLog(#"Row 2: %i", y);
//Blue Ridge
if (x == 0 && y == 0)
{NSLog(#"Blue Ridge Basketball");}
else if (x == 0 && y == 1)
{NSLog(#"Blue Ridge Basketball");}
else if (x == 0 && y == 2)
{NSLog(#"Blue Ridge Cross Country");}
else if (x == 0 && y == 3)
{NSLog(#"Blue Ridge Football");}
else if (x == 0 && y == 4)
{NSLog(#"Blue Ridge Golf");}
else if (x == 0 && y == 5)
{NSLog(#"Blue Ridge Soccer");}
else if (x == 0 && y == 6)
{NSLog(#"Blue Ridge Softball");}
else if (x == 0 && y == 7)
{NSLog(#"Blue Ridge Track");}
else if (x == 0 && y == 8)
{NSLog(#"Blue Ridge Volleyball");}
else if (x == 0 && y == 9)
{NSLog(#"Blue Ridge Wrestling");}
//DeeMack
else if (x == 1 && y == 0)
{NSLog(#"DeeMack Baseball");}
else if (x == 1 && y == 1)
{NSLog(#"DeeMack Basketball");}
else if (x == 1 && y == 2)
{NSLog(#"DeeMack Cross Country");}
else if (x == 1 && y == 3)
{NSLog(#"DeeMack Football");}
else if (x == 1 && y == 4)
{NSLog(#"DeeMack Golf");}
else if (x == 1 && y == 5)
{NSLog(#"DeeMack Soccer");}
else if (x == 1 && y == 6)
{NSLog(#"DeeMack Softball");}
else if (x == 1 && y == 7)
{NSLog(#"DeeMack Track");}
else if (x == 1 && y == 8)
{NSLog(#"DeeMack Volleyball");}
else if (x == 1 && y == 9)
{NSLog(#"DeeMack Wrestling");}
}
...etc, This format continues for 11 more schools.
Any ideas as to how we can use these outputs to drop the pins on the map, or ideas to make this code shorter would be greatly appreciated. Also it is important to note that we are all very new to coding, so the simpler the better. Thanks!
To make the code shorter, all you need to do is maintain 2 arrays, one containing the high school names and the other containing the sports.
So create 2 NSArrays as properties of your controller
#property (strong, nonatomic) NSArray *highSchools;
#property (strong, nonatomic) NSArray *sports;
and then in the viewDidLoad method, do this :
self.highSchools = [NSArray arrayWithObjects:#"Blue Ridge", #"DeeMack," nil];
self.sports = [NSArray arrayWithObjects:#"Baseball", #"BasketBall", #"Cross Country", nil];
So now your picker delegate method will reduce to this ::
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
int x = [pickerView selectedRowInComponent:0]; NSLog(#"Row 1: %i", x);
int y = [pickerView selectedRowInComponent:1]; NSLog(#"Row 2: %i", y);
NSLog(#"%# %#", [self.highSchools objectAtIndex:x], [self.sports objectAtIndex:y]);
}
Now, for placing the pins, its not clear from the question, exactly how or where you want to place them and what the pins represent. It seems like you want to drop a single pin on the map depending on a school/sport combination at the location of the selected school. Is that right?

Logical operators and wrong result in Objective C

I got the x and y by a UITapGestureRecognizer set on the screen, after having obtained the location I find the object touched, so I put the conditions, but does not work. Maybe I put the wrong conditions in objective C? Xcode gives no errors but the function does not work.
-(void)tappedMapOniPad:(int)x andy:(int)y{
NSLog(#"the x is: %d", x);
//the x is: 302
NSLog(#"the y is: %d", y);
//the y is: 37
if((121<x<=181) && (8<y<=51)){ //the error is here
self.stand = 431;
}else if ((181<x<=257) && (8<y<=51)){
self.stand=430;
}else if ((257<x<=330) && (8<y<=51)){
self.stand = 429;
}
NSLog(#"The stand is %d", self.stand);
//The stand is 431
}
How can I do?
121<x<=181
let's assume x := 10 121<10<=181 -> false<=181 -> 0<=181 -> true
you have to do it step by step.
((121 < x) && (x <=181))
let's assume x := 10 ((121 < 10) && (10 <=181)) ->false && true ->false
Replace
if((121<x<=181) && (8<y<=51))
by
if((121 < x && x <= 181) && (8 < y && y <= 51))
(121<x<=181) kind of expression is invalid in Obj-c.
Use, (x>121 && x<=181)
Your full code will be like :
if((x>121 && x<=181) && (y>8 && y<=51)){ //the error is here
self.stand = 431;
}
else if ((x>181 && x<=257) && (y>8 && y<=51)){
self.stand=430;
}
else if ((x> 255 && x<=330) && (y>8 && y<=51)){
self.stand = 429;
}
Or you can optimize it as:
if(y>8 && y<=51){
if (x> 257 && x<=330) {
self.stand = 429;
}
else if(x>181){
self.stand=430;
}
else if(x>121){
self.stand = 431;
}
}
Missing &&
Try
if((121<x&&x <=181)&&(8<y&&y <=51))
Hope it helps

Resources