Please, can somebody help?


#1

Could somebody please explain to me how some Employees are assigned multiple assets?

For example, Employee 5 has a total of $136 assets, Latop 3 ($51) and Laptop 5 ($85).

Im looking at the following line of code for the employee to asset assignment:

//Assign the asset to the employee [randomEmployee addAssetsObject:asset];

I don’t see how it some employees are being assigned 2 laptops while others are being assigned 1.

My only guess comes from the following code:

//Get a random number between 0 and 9 inclusive NSUInteger randomIndex = random () % [employees count];

I’m under the impression that this code generates a “random” number from 0-9. Is this correct? If so, is it possible that the same number can be generated multiple times causing the current asset being enumerated to be assigned to an employee which was already given a previous Asset?

Also, if Asset with ResaleValue of $0 is only enumerated in main.m once, how is it that Employee 0, 4, and 8 all have $0 in assets?

If they are not selected via randomIndex do their asset values automatically defalt to $0?

Am I even on the right track here?

Thanks.


#2

That’s not quite correct.

NSUInteger randomIndex = random () % [employees count]; Will give you a random index between 0 and whatever the value [employees count] returns; that is

randomIndex >= 0 and randomIndex < [employees count]

Yes, you can get the same number multiple times in a row but that should be rare.


#3

Ibex, I really appreciate you taking the time to answer all these questions.

Going back to my original question, could you explain why some employees are assigned multiple assets, while others do not have any?

Once I figure this out, I can finally move from this chapter that’s been killing me for the past couple weeks.

Thanks so much.


#4

Hard to answer without seeing your code. Can you post the entire code?


#5

It’s the exact same code in the book. My results are the exact same as in the book as well.

Asset.h

[code]#import <Foundation/Foundation.h>

@interface Asset : NSObject

{
NSString *label;
unsigned int resaleValue;
}
@property (strong) NSString *label;
@property unsigned int resaleValue;
@end
[/code]

Asset.m

[code]#import “Asset.h”

@implementation Asset

@synthesize label, resaleValue;

  • (NSString *)description
    {
    return [NSString stringWithFormat:@"<%@: $%d >",
    [self label], [self resaleValue]];
    }

  • (void)dealloc
    {
    NSLog(@“dallocating %@”, self);
    }

@end
[/code]

Employee.h

[code]#import <Foundation/Foundation.h>
#import “Person.h”
@class Asset;

@interface Employee : Person
{
int employeeID;
NSMutableArray *assets;

}

@property int employeeID;

  • (void)addAssetsObject:(Asset *)a;
  • (unsigned int)valueOfAssets;

@end
[/code]

Employee.m

[code]#import “Employee.h”
#import “Asset.h”

@implementation Employee

@synthesize employeeID;

  • (void)addAssetsObject:(Asset *)a
    {
    if (!assets) {
    assets = [[NSMutableArray alloc] init];
    }
    [assets addObject:a];
    }

  • (unsigned int)valueOfAssets
    {
    unsigned int sum = 0;
    for (Asset *a in assets) {
    sum += [a resaleValue];
    }
    return sum;

}

  • (float)bodyMassIndex
    {
    float normalBMI = [super bodyMassIndex];
    return normalBMI * .9;
    }

  • (NSString *)description
    {
    return [NSString stringWithFormat:@"<Employee %d; $%d in assets>",
    [self employeeID], [self valueOfAssets]];
    }

  • (void)dealloc
    {
    NSLog(@“deallocating %@”, self);
    }

@end
[/code]

Result:

013-11-04 22:07:35.201 BMITime[860:303] Employees: (
"<Employee 0; $0 in assets>",
"<Employee 1; $153 in assets>",
"<Employee 2; $119 in assets>",
"<Employee 3; $68 in assets>",
"<Employee 4; $0 in assets>",
"<Employee 5; $136 in assets>",
"<Employee 6; $119 in assets>",
"<Employee 7; $34 in assets>",
"<Employee 8; $0 in assets>",
"<Employee 9; $136 in assets>"
)
2013-11-04 22:07:35.203 BMITime[860:303] Giving up ownership of one employee
2013-11-04 22:07:35.203 BMITime[860:303] deallocating <Employee 5; $136 in assets> [color=#FF0000]<~~~ why are some employees like Employee 5 assigned mutiple assets (Laptop 3 and Laptop 5 while others are assigned none? [/color]
2013-11-04 22:07:35.204 BMITime[860:303] dallocating <Laptop 3: $51 >
2013-11-04 22:07:35.204 BMITime[860:303] dallocating <Laptop 5: $85 >
2013-11-04 22:07:35.204 BMITime[860:303] Giving up owndership of array
2013-11-04 22:07:35.205 BMITime[860:303] deallocating <Employee 0; $0 in assets> [color=#FF0000]<~~ No assets assigned to Employee 0[/color]
2013-11-04 22:07:35.205 BMITime[860:303] deallocating <Employee 1; $153 in assets>
2013-11-04 22:07:35.205 BMITime[860:303] dallocating <Laptop 9: $153 >
2013-11-04 22:07:35.205 BMITime[860:303] deallocating <Employee 2; $119 in assets>
2013-11-04 22:07:35.206 BMITime[860:303] dallocating <Laptop 7: $119 >
2013-11-04 22:07:35.206 BMITime[860:303] deallocating <Employee 3; $68 in assets>
2013-11-04 22:07:35.206 BMITime[860:303] dallocating <Laptop 0: $0 >
2013-11-04 22:07:35.207 BMITime[860:303] dallocating <Laptop 4: $68 >
2013-11-04 22:07:35.207 BMITime[860:303] deallocating <Employee 4; $0 in assets> [color=#FF0000]<~~ No assets assigned to Employee 4[/color]
2013-11-04 22:07:35.207 BMITime[860:303] deallocating <Employee 6; $119 in assets>
2013-11-04 22:07:35.208 BMITime[860:303] dallocating <Laptop 1: $17 >
2013-11-04 22:07:35.208 BMITime[860:303] dallocating <Laptop 6: $102 >
2013-11-04 22:07:35.208 BMITime[860:303] deallocating <Employee 7; $34 in assets>
2013-11-04 22:07:35.208 BMITime[860:303] dallocating <Laptop 2: $34 >
2013-11-04 22:07:35.209 BMITime[860:303] deallocating <Employee 8; $0 in assets>
2013-11-04 22:07:35.209 BMITime[860:303] deallocating <Employee 9; $136 in assets>
2013-11-04 22:07:35.209 BMITime[860:303] dallocating <Laptop 8: $136 >
Program ended with exit code: 0

So I just wanna know how the assets are being assigned. Why do some employees have 1 or 2 assets assigned while others have none.

Thanks!


#6

Your code looks good, but you need to read the code in the book carefully.

It’s all happening inside the second for loop in main () on Page 121.
Looking at the last three statements in the for loop:

// create 10 assets
for (int i = 0; i < 10; ++i)
{
...
...
...
// Get a random number ...
NSUInteger randomIndex = random () % [employees count];

// Find that employee
Employee *randomEmployee = [employees objectAtIndex:randomIndex];

// Assign the asset to the employee
[randomEmployee addAssetsObject:randomEmployee];
}

Each time through the loop the value of randomIndex will be a random number between 0 and 10 ([0, 10) or [0, 9].) It can be the same number more than once. Therefore the randomEmployee can point to the same employee multiple times, and as a result some employees can have more than one asset and thus some can have none. There are 10 assets and 10 employees. If you assign an employee more than one asset, then there will be at least one employee with no assets.

Now to celebrate, you should at least research the meaning of the mathematical notations ([A, B) and [A, C]) which I have used above.


#7

Thank you ibex for clearing that up.

That is why I originally asked whether randomIndex can assign the same number mutiple times. You responded by saying that it can, but that it is rare; which left me wondering what else could be the issue.

Thanks for the clarification, it really is much appreciated!