I´m wondering if it´s possible to create some kind of an or statement inside an if/else statement that randomly chooses from different values -
Like this:
if ([white.number floatValue] >= 15.0f) {
[forcastLabel setText:#"Good!"]; OR STATEMENT - [forcastLabel setText:#"BRILLIANT"];
You can try sth like this :)
NSInteger randomNumber = arc4random() % 2;
if ([white.number floatValue] >= 15.0f) {
randomNumber > 0 ? [forcastLabel setText:#"Good!"] : [forcastLabel setText:#"BRILLIANT"];
There is many other ways to achieve what you want my is just one of them.
Related
I have been attempting to create an if else statement that will return a text string based on certain constraints. The first 3 constraints work, but when the event of the final constraint occurs, it triggers the second again. The random number generator occasionally used a 0 value, so I wanted to account for that. I am new to this, and apologize for indenting, etc.
I have been looking around here for a bit and couldn't find anything that seemed to cover this. If I missed it, a hint in the right direction would be appreciated as well.
double txtestimateCategory = [mynum computeVolume];
NSLog(#"The volume is %f", txtestimateCategory);
int v = ((txtestimateCategory * 1));
if ((v >= 8000))
{
NSLog(#"The box is large");
}
else if ((1 <= v < 1000))
{
NSLog(#"The box is small");
}
else if ((1000 <= v < 8000))
{
NSLog(#"The box is medium");
}
else
{
NSLog(#"The box is a lie");
}
Comparators are binary operators. You have to write:
else if (1 <= v && v < 1000)
etc.
(Otherwise you would be evaluating things like true < 1000, and true converts to 1 implicitly. Not what you meant!)
I am trying to make a game which has a jumbled up algebra equation. I have assigned an outlet to each component of the equation. For example, if the equation was:
x2 + y = 2(a+b)
Then x2 (which is x squared), +, y, = and 2(a+b) would all be its own outlet. But the equation is going to be jumbled up and I want the user to move the label outlets to the correct order. I have enabled touchesMoved, but my problem lies in checking if the equation is in the correct order. I would wrap the code into an IBAction button action, but how do I analyze the text? Would I check for the offset between each label? Is there an easy way/API to do this? Thanks!
Assuming your labels are called xLabel,plusLabel, yLabel, equalsLabel, and abLabel, you could do something like this:
NSUInteger xLeftBorder = CGRectGetMinX(xLabel.frame);
NSUInteger plusLeftBorder = CGRectGetMinX(plusLabel.frame);
NSUInteger yLeftBorder = CGRectGetMinX(yLabel.frame);
NSUInteger equalsLeftBorder = CGRectGetMinX(equalsLabel.frame);
NSUInteger abLeftBorder = CGRectGetMinX(abLabel.frame);
if(xLeftBorder < plusLeftBorder && plusLeftBorder < yLeftBorder && yLeftBorder < equalsLeftBorder && equalsLeftBorder < abLeftBorder){
//Correct!
}
else{
//Incorrect
}
This is kind of clumsy, but it works. An even better way to do it would be to put this in a function with each parameter being a label to check. For example:
bool isCorrect = [self checkIf:xLabel isLessThan: plusLabel isLessThan: yLabel isLessThan:equalsLabel isLessThan:abLabel];
This is assuming the function you write returns a bool.
Hope this helped!
i am not sure about my english, but i need to get the unit digit of an integer.
WITHOUT complex algorithm but with some API or another trick.
for example :
int a= 53;
int b=76;
this i add because i almost always dont "meet the quality standards" to post! its drive me crazy! please , fix it ! it took me 10 shoots to post this,and other issue also.
i need to get a=3 and b=6 in a simple smart way.
same about the other digit.
thanks a lot .
here is how to split the number into parts
int unitDigit = a % 10; //is 3
int tens= (a - unitDigit)/10; //is 53-3=50 /10 =5
You're looking for % operator.
a=a%10;//divides 'a' by 10, assigns remainder to 'a'
WARNING
here is how to divine the number into parts
int unitDigit = a % 10; //is 3
int tens= (a - unitDigit)/10; //is 53-3=50 /10 =5
this answer is totally incorrect. It may work only in a number of cases. For example try to get the first digit of 503 via this way
It seems the simplest answer (but not very good in performance):
int a = ...;
int digit = [[[NSString stringWithFormat:#"%d", a] substringToIndex:1] intValue]; //or use substringWithRange to get ANY digit
Modulo operator will help you (as units digit is a reminder when number is divided by 10):
int unitDigit = a % 10;
The following code "gets" the digits of a given number and counts how many of them divide the number exactly.
int findDigits(long long N){
int count = 0;
long long newN = N;
while(newN) // kinda like a right shift
{
int div = newN % 10;
if (div != 0)
if (N % div == 0) count++;
newN = newN / 10;
}
return count;
}
Is there a method in objective-c that allows you to pseudorandomly decide between two ints? Or is there a quick way to implement this?
Use the following:
int number = arc4random() % 2;
if(number==0){
//pick one number
}else{
//pick other number
}
randomSelection = arc4random() % 2 ? choice1 : choice2;
in C++ that would be result = rand() < 0.5 ? int1 : int2
why can't you build the same command in ObjC?
I have another question on how to make most elegant solution to this problem, since I cannot afford to go to computer school right so my actual "pure programming" CS knowledge is not perfect or great. This is basically an algorhythm problem (someone please correct me if I am using that wrong, since I don't want to keep saying them and embarass myself)
I have 4 objects. Each of them has an species property that can either be a dog, cat, pig or monkey. So a sample situation could be:
object1.species=pig
object2.species=cat
object3.species=pig
object4.species=dog
Now, if I want to figure out if all 4 are the same species, I know I could just say:
if ( (object1.species==object2.species)
&& (object2.species==object3.species)
&& (object3.species==object4.species)
) {
// They are all the same animal (don't care WHICH animal they are)
}
But that isn't so elegant right? And if I suddenly want to know if EXACTLY 3 or 2 of them are the same species (don't care WHICH species it is though), suddenly I'm in spaghetti code.
I am using Objective C although I don't know if that matters really, since the most elegant solution to this is I assume the same in all languages conceptually? Anyone got good idea?
Thanks!!
You can use a hash table (or dictionary in objective-C) with the key on the species, incrementing the count every time.
For clarity (pseudo-code):
// N is 4 for your particular case
for ( int i = 0; i < N; i++ )
hashtable[ object[i].species ]++;
hashtable[ Species.PIG ]; // number of pigs (constant)
or if you want to unroll it manually:
hashtable[ object1.species ]++;
hashtable[ object2.species ]++;
hashtable[ object3.species ]++;
hashtable[ object4.species ]++;
now you can check through the species to see the count:
for each key in hashtable
if ( hashtable[ key ] == 3 ) // species "key" has 3 items
/* Code */
of course, "hashtable" can be just a simple array if species is just a simple enum/integer. The above should be the same theory in either case.
This is precisely what sets and counted sets are for.
BOOL allSameSpecies(NSArray *animals) {
NSSet *speciesSet = [NSSet setWithArray:[animals valueForKey:#"species"]];
return [[speciesSet] count] == 1;
}
BOOL sameSpeciesNumber(NSArray *animals, NSUInteger targetCount) {
NSCountedSet *speciesCounts = [NSCountedSet setWithArray:[animals valueForKey:#"species"]];
for (id species in speciesCounts) {
if ([speciesCounts countForObject:species] == targetCount)
return YES;
}
return NO.
}
You can count how many comparisons match of all possible comparisons. If exactly 1 comparison is true, then exactly 2 items are the same, if 3 comparisons match, exactly 3 items are the same. This example is in C, you'll have to convert it to objective-C on your own.
int count = 0;
count += object1.species == object2.species;
count += object1.species == object3.species;
count += object1.species == object4.species;
count += object2.species == object2.species;
count += object2.species == object3.species;
count += object3.species == object4.species;
// count 0 - all different
// count 1 - exactly 2 are the same
// count 2 - two pairs of 2
// count 3 - exactly 3 are the same
// count 6 - all are the same
The counting can also be implemented as a loop:
for (int i = 0; i < 4; i++)
for (int j = i + 1; j < 4; j++)
count += objects[i].species == objects[j].species;
This approach only works if the amount of objects is 5 or less. Because of this and the fact that the amount of comparisons scales quadratically, it's better to use a hashtable if the amount of objects is larger.