Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

buldre

macrumors newbie
Original poster
Jan 25, 2010
28
0
Hello!

I am in need of a simple c style 2-dim array to use for a small function. And i need to initzialize the whole array with zeroes (the int value 0)

my function is as follows;

Code:
-(void) function: (NSMutableArray *) a: (NSMutableArray *) b;

Optimally, I want a double two dimmensional array like this;

double matrix[[a count]+1][[b count]+1];

i tried to attach ={0}; behind the declaration, but this doesnt work.

any ideas?
 
I think you might need something like:

Code:
NSMutableArray *rowList = [[NSMutableArray alloc] init];
NSMutableArray *colList = [[NSMutableArray alloc] init];

//Fill your column array
[colList addObject:obj1];
[colList addObject:obj2];

//Add column array to your rowlist array
[rowList addObject:colList];

// Fill your output array with each array within rowlist loop
NSMutableArray *output1 = [[NSMutableArray alloc] initwitharray:[rowList objectAtIndex:0];

//Get your values
int num1 = [[output1 objectAtIndex:0] intValue];
int num2= [[output1 objectAtIndex:1] intValue];

This is the way i do multi dimensional arrays maybe an easier method.
 
Hmm, not quite what i was looking for.

The two arrays that i get as variables to my function stores a huge amount of float values. What im going to do is to fill out this new double matrix with these float values, and then perform a range of mathematical procedures on it. Therefore it would be very nice if I could keep the simple C structure on my arrays. The only thing i need is to initzialise the arrrays with Zeros now...
 
You could use the method i posted previously, you need to use objects so fill the arrays with NSNumber *num = [NSNumber numberWithFloat:0];

then when you pull it out do [[arr objectAtIndex:i] floatValue];
 
ah, ok. What is the advantage of using this method compared to ;

Code:
int length =[a count]+1;
int length2=[b count]+1;

double arr[length][length2];
	
		// initialize array.
	for( i = 0; i < length; i++)
		for( j = 0; j < length2; j++)
			arr[i][j] = 0;

Another question i have in that case is;

In c# i would normally write something like this to find the maximum value of an array, where a and b are; double a[], double b[]. Is there an equivilant in obj c?

Code:
double S = Math.Max((a.Max() - b.Min()), (b.Max() - a.Min()));
 
Yeah ive looked through the documentation, unsuccessfully though :/

I haven't been working with Cocoa and objective-c for long, but one of the things im experiencing is that i feel that i dont have the control i need over my objects. The API is nice and understandable, but for me who have my main experience in c/c++ and bit of assembly, programmin in c# was a hassle with alle the libraries and API's, and i feel that Obj-c is even worse than this, hehe..

By the way, in this framework, if i make a class function that has the name "max" for instance, and takes some parameters, how do i call that method from another class method of the same class? I cant get this working..

Anyways, when it comes to solving the max / min probem i guess this could work;

Code:
	for (i=0; i<[a count]; i++){
		NSDecimalNumber *test;
		test = [a objectAtIndex:i];
		float temp = [test floatValue];
		if(temp > maxval)
			maxval =temp;
	}

or what do you think? is there an easier way? the above code supports my theory that this framework is not meant for mathematical procedures. In the context of my masterthesis im making a gesture recognition application for Iphone you see :)
 
Ooo nice, I was thinking in the future about going for a masters :p

If you mean calling a method from within your class

[self methodName:param1];

or if your calling it from another, as if you initialized the method like so:
+ (int) max:(nsmutablearray*) arr;

[Class_Name max:theArray];
 
In theory it looks like that would work. I never really had to use min/max in objective c with arrays, so I never really noticed there wasn't built in methods for this. Which is weird to me lol.
 
Thanks mate, "self" did the trick :)

Yeah, this is the last semester of my master in information security. If you have a BSc, you should definately head for a MSc. An MSc isnt just nice to have on your resumee, but it gives you a much higher level of knowledge in the area your studying, i really recommend checking it out!

I kinda regret the choice of my masterthesis as it involves alot of cocoa and objC programming, but i guess i'll manage! At least it helps alot when you have some forums to ask around on :) The most annoying part is that is that i sit here and know exactly the code i want to write, but the API is just totally owning me, heh :)

oh, btw. Should you ever have use for such functions, heres the ones i ended up with;

Code:
-(double) maxfunc: (NSMutableArray *) a{
	double maxval,temp=0;
	int i;
	for (i=0; i<[a count]; i++){
		NSDecimalNumber *test;
		test = [a objectAtIndex:i];
		temp = [test floatValue];
			if(temp > maxval)
				maxval=temp;
	}
	
	return maxval;
	
}

-(double) minfunc: (NSMutableArray *) a{
	
	double minval,temp=100;
	int i;
	for (i=0; i<[a count]; i++){
		NSDecimalNumber *test;
		test = [a objectAtIndex:i];
		temp = [test floatValue];
			if(temp < minval)
				minval =temp;
	}
	return minval;
	
}
 
Lol i know what you mean. I work in mostly legacy development and development that is not OOP so Coming from doing that all day at work to a OOP environment that I have not really seen since college is a bit rough. getting back into using Classes and all these OOP standards is a bit rough.

I do know what I want to do while I'm working on a project but knowing the best method and how is what I'm trying to figure out. The memory management stuff took me a little while to figure out and understand.
 
If you have any interest in efficiency you'll use fast enumeration.

Code:
-(double) maxfunc: (NSArray *) a{
	double maxval = 0,temp=0;
	for (NSDecimalNumber *test in a){
		temp = [test floatValue];
			if(temp > maxval)
				maxval=temp;
	}
	
	return maxval;
}
 
You might also consider creating these functions (with some tweaking to make them more generic) as part of a category on NSArray.

I'm sorry, but i didnt quite understand what you meant? Do you mean like creating it as a class function for the Nsarray class? How can one do this? im making more of these "math " functions nowadays, so it would be nice to have them available like that!
 
I'm sorry, but i didnt quite understand what you meant? Do you mean like creating it as a class function for the Nsarray class? How can one do this? im making more of these "math " functions nowadays, so it would be nice to have them available like that!
Categories in Objective-C let you add methods (whether they be class methods or instance methods) to a class, even classes that you could not normally change, like NSString or NSArray . They're fairly easy to set up. See this blog post to get you started.
 
If the arrays are large and performance is any part of your requirement, do not use Objective C arrays or objects in your inner loops. Obj C is completely unsuitable (until someone implements a lot more sophisticated compiler optimizations). The message passing overhead will consume the vast majority of your loop time. Compare it by benchmarking huge arrays if you don't believe me.

Use 1 dimensional C arrays:

float *a;
a = malloc(numRows * numCols * sizeof(float));
// check for a null array & don't forget to free a non-null array when done

item = a[myRow * numCols + myCol];
// if the compiler doesn't do it for you, hoisting the row multiply out of the inner loops is left as an exercise for the student.

Obj C was designed to be good for RAD code reuse, not for performance of number crunching. Use the object message paradigm only where it will buy you something; consider straight C otherwise.
 
To further optimize this code see this page for info:

http://www.mulle-kybernetik.com/artikel/Optimization/

BTW, in the implementations posted of maxfunc and minfunc the min and max values aren't initialized, which is obviously wrong. I initialized maxval to zero in the code I posted but this is probably wrong also. These values should be initialized to either the minimum or maximum possible values or they should be initialized to the first value in the input array. It should also take into account the possibility of an empty array.
 
To further optimize this code see this page for info:

http://www.mulle-kybernetik.com/artikel/Optimization/

BTW, in the implementations posted of maxfunc and minfunc the min and max values aren't initialized, which is obviously wrong. I initialized maxval to zero in the code I posted but this is probably wrong also. These values should be initialized to either the minimum or maximum possible values or they should be initialized to the first value in the input array. It should also take into account the possibility of an empty array.

As for the initialations; this was simply a misswrite, in many other languages you can initialize two variables like that, at least when they are decleared right before...

If the arrays are large and performance is any part of your requirement, do not use Objective C arrays or objects in your inner loops. Obj C is completely unsuitable (until someone implements a lot more sophisticated compiler optimizations). The message passing overhead will consume the vast majority of your loop time. Compare it by benchmarking huge arrays if you don't believe me.

Use 1 dimensional C arrays:

float *a;
a = malloc(numRows * numCols * sizeof(float));
// check for a null array & don't forget to free a non-null array when done

item = a[myRow * numCols + myCol];
// if the compiler doesn't do it for you, hoisting the row multiply out of the inner loops is left as an exercise for the student.

Obj C was designed to be good for RAD code reuse, not for performance of number crunching. Use the object message paradigm only where it will buy you something; consider straight C otherwise.

I bellive you, but the problem is that i need the structure of a two dimensional matrix to do my math. as you point out, objC isnt made for number crunching, thats ok, but since objC is the only language i can use on the iPhone, i dont really have another option. Thanks for the tips though!

Adding a question here:

ok, so i have an nsmutablearray with nsdecimalnumbers in it. What i want to do, is to build a file locally on the Iphone that is readable in plaintext, and has 1 number on each line. (this means that im going to loop through the NSMutableArray, and for each NSDecimalNumber in it, i want to have one line in the file.

Ok, then after i have made this file, i need to get a hold of it on my mac, i.e. extract it from the iphone. I have found that one can upload to FTP server using objective-c. but i cannot understand how i do this, could someone please help me with some example code? this is very easy in C# for instance, and it is probably just as easy here, just that i dont understand how i do it.

all help is GREATLY appreciated!! :)
 
I bellive you, but the problem is that i need the structure of a two dimensional matrix to do my math. as you point out, objC isnt made for number crunching, thats ok, but since objC is the only language i can use on the iPhone, i dont really have another option. Thanks for the tips though!

What on earth makes you think you can't use straight C on the iphone?
C++ *might* compile C code
Obj-C *will* compile C code.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.