2 Replies - 7332 Views - Last Post: 03 June 2013 - 05:59 AM

#1 PaC-mAn336  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 16-January 13

Objective C trying to generate random question from array

Posted 30 January 2013 - 04:58 PM

For this assignment you need to create a single view app that will serve as a source for questions and answers in a geography trivia game. The GUI should have two labels. One to display the question, another one to display the answer, and two buttons. When the user presses one button the new question should appear. When user presses the second button the answer to currently displayed question should appear. To avoid confusion, when the new question is displayed, the answer label should change to display a sequence of question marks. Just like we did in our instaemail applications hardcode the questions (and answers) into arrays (please have at least five questions). When the user presses the button to display new question, the question should be chosen randomly. You can use function rand() to generate random integer. To get the random number in the range 0 to array size -1 you can use the following statement
rand() % (array_size)
where the (array_size) need to be replaced with the valid Objective-C statement that returns the number of elements in the array. Just like in Java or C you can use variable of type int to store the random number (or use NSInteger like in InstaEmail app). Make sure that you change the target from 6.0 to 5.1 to make it easier for me to test and grade your apps.

this is the assignment I have for my iOS class this semester so far it has been fun but I just ran into a problem the warning I receive is this
/Users/xcode/Documents/Questions/Questions/ViewController.m:28:90: Incompatible pointer to integer conversion sending 'NSInteger *' (aka 'int *') to parameter of type 'NSUInteger' (aka 'unsigned int');

viewercontroller.h file
//
//  ViewController.h
//  Questions
//
//  Created by xcode on 1/30/13.
//  Copyright (c) 2013 xcode. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController{
NSArray *questions_;
NSArray *answers_;
NSInteger *randomNumber_;
}

@property (nonatomic, retain) IBOutlet UILabel *labelQuestion;
@property (nonatomic, retain) IBOutlet UILabel *labelAnswer;

- (IBAction)displayQuestions:(id)sender;
- (IBAction)displayAnswers:(id)sender;
@end



viewcontroller.m file
//
//  ViewController.m
//  Questions
//
//  Created by xcode on 1/30/13.
//  Copyright (c) 2013 xcode. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController
@synthesize labelQuestion;
@synthesize labelAnswer;

- (void)viewDidLoad
{
    [super viewDidLoad];
	questions_ = [[NSArray alloc] initWithObjects:@"How many days are their in January?", @"Whos is the first fighter to win a UFC tournament?", @"What is the largest country in South America?", @"What country is said to be shaped like a boot?", @"What NFL team is the only team to go to four Super Bowls in a row and not win?", nil];
    answers_ = [[NSArray alloc] initWithObjects:@"31", "Royce Gracie", "Brazil", "Italy", "Buffalo Bills", nil];
}
    // Do any additional setup after loading the view, typically from a nib.
    - (IBAction)displayQuestions:(id)sender{
       randomNumber_ = rand() % [questions_ count];
        NSString *question = [NSString stringWithFormat:@"%@", [questions_ objectAtIndex:randomNumber_]];
        [labelQuestion setText:question];
             [labelAnswer setText:@"???"];
    }
        - (IBAction)displayAnswers:(id)sender{
            NSString *answer = [NSString stringWithFormat:@"%@", [answers_ objectAtIndex:randomNumber_]];
            [labelAnswer setText:answer];
    }

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end


Is This A Good Question/Topic? 0
  • +

Replies To: Objective C trying to generate random question from array

#2 PaC-mAn336  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 16-January 13

Re: Objective C trying to generate random question from array

Posted 30 January 2013 - 07:00 PM

Fixed the issue just need to figure out how I can get the two arrays to return the same value. I have a question in one array and the answer in another I want to randomly select a question that returns the correct answer
Was This Post Helpful? 0
  • +
  • -

#3 howetechnical  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 03-June 13

Re: Objective C trying to generate random question from array

Posted 03 June 2013 - 05:59 AM

View PostPaC-mAn336, on 30 January 2013 - 07:00 PM, said:

Fixed the issue just need to figure out how I can get the two arrays to return the same value. I have a question in one array and the answer in another I want to randomly select a question that returns the correct answer



If you're grabbing a rand off of two different arrays, they will always return different values. One way around this would be to either declare one multidimensional array where you have both the question and answer, or only perform your rand function on one multidimensional array (the second dimension would have an index value, or something like that) and your second array (answers) would be told to grab the same index value that the first one just grabbed.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1