# Solution to challenge with random entries

#1

This is my solution to the challenge, I added a bit of code to randomize the entries into the NSMutableArray.
The random number generator I got from http://stackoverflow.com/a/9310745 since I’m pretty new to Objective-C and couldn’t figure out how to add lower and upper bounds myself.

StockHolding.h

``````#import <Foundation/Foundation.h>

@interface StockHolding : NSObject
{
float purchaseSharePrice;
float currentSharePrice;
int numberOfShare;
}
@property float purchaseSharePrice;
@property float currentSharePrice;
@property int numberOfShares;

-(float)costInDollars;
-(float)valueInDollars;

@end``````

StockHolding.m

[code]#import “StockHolding.h”

@implementation StockHolding

@synthesize purchaseSharePrice, currentSharePrice, numberOfShares;

-(float)costInDollars
{
float psp = [self purchaseSharePrice];
return [self numberOfShares] * psp;
}

-(float)valueInDollars
{
float csp = [self currentSharePrice];
return [self numberOfShares] * csp;
}

@end
[/code]

main.m

[code]#import <Foundation/Foundation.h>
#import “StockHolding.h”

int main(int argc, const char * argv[])
{

``````@autoreleasepool {

NSMutableArray *stocks = [[NSMutableArray alloc] init];

for (int s = 0; s<4; s++) {

float low_bound = 0;
float high_bound = 200;
float rndValue = (((float)arc4random()/0x100000000)*(high_bound-low_bound)+low_bound);
float low_bound2 = 0;
float high_bound2 = 200;
float rndValue2 = (((float)arc4random()/0x100000000)*(high_bound2-low_bound2)+low_bound2);
float low_bound3 = 100;
float high_bound3 = 50000;
float rndValue3 = (((float)arc4random()/0x100000000)*(high_bound3-low_bound3)+low_bound3);
int intRndValue = (int)(rndValue3 + 0.5);

StockHolding *s = [[StockHolding alloc] init];

[s setNumberOfShares:intRndValue];
[s setPurchaseSharePrice];
[s setCurrentSharePrice:rndValue2];

}
int pointer = 1;
for (StockHolding *stk in stocks)
{
NSLog(@"\nCurrent value of stock%i is \$%.2f", pointer, [stk currentSharePrice]);
NSLog(@"\nThe original value of stock%i was \$%.2f", pointer,[stk purchaseSharePrice]);
NSLog(@"\nNumber of shares for stock%i: %i.", pointer,[stk numberOfShares]);
NSLog(@"\nCost in dollars of stock %i: \$%.2f", pointer,[stk costInDollars]);
NSLog(@"\nValue in dollars of stock %i: \$%.2f", pointer,[stk valueInDollars]);
NSLog(@"\nThe difference of stock%i is: \$%.2f", pointer,[stk valueInDollars] - [stk costInDollars]);
pointer++;
}

}
return 0;
``````

}[/code]

Hope you find this useful,

#2

I think your generation of random numbers can be simplified to this…

``````float rndValue  = ( (float)arc4random() / 0x100000000 ) * 200.0;      // result = random float between 0.0 - 200.0
float rndValue2 = ( (float)arc4random() / 0x100000000 ) * 200.0;      // result = random float between 0.0 - 200.0
int rndValue3   = ( arc4random() % 49900 ) + 100;                     // result = random int between 100 - 50,000``````

Only the first two price values need to be floats; the number of shares value can be an integer, so you don’t need to go through any contortions to get it. Also, the lowBound of the price values is 0.0, so it can be safely removed from your calculations.

I didn’t run this code to verify it would work. I leave that to you as an exercise, should you wish to try it.

#3

I got the random solution that @calwas proposed to work. Thanks!

My question is about the creation of the NSMutable Array instance. I’m not completely sure that this is the way that it is supposed to be done. Also, I kinda get the impression from the book that we should be using self in some way to pass addresses back and forth. So, yeah, this code is working, but I have the suspicion that there is a better, more proper, way to do it.

Here is what I have.

main.m

``````int main(int argc, const char * argv[])
{

@autoreleasepool {

// Create an Instance of StockHolding
NSMutableArray *thePortfolio = [[NSMutableArray alloc] init];

for (int s = 0; s < 3; s++)
{
StockHolding *s = [[StockHolding alloc] init];

float rndValue  = ( (float)arc4random() / 0x100000000 ) * 200.0;      // result = random float between 0.0 - 200.0
float rndValue2 = ( (float)arc4random() / 0x100000000 ) * 200.0;      // result = random float between 0.0 - 200.0
int rndValue3   = ( arc4random() % 199 ) + 1;                     // result = random int between 1 - 200

[s setPurchaseSharePrice];
[s setCurrentSharePrice:rndValue2];
[s setNumberOfShares:rndValue3];

}

for (StockHolding *h in thePortfolio) {
NSLog(@"The purchase price was \$%.2f", [h purchaseSharePrice]);
NSLog(@"The current price is \$%.2f", [h currentSharePrice]);
NSLog(@"That means you have netted \$%.2f with your %d shares.", [h netProfit], [h numberOfShares]);
}

}
return 0;
}``````

StockHolding.m

``````@interface StockHolding : NSObject
{
float purchaseSharePrice;
float currentSharePirce;
int numberOfShares;
}
@property float purchaseSharePrice, currentSharePrice;
@property int numberOfShares;

// Calculate Purchase Coast in Dollars
- (float)costInDollars;

// Calculate Current Value in Dollars
- (float)valueInDollars;

// Calculates the Net Profit multiplied by the Number of Shares
- (float)netProfit;

@end``````

StockHolding.m

``````@implementation StockHolding

@synthesize purchaseSharePrice, currentSharePrice, numberOfShares;

- (float)costInDollars
{
return [self purchaseSharePrice] * [self numberOfShares];
}

- (float)valueInDollars
{
return [self currentSharePrice] * [self numberOfShares];
}

- (float)netProfit
{
return ([self currentSharePrice] - [self purchaseSharePrice]) * [self numberOfShares];
}

@end``````