Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Google Sign In with OpenID
Please do not post the same thing multiple times. The board software automatically flags certain posts as needing moderator attention. This happens the most often for new users. I'm pretty sure this is made clear at the time you attempt to post. Posting the same thing over and over again just makes that many more posts the moderators have to weed through later. This makes us sad. Don't make us sad. If your post/thread doesn't appear, just wait a while. Don't post it again. If it hasn't shown up by the next day, then you can try again. I normally go through posts in the mornings, and try to check a few times throughout the day, but I'm not here 24/7. There will typically be a significant delay before posts are approved. Just be patient.

Pick random number from array + running total

KillbillKillbill Posts: 15Registered Users
Hi,
I'm stuck....
I'm trying to make an app which upon button click picks random number from an array. Upon next button click it add up the values (running total of picked numbers).
I get an unused varibale 'numberPicker' and EXC_ARITHMETIC error and i don't understand why.
Help is much apprecriated!

In the .h:

IBOutlet UILabel *anumber;
IBOutlet UILabel *bnumber;
IBOutlet UILabel *cnumber;
IBOutlet UILabel *dnumber;
NSMutableArray *randomB;
NSMutableArray *numberPicker;

}



-(IBAction)Calculatenow;





In the .m:



float VarA = 0.00;
float VarB = 0.00;
float VarC = 0.00;
float VarD = 0.00;
float result = 0.00;


@implementation DataViewController

@synthesize dataObject = _dataObject;





-(IBAction) Calculatenow{
result = [[numberPicker objectAtIndex:arc4random() % [numberPicker count]] floatValue];

VarA = VarC;
VarB = result;
VarC = VarA + VarB;


anumber.text = [[NSNumber numberWithFloat:VarA] stringValue];
bnumber.text = [[NSNumber numberWithFloat:VarB] stringValue];
cnumber.text = [[NSNumber numberWithFloat:VarC] stringValue];
dnumber.text = [[NSNumber numberWithFloat:VarD] stringValue];
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
anumber.text = @\"0.00\";
bnumber.text = @\"0,00\";
cnumber.text = @\"0.00\";
dnumber.text = @\"0.00\";





[super viewDidLoad];

NSMutableArray *numberPicker = [NSArray arrayWithObjects:
[NSNumber numberWithFloat:10.00],
[NSNumber numberWithFloat:2.56],
[NSNumber numberWithFloat:4.25],
[NSNumber numberWithFloat:1.95],
nil];





}

Post edited by Killbill on

Replies

  • Duncan CDuncan C Posts: 8,015Tutorial Authors, Registered Users
    Killbill;400964 said:
    Hi,
    I'm stuck....
    I'm trying to make an app which upon button click picks random number from an array. Upon next button click it add up the values (running total of picked numbers).
    I get an unused varibale 'numberPicker' and EXC_ARITHMETIC error and i don't understand why.
    Help is much apprecriated!

    In the .h:

    IBOutlet UILabel *anumber;
    IBOutlet UILabel *bnumber;
    IBOutlet UILabel *cnumber;
    IBOutlet UILabel *dnumber;
    NSMutableArray *randomB;
    NSMutableArray *numberPicker;

    }



    -(IBAction)Calculatenow;





    In the .m:



    float VarA = 0.00;
    float VarB = 0.00;
    float VarC = 0.00;
    float VarD = 0.00;
    float result = 0.00;


    @implementation DataViewController

    @synthesize dataObject = _dataObject;





    -(IBAction) Calculatenow{
    result = [[numberPicker objectAtIndex:arc4random() % [numberPicker count]] floatValue];

    VarA = VarC;
    VarB = result;
    VarC = VarA + VarB;


    anumber.text = [[NSNumber numberWithFloat:VarA] stringValue];
    bnumber.text = [[NSNumber numberWithFloat:VarB] stringValue];
    cnumber.text = [[NSNumber numberWithFloat:VarC] stringValue];
    dnumber.text = [[NSNumber numberWithFloat:VarD] stringValue];
    }

    - (void)didReceiveMemoryWarning
    {
    [super didReceiveMemoryWarning];
    // Release any cached data, images, etc that aren't in use.
    }

    #pragma mark - View lifecycle

    - (void)viewDidLoad
    {
    anumber.text = @\"0.00\";
    bnumber.text = @\"0,00\";
    cnumber.text = @\"0.00\";
    dnumber.text = @\"0.00\";





    [super viewDidLoad];

    NSMutableArray *numberPicker = [NSArray arrayWithObjects:
    [NSNumber numberWithFloat:10.00],
    [NSNumber numberWithFloat:2.56],
    [NSNumber numberWithFloat:4.25],
    [NSNumber numberWithFloat:1.95],
    nil];





    }



    You need to read up on variable scope, and on Cocoa memory management.

    You've defined an instance variable numberPicker. Then in your viewDidLoad method, you create a local variable which is also called numberPicker. That local variable gets forgotten when you exit your viewDidLoad method (the local variable "goes out of scope".)

    You should change your instance variable numberPicker to a retained property (or a strong property, if you're using ARC):

    @property (nonatomic, retain) NSMutableArray *numberPicker;

    or, for ARC applications:

    @property (nonatomic, strong) NSMutableArray *numberPicker;

    You need to change that line to assign a value to your property instead of creating a local variable.

      self.numberPicker = [NSArray arrayWithObjects:
    [NSNumber numberWithFloat:10.00],
    [NSNumber numberWithFloat:2.56],
    [NSNumber numberWithFloat:4.25],
    [NSNumber numberWithFloat:1.95],
    nil];



    Then you need to set the numberPicker property to nil in your viewDidUnload method:

    self.numberPicker = nil;


    There may be other problems with your code as well. Like I said, you need to stop and learn about memory management and the fundamental differences between local variables, instance variables, and properties, before you do anything else. Until you understand those concepts, you are going to be lost, and your code will crash all over the place.
    Regards,

    Duncan C
    WareTo

    mug

    Animated GIF created with Face Dancer, available for free in the app store.
  • KillbillKillbill Posts: 15Registered Users
    Thank you. I got it working.
    I think you right on the memory issue. It doesn't always produce
    a result... maybe 3 out of 10 button clicks.
    Does this have to do with the arc4random? Does that make it sluggish?
    Thank you for your response.
  • Duncan CDuncan C Posts: 8,015Tutorial Authors, Registered Users
    Killbill;400989 said:
    Thank you. I got it working.
    I think you right on the memory issue. It doesn't always produce
    a result... maybe 3 out of 10 button clicks.
    Does this have to do with the arc4random? Does that make it sluggish?
    Thank you for your response.
    The arc4Random method will return thousands of results a second. If you're using it as part of the calculation on every pixel on the screen, it will make things slow. If you're calling it a couple of dozen times in response to a user click, you would not see any visible delay.

    Post your code. It sounds like you still have problems with it.
    Regards,

    Duncan C
    WareTo

    mug

    Animated GIF created with Face Dancer, available for free in the app store.
  • KillbillKillbill Posts: 15Registered Users
    Thank you for your patience!

    In the .h
    @interface DataViewController : UIViewController{


    IBOutlet UILabel *anumber;
    IBOutlet UILabel *bnumber;
    IBOutlet UILabel *cnumber;
    IBOutlet UILabel *dnumber;
    NSMutableArray *numberPicker;

    }



    -(IBAction)Calculatenow;


    @property (strong, nonatomic) id dataObject;
    @property (nonatomic, retain) NSMutableArray *numberPicker;
    @end


    In the .m

    @synthesize dataObject = _dataObject;
    @synthesize numberPicker;




    -(IBAction) Calculatenow{
    result = [[numberPicker objectAtIndex:arc4random() % [numberPicker count]] floatValue];


    VarB = result;
    VarC = VarA + VarB;
    VarA = VarC;


    anumber.text = [[NSNumber numberWithFloat:VarA] stringValue];
    bnumber.text = [[NSNumber numberWithFloat:VarB] stringValue];
    cnumber.text = [[NSNumber numberWithDouble:VarC] stringValue];
    dnumber.text = [[NSNumber numberWithDouble:VarD] stringValue];


    }

    - (void)didReceiveMemoryWarning
    {
    [super didReceiveMemoryWarning];
    // Release any cached data, images, etc that aren't in use.
    }

    #pragma mark - View lifecycle

    - (void)viewDidLoad
    {

    self.numberPicker = [NSArray arrayWithObjects:
    [NSNumber numberWithFloat:10.01],
    [NSNumber numberWithFloat:2.56],
    [NSNumber numberWithFloat:4.25],
    [NSNumber numberWithFloat:1.95],
    nil];



    anumber.text = @\"0.00\";
    bnumber.text = @\"0.00\";
    cnumber.text = @\"0.00\";
    dnumber.text = @\"0.00\";





    [super viewDidLoad];










    // Do any additional setup after loading the view, typically from a nib.
    }

    - (void)viewDidUnload

    {
    self.numberPicker = nil;
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
    }

    - (void)viewWillAppear:(BOOL)animated
    {
    [super viewWillAppear:animated];

    }

    - (void)viewDidAppear:(BOOL)animated
    {
    [super viewDidAppear:animated];
    }

    - (void)viewWillDisappear:(BOOL)animated
    {
    [super viewWillDisappear:animated];
    }

    - (void)viewDidDisappear:(BOOL)animated
    {
    [super viewDidDisappear:animated];
    }

    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
    {
    // Return YES for supported orientations
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
    }

    @end
  • Duncan CDuncan C Posts: 8,015Tutorial Authors, Registered Users
    Killbill;401008 said:
    Thank you for your patience!

    In the .h
    @interface DataViewController : UIViewController{


    IBOutlet UILabel *anumber;
    IBOutlet UILabel *bnumber;
    IBOutlet UILabel *cnumber;
    IBOutlet UILabel *dnumber;
    NSMutableArray *numberPicker;

    }



    -(IBAction)Calculatenow;


    @property (strong, nonatomic) id dataObject;
    @property (nonatomic, retain) NSMutableArray *numberPicker;
    @end


    In the .m

    @synthesize dataObject = _dataObject;
    @synthesize numberPicker;




    -(IBAction) Calculatenow{
    result = [[numberPicker objectAtIndex:arc4random() % [numberPicker count]] floatValue];


    VarB = result;
    VarC = VarA + VarB;
    VarA = VarC;


    anumber.text = [[NSNumber numberWithFloat:VarA] stringValue];
    bnumber.text = [[NSNumber numberWithFloat:VarB] stringValue];
    cnumber.text = [[NSNumber numberWithDouble:VarC] stringValue];
    dnumber.text = [[NSNumber numberWithDouble:VarD] stringValue];


    }

    - (void)didReceiveMemoryWarning
    {
    [super didReceiveMemoryWarning];
    // Release any cached data, images, etc that aren't in use.
    }

    #pragma mark - View lifecycle

    - (void)viewDidLoad
    {

    self.numberPicker = [NSArray arrayWithObjects:
    [NSNumber numberWithFloat:10.01],
    [NSNumber numberWithFloat:2.56],
    [NSNumber numberWithFloat:4.25],
    [NSNumber numberWithFloat:1.95],
    nil];



    anumber.text = @\"0.00\";
    bnumber.text = @\"0.00\";
    cnumber.text = @\"0.00\";
    dnumber.text = @\"0.00\";





    [super viewDidLoad];










    // Do any additional setup after loading the view, typically from a nib.
    }

    - (void)viewDidUnload

    {
    self.numberPicker = nil;
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
    }

    - (void)viewWillAppear:(BOOL)animated
    {
    [super viewWillAppear:animated];

    }

    - (void)viewDidAppear:(BOOL)animated
    {
    [super viewDidAppear:animated];
    }

    - (void)viewWillDisappear:(BOOL)animated
    {
    [super viewWillDisappear:animated];
    }

    - (void)viewDidDisappear:(BOOL)animated
    {
    [super viewDidDisappear:animated];
    }

    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
    {
    // Return YES for supported orientations
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
    }

    @end
    It seems like that code should work. I would suggest adding a breakpoint at the beginning of your IBAction and stepping through your code to figure out what's going on.

    If you don't know how to step through your code in the debugger, you should learn. In the meantime, add log statements that display the new random value as well as the values of the variables that you are displaying.
    Regards,

    Duncan C
    WareTo

    mug

    Animated GIF created with Face Dancer, available for free in the app store.
  • KillbillKillbill Posts: 15Registered Users
    Hi,
    Spent some hours trying to find the problem without any success...
    In simulator, after clicking the button 5 times without any result, the sixth time it produces six results. On my device it doesn't produce anything.
    I uploaded the project here in case you want to have a look.
    http://wtrns.fr/hdRAAah6Iqc_ag

    Thanks
Sign In or Register to comment.