I’m new to programming, I have some basic python programming from college, I am familiar with some of the OOP basics and would like some help with managing large amounts of integers. I have 88 of them. 7 will be used for capturing user input and the other 81 will be used for a specific calculation. Instead of writing the following code:
int currentPlace;
int futurePlace;
int speed;
int distance;
int place1 = 1;
int place2 = 2;
int place3 = 3;
// etc...
int place81 = 81;
And then later coming back to the integers and asking user defined questions such as:
NSLog(@"What place is the runner in?");
scanf("%i", ¤tPlace);
NSLog(@"What place does the runner finish in?");
scanf("%i", &futurePlace);
NSLog(@"What is the distance of the track?");
// doing some math
NSLog(@"The runner is running at "i" MPH.",speed);
I remember there being an easier way to use the integers but I keep thinking enums or typedefs.
I’d like for the user to pick a number and not have to run a huge if statement to get the work done to cut the size of the program as much as possible.
This is my first “on my own” application so any helpful pointers would be great.
Thanks.
I haven’t understood why you need all these
place‘s, but I also assume that an array would be easier to use here. You can use eitherNSArrayorNSMutableArray. The difference between them is that anNSArrayinstance can’t be changed after being created (you can’t add/remove elements) unlike anNSMutableArray.Using
NSArraynilat the end means the end of the contents of an array.[NSNumber numberWithInt:1]returns an int given as an argument (we can’t straight give an int to the array, as an array expects an object as an argument.You can access the contents of the array using:
Remeber that an array starts counting with 0, so if you want to get 5, you have to do this
Using
NSMutableArrayI suggest that you should use this option.
It’s easier to use
forhere.Then you can access data the same way as in the NSArray:
This will return 1. You can start the
for-cycle with 0. After that the index of an array will correspond to the integer inside, sowill actually return 5.