Adding together vector array functions - objective-c

This code works as intended, giving me the number from these 10 input fields. Now, I want them all added together to one number. (all the miTimesFrac results.) Any really effective way of doing this?
double fraction[10] = {methane, ethane, propane, nbutane, ibutane,npentane,ipentane,nhexane,nitrogen,oxygen};
double Mi[10] = {16.0426,30.064,44.0962,558.123,558.123,72.1498,72.1498,86.1766,28.0134,31.9988};
double miTimesFrac[10];
for (int i=0; i<10; ++i) miTimesFrac[i] = Mi[i] * fraction[i];
I've tried a few things, like this
double TMiTimesFrac;
for (int i=1; i<10; ++i) TMiTimesFrac = miTimesFrac[i]+miTimesFrac[i-1];
This returns the same value every time tho. Any functions i'm missing?

This:
double TMiTimesFrac;
for (int i=1; i<10; ++i)
TMiTimesFrac = miTimesFrac[i]+miTimesFrac[i-1];
Doesn't do the job, because you don't keep track of all the previous values of TMiTimesFrac, so that only the last value (miTimesFrac[9]+miTimesFrac[8]) contributes to the final value of TMiTimesFrac.
So first of all initialize TMiTimesFrac to zero. Then add every value to it:
double TMiTimesFrac= 0.0;
for(unsigned int i=0; i<10; i++)
{
TMiTimesFrac+=miTimesFrac[i];
}
Now you should be able to do it without pasting the code. I suggest to try it.

Related

Writing Hex To Console Using NSString

I'm trying to write all the hex values, from 0 - 255 in a for loop. It's not printing in hex. I think the %x is what is causing the issue.
int i=0;
for (i = 0; i < 256; i++)
{
[_d writeXYZ:[NSString stringWithFormat:#"%x", i]];
}
Does anyone know the proper syntax to display 0-255 in hex?
When I do it like this:
int i=0;
for (i = 0; i < 256; i++)
{
NSLog(#"%x", i);
}
It runs perfectly, so it looks like my issue is within NSString.

Dividing an array into separate arrays of four elements plus the reminder

I'm trying to divide an array into individual arrays of four elements, where the last array will contain the reminder. For example, if that main array's length property will be ten, three subarrays will be created - two consisting of four elements, and one of two elements.
The code I have right now looks like the following:
NSMutableArray *mainMutableArray = [NSMutableArray arrayWithObjects:#"First", #"Second", #"Third", #"Fourth", #"Fifth", #"Sixth", #"Seventh", #"Eighth", nil];
NSMutableArray *mutableArrayOfSubarrays = [NSMutableArray array];
int length = mainMutableArray.count / 4;
int location = 0;
for (int i = 0; i < length; i++)
{
[mutableArrayOfSubarrays addObject:[mainMutableArray subarrayWithRange:NSMakeRange(location, 4)]];
location += 4;
}
This of course works only when the reminder is equal to 0.
Any help would be greatly appreciated.
Ok, here we go:
int length = mainMutableArray.count;
for (int location = 0; location < length; location+=4)
{
unsigned int Size=length-location;
if (Size>4) Size=4;
[mutableArrayOfSubarrays addObject:[mainMutableArray subarrayWithRange:NSMakeRange(location, Size)]];
}
If you use a while loop, you can make the condition describe what you are actually trying to do:
NSUInteger length = [mainMutableArray count];
NSUInteger location = 0;
// Until the location is less than four away from the end
while( location <= (length - 4) ){
[mutableArrayOfSubarrays addObject:[mainMutableArray subarrayWithRange:NSMakeRange(location, 4)]];
location += 4;
}
// Pick up the remainder, if any
if( location != length ){
[mutableArrayOfSubarrays addObject:[mainMutableArray subarrayWithRange:NSMakeRange(location, length-location)]];
}
Loop from length*4 to mainMutableArray.count to get the remainder of the array.

Reusing a variable name as for loop index doesn't produce the error I expect

Am I allowed to re-use the same variable name in different for loops in Objective-C? For example:
// This doesn't give me an error but I feel like it should:
for(int i = 0; i < 10; i++){
//do something
}
for (int i = 0; i < 5; i++){ // I'm using "i" again. Is this allowed?
//do something else
}
This compiles and seems to run fine, but I just want to make sure that this is legal and allowed without causing some sort of complication in my program. I'm newish to ObjC, but in Java I normally would get errors from this.
That should be fine. The scope of i in the snippet you show is limited to each of the for loops, so there's no conflict. If you instead do it like this:
int i;
for (i = 0; i < 2; i++) {
//...
}
int i;
for (i = 5; i < 10; i++) {
//...
}
then you'll have a problem because you're declaring i twice in the same scope.
This is perfectly fine. Because you doesn't initialize the integer outside of the for loop. If you had two times this:
int i;
for (i = 0; ...)
then you would of course get a compile error, because you can't define two variables with the same name in the same block.

Shuffling an array of NSNumbers & converting back to ints

I have an NSMutableArray urlArray of size n, I want to randomly choose 4 of these URLs from the total number of elements in the array.
However I don't want to shuffle urlArray directly, I'd prefer to make an "indexArray" [0 ... (n-1)] & shuffle these, & then use the first 4 elements of the shuffled indexArray to decide which elements I choose from urlArray.
First off I created the indexArray as follows:
for (int i = 0; i < numberOfStems; i++) {
[indexArray addObject:[NSNumber numberWithInteger:i]];
}
This allowed me to shuffle my indexArray, so far so good. Because I used the
[NSNumber numberWithInteger:i] method, the elements in the shuffled indexArray are NSNumbers.
Is there a way to convert the NSNumber objects in indexArray into ints?
I attempted to use the intValue function but this didn't appear to be what I needed.
I also tried creating a c style array but that wasn't so successful either - I'd like to stick with objective-c syntax if possible.
Any ideas? any hints appreciated :)
Why don't you just create a normal c array, shuffle that and then use the first four integers in the array as the for random index?
something like
int* index = malloc(numberOfStems*sizeof(int));
for (int i = 0; i < numberOfStems; ++i)
{
index[i] = i;
}
for (int i = numberOfStems - 1; i > 0; --i)
{
int randomIndex = arc4random() % i;
int tmp = index[i];
index[i] = index[randomIndex];
index[randomIndex] = tmp;
}
now use index to access the URL's
EDITED: updated algorithm (although not really related to OP question)
For a temporary array that stores only integers and gets thrown away after a relatively short task I would definitely prefer a C-style array: this would avoid a great deal of overhead, and is also simple to read.
int *array = (int*)malloc(sizeof(int)*numberOfStems);
for (int i = 0 ; i != numberOfStems ; i++) {
array[i] = i;
}
// Do the shuffle
// Pick first four, and do whatever you need to do
// ...
// Now that you are done with the array, do not forget to free it:
free(array);

Objective-C - Loop played but loop condition is false

I'm trying to convert a sectionned table into a flat list using this function into didSelectRowAtIndexPath (I have a NSArray that is initialisated with the number of items contained in each section) :
Somewhere... :-)
self.sectionsArray = [NSArray arrayWithObjects:macroIntNumber(1), macroIntNumber(3), macroIntNumber(12), nil];
then into didSelectRowAtIndexPath :
int selectedRow = 0;
int a = indexPath.section;
for (int i=0; i<indexPath.section-1; i++) {
selectedRow += [[self.sectionsArray objectAtIndex:i] intValue];
}
selectedRow += indexPath.row;
But... This crashes for indexPath.section = 0 (first section).
Because the loop is played infinitly until crash of the NSArray call...
Strange !!!
forcing for (int i=0; i<0-1; i++) { works
forcing for (int i=0; i<a-1; i++) { works
What am I missing ?
section is an NSUInteger, so it's unsigned. Thus, subtracting 1 from 0 on that unsigned integer is taking you to a very large number rather than -1.
It works when using a, because you've declared a as an int. :)