Pick a random number but not the number 3 - ios

What would you add to make it so it still picks a random number but not the number 3?
- (IBAction)Button3 {
{
int randomviews = rand() % 6;
Label1.text = [NSString stringWithFormat:#"%i", randomviews];
}

Here's another way to do it with just a single call to rand(). Since you're excluding one number from your range, request a smaller range of numbers and then replace any generated 3's with the top number of the previous range:
- (IBAction)Button3 {
{
int randomviews = rand() % 5;
if (randomviews == 3) {
randomviews = 5
}
Label1.text = [NSString stringWithFormat:#"%i", randomviews];
}

- (IBAction)Button3 {
int randomviews;
do {
randomviews = rand() % 6;
}
while (randomviews == 3);
Label1.text = [NSString stringWithFormat:#"%i", randomviews];
}
If you would like to exclude zero as well:
- (IBAction)Button3 {
int randomviews;
do {
randomviews = rand() % 6;
}
while (randomviews == 3 || randomviews == 0);
Label1.text = [NSString stringWithFormat:#"%i", randomviews];

Related

Addition of two random number , with four options on swipe viewcontroller is not work perfectly

I am developing application of addition of two random number with their four options , On Swipe of View-controller the two numbers and their options will change, for change options I use array to marge that number & print it again. But on swipe of view it will not work perfectly, smooth swipe is not happen, here is my code please help me.
-(void)makeAddition
{
number1 = (arc4random()%100)+1; //Generates Number from 1 to 100.
number2 = (arc4random()%100)+1;
addition = number1+number2;
wrongOne = (arc4random()%100)+1;
wrongTwo = (arc4random()%100)+1;
wrongThree = (arc4random()%100)+1;
NSString *w1 = [NSString stringWithFormat:#"%d",wrongOne];
NSString *w2 = [NSString stringWithFormat:#"%d",wrongTwo];
NSString *w3 = [NSString stringWithFormat:#"%d",wrongThree];
answer = [NSString stringWithFormat:#"%d",addition];
// add randam values in array
[_AnswerArray addObject:answer];
[_AnswerArray addObject:w1];
[_AnswerArray addObject:w2];
[_AnswerArray addObject:w3];
NSLog(#"%# add objects in array",_AnswerArray);
// mearge arrays object here
NSUInteger count = [_AnswerArray count];
for (NSUInteger i = 0; i < count; ++i)
{
NSUInteger nElements = count - i;
NSUInteger n = (arc4random() % nElements) + i;
[_AnswerArray exchangeObjectAtIndex:i withObjectAtIndex:n];
NSLog(#"%# exchange array objects",_AnswerArray);
} // if addition is less than 100 then print number & options
if (addition <= 100)
{
_numberOne.text = [NSString stringWithFormat:#"%d",number1];
_numberTwo.text=[NSString stringWithFormat:#"%d",number2];
// checking all four buttons value with array value
if (_AnswerOne.currentTitle != _AnswerTwo.currentTitle ||
_AnswerOne.currentTitle != _AnswerThree.currentTitle ||
_AnswerOne.currentTitle != _AnswerFour.currentTitle) {
[_AnswerOne setTitle:[_AnswerArray objectAtIndex:0] forState:UIControlStateNormal];
}
if (_AnswerTwo.currentTitle != _AnswerOne.currentTitle ||
_AnswerTwo.currentTitle != _AnswerThree.currentTitle ||
_AnswerTwo.currentTitle != _AnswerFour.currentTitle) {
[_AnswerTwo setTitle:[_AnswerArray objectAtIndex:1] forState:UIControlStateNormal];
}
if (_AnswerThree.currentTitle != _AnswerOne.currentTitle ||
_AnswerThree.currentTitle != _AnswerTwo.currentTitle ||
_AnswerThree.currentTitle != _AnswerFour.currentTitle) {
[_AnswerThree setTitle:[_AnswerArray objectAtIndex:2] forState:UIControlStateNormal];
}
if (_AnswerFour.currentTitle != _AnswerOne.currentTitle ||
_AnswerFour.currentTitle != _AnswerTwo.currentTitle ||
_AnswerFour.currentTitle != _AnswerThree.currentTitle) {
[_AnswerFour setTitle:[_AnswerArray objectAtIndex:3] forState:UIControlStateNormal];
}
}
[_AnswerArray removeAllObjects];
}
-(void)slideToLeftWithGestureRecognizer : (UISwipeGestureRecognizer *)gestureRecognizer
{
[self makeAddition];
}

Taking Number From button will not allow to enter more than two digit number

I am developing calculater ,
i use sender button tag value to get number from button.. user will not able to enter more than two
digit value in textfield
i.e. 34+22 , 23+22 like this he will able to enter,
234+234 like that he is not able to enter value in textfield.
for that i use
-(IBAction)numberpress:(UIButton *)sender //number select
{
NSString *number = sender.currentTitle;
self.caldisplay.text = [self.caldisplay.text stringByAppendingString:number];
NSLog(#"%# number is caldis",self.caldisplay.text);
}
How i do this , help me thanks..
try
-(IBAction)numberpress:(UIButton *)sender //number select
{
NSString *number = sender.currentTitle;
NSString *calculatingString = nil;
if (self.caldisplay.text.length >= 2) {
NSString *lastTwoChar = [self.caldisplay.text substringFromIndex:[self.caldisplay.text length] - 2];
if ([lastTwoChar integerValue] >= 10) { // last char is content two number
if ([number integerValue] > 0 || [number isEqualToString:#"0"]) {
// invalid case. donothing here
calculatingString = self.caldisplay.text;
} else {
calculatingString = [self.caldisplay.text stringByAppendingString:number];
}
} else {
calculatingString = [self.caldisplay.text stringByAppendingString:number];
}
} else {
calculatingString = [self.caldisplay.text stringByAppendingString:number];
}
// check valid input
if ([self calculate:calculatingString] > 100) {
// invalid case. donothing here
} else {
self.caldisplay.text = calculatingString;
}
NSLog(#"%# number is caldis",self.caldisplay.text);
}
- (NSInteger)calculate:(NSString *)input {
// you need to wite code calculating string here
// example code to calculate only operator '+'
NSArray *operands = [input componentsSeparatedByString:#"+"];
NSInteger result = 0;
for (NSString *operand in operands) {
result = result + [operand integerValue];
}
return result;
}
You can use it like this
- (IBAction)btnNumberClicked:(UIButton*)aButton
{
if([self.caldisplay.text isEqualToString:#"0"])
{
self.caldisplay.text = aButton.currentTitle;
}
else
{
self.caldisplay.text = [self.caldisplay.text stringByAppendingString:aButton.currentTitle];
}
}

Adding Value in variable (If statement)

I'm trying to figure this out, I have a counting app and want it to increase by 45. Assume the user clicks + 5 times, they can only subtract it -5 for it to equal 0.
Here is my if statement but it doesn't work, it goes into the negatives.
Can anyone help? It's not coming to me. It's the (if count >= 0)
-(IBAction)upButton45:(id)sender {
xCount1 +=1;
countNumber45 +=45;
x45Label.text = [NSString stringWithFormat:#"45x%i", xCount1];
totalWeight += 45;
TotalWeightLabel.text = [NSString stringWithFormat:#"%d LBS", totalWeight];
}
-(IBAction)downButton45:(id)sender {
xCount1 -= 1;
countNumber45 -= 45;
x45Label.text = [NSString stringWithFormat:#"45x%i", xCount1];
if (countNumber45 <= 0) {
countNumber45 = 0;
xCount1 = 0;
x45Label.text = #"";
}
if (xCount1 >= 0) {
totalWeight -= 45;
TotalWeightLabel.text = [NSString stringWithFormat:#"%d LBS", totalWeight];
}
}
-(IBAction)upButton45:(id)sender
{
if(xCount + 1 <= 45) //Your max allowed tap
{
xCount1 +=1;
countNumber45 +=45;
x45Label.text = [NSString stringWithFormat:#"45x%i", xCount1];
totalWeight += 45;
TotalWeightLabel.text = [NSString stringWithFormat:#"%d LBS", totalWeight];
}
}
-(IBAction)downButton45:(id)sender
{
if(xCount - 1 >= 0)
{
xCount1 -= 1;
countNumber45 -= 45;
x45Label.text = [NSString stringWithFormat:#"45x%i", xCount1];
totalWeight -= 45;
TotalWeightLabel.text = [NSString stringWithFormat:#"%d LBS", totalWeight];
}
}
-(IBAction)upButton45:(id)sender
{
//Assuming that the lable always hold value and have "0" as start value, and number is at first position
NSArray *stringList = [TotalWeightLabel.text componentsSeparatedByString:#" "];
int currentValue = [(NSString *)stringList[0] intValue];
currentValue = currentValue + 45;
TotalWeightLabel.text = [NSString stringWithFormat:#"%d LBS", currentValue];
}
-(IBAction)downButton45:(id)sender
{
//Assuming that the lable always hold a int value and number is at first position
NSArray *stringList = [TotalWeightLabel.text componentsSeparatedByString:#" "];
int currentValue = [(NSString *)stringList[0] intValue];
if (currentValue != 0) {
currentValue = currentValue - 45;
}
TotalWeightLabel.text = [NSString stringWithFormat:#"%d LBS", currentValue];
}

Randomizing position of correct answer in a quiz application for ios(Objective -C)

I am trying to develop a simple quiz application for IOS. I need to pick up 10 random images out of 50 , and display each image and 4 options with one correct answer. Presently I am able to randomize images , but each time, the correct option is displayed only at one position. Can anyone help me for randomizing the position of correct answer for different questions (i.e, images)?
- (void)viewDidAppear:(BOOL)animated
{
myData = nil;
[super viewDidAppear:animated];
[self.view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:#"WoodTexture"]]];
NSString *bundle = [[NSBundle mainBundle] pathForResource:#"Images" ofType:#"plist"];
myData = [[NSMutableArray alloc] initWithContentsOfFile:bundle];
[self setImageswithOptions];
count = 0;
score = 0;
[self resetScreen];
}
The method definition goes below.
- (void)setImageswithOptions
{
NSInteger number[4],i;
for ( i = 0; i <4 ; ++i ) {
number[i] = arc4random() % myData.count ;
for (int j = 0; j < i; ++j) {
if ((number[i] < 0) || (number[i] == number[j] )) {
--i;
}
}
}
self.submit.hidden =YES;
self.displayImage.image = [UIImage imageNamed:[NSString stringWithFormat:#"%#",myData[number[0]]]];
NSInteger random = arc4random() % 4;
if (random == 0) {
random += 1;
}
[(UILabel *)[self.view viewWithTag:random] setText:[NSString stringWithFormat:#"%#",myData[number[0]]]];
[(WQZOptionButton *)[self.view viewWithTag:random + 4] setIsCorrect:YES];
NSInteger imageCount = 0;
for (NSInteger index = 1; index <= 4; index++) {
if (index != random) {
imageCount++;
[(UILabel *)[self.view viewWithTag:index] setText:[NSString stringWithFormat:#"%#",myData[number[imageCount]]]];
[(WQZOptionButton *)[self.view viewWithTag:index + 4] setIsCorrect:NO];
}
}
[myData removeObjectAtIndex:number[0]];
self.scoreLabel.text = [NSString stringWithFormat:#"Score: %ld", (long)score];
}
There are several problems.
One problem:
NSInteger random = arc4random() % 4;
if (random == 0) {
random += 1;
}
random will be 1, 2 or 3 and 1 will occur twice as often as 2 or 3.
Also it is better to use arc4random_uniform() than the modular operator:
Instead of arc4random() % 4
use arc4random_uniform(4)

iOS: Setting variables on several custom view objects

Okay, so I'm creating X number of a custom UIView, that I've created in IB...
I create them in a grid-like formation and need to set their individual properties based on a response from a web service call...
The part I'm having trouble with is how to iterate through the different UIViews and set the variables...
I'm pretty sure the solution is really simple, but I've been staring blindly at this for some time now...
It's the part after:
if([theStatus.groupName isEqualToString:groupEntry.groupNameLabel.text])
{
Here is the entire method:
- (void)receivedGroups
{
int rows, columns;
if([groupConnection.groupsArray count] <= 4)
{
rows = 1;
columns = [groupConnection.groupsArray count];
} else if([groupConnection.groupsArray count] >= 5 && [groupConnection.groupsArray count] <= 8)
{
rows = 2;
columns = ([groupConnection.groupsArray count] + 1 )/ 2;
} else
{
rows = 3;
columns = ([groupConnection.groupsArray count] + 2 )/ 3;
}
int number = 0;
for(int j=1; j < columns+1; j++)
{
for(int k=0; k < rows; k++)
{
// Only create the number of groups that match the number of entries in our array
if(number < [groupConnection.groupsArray count])
{
// Create an instance of the group view
GroupEntry *groupEntry = [[GroupEntry alloc] initWithFrame:CGRectMake(230*j, 250*k, 180, 233)];
// Add it to the view
[self.view addSubview:groupEntry];
// Get the group
GetGroupsActive *theGroups = [groupConnection.groupsArray objectAtIndex:number];
groupEntry.groupNameLabel.text = theGroups.groupName;
for(int i=0; i<[statusConnection.statusArray count]; i++)
{
CurrentStatus *theStatus = [statusConnection.statusArray objectAtIndex:i];
if([theStatus.groupName isEqualToString:groupEntry.groupNameLabel.text])
{
//allChildren++;
switch(theStatus.currentStatus)
{
case 0:
//childrenSick++;
break;
case 1:
//childrenVacation++;
break;
case 2:
//childrenPresent++;
break;
case 3:
//childrenOut++;
break;
case 4:
//childrenTour++;
break;
default:
break;
}
}
}
NSString *allLabelText = [NSString stringWithFormat:#"%i", allChildren];
NSString *sickLabelText = [NSString stringWithFormat:#"%i", childrenSick];
NSString *vacationLabelText = [NSString stringWithFormat:#"%i", childrenVacation];
NSString *presentLabelText = [NSString stringWithFormat:#"%i", childrenPresent];
NSString *outLabelText = [NSString stringWithFormat:#"%i", childrenOut];
NSString *tripLabelText = [NSString stringWithFormat:#"%i", childrenTour];
groupEntry.sickLabelNumber.text = sickLabelText;
groupEntry.presentLabelNumber.text = presentLabelText;
groupEntry.numberLabelNumber.text = allLabelText;
groupEntry.tripLabelNumber.text = tripLabelText;
groupEntry.outLabelNumber.text = outLabelText;
groupEntry.vacationLabelNumber.text = vacationLabelText;
// Create the buttons to handle button press
UIButton *childButton = [UIButton buttonWithType:UIButtonTypeCustom];
childButton.frame = CGRectMake(230*j, 250*k, 180, 233);
// Set an identity tag, so we can recognize it during button press
childButton.tag = theGroups.ID;
// When EventTouchUpInside, send an action to groupSelected:
[childButton addTarget:self action:#selector(groupSelected:) forControlEvents:UIControlEventTouchUpInside];
// Add it to the view
[self.view addSubview:childButton];
}
number++;
}
}
}
If you added all the views in a parent view. You can get all the subviews using,
NSAarry *subviews = [base subviews];
for(UIView *subview in subviews)
{
subview.property = yourVaule;
}
You can differentiate between subviews using its tag or another property.

Resources