View unanswered posts | View active topics It is currently Fri Oct 24, 2014 4:33 pm






Reply to topic  [ 7 posts ] 
Array as function parameter? 
Author Message
Rookie

Joined: Thu May 29, 2008 4:46 am
Posts: 3
Post Array as function parameter?
Hi,

I need matrix functions for a bachelor thesis concerning special calculations on a NXT. As I couldn't find any RobotC headers for matrix funtions in the web, I decided to write my own one. However, I didn't find a solution to use arrays as function parameters. Every time I get an error message.

Is there any method to pass an array to a function?

Perhaps I could use the pointer of the array and cast it afterwards but I don't know how to do it in RobotC.
I'm an experienced programmer, so please tell me whatever ideas you have no matter how complex they are.
Thank you.

P.S.: In previous articles some of you sugested to wrap the array in a struct-statement. The problem with this way is that you have to define a specific array size.


Thu May 29, 2008 10:36 am
Profile
Guru
User avatar

Joined: Sat Mar 01, 2008 12:52 pm
Posts: 1030
Post 
hi,
I got an idea by defining a structure containing an array, but I don't know if it's the ultimate way, and I still have not tested the code.
Maybe there will be a much easier way...

Code:
typedef struct {
 int value[3];} tArray;

void array_test(tArray &t) {
   int j;

   for (j=0;j<3;j++) {t.value[j]=j+1;}
 }

task main() {
  tArray myArray;
  int i;


  eraseDisplay();

  for (i=0;i<3;i++) {
   myArray.value[i]=0;
   nxtDisplayTextLine(i, "%3d",myArray.value[i]);
  }

  array_test(myArray);


  for (i=0;i<3;i++) {
   nxtDisplayTextLine(i+4, "%3d",myArray.value[i]);
  }
  while (true) {;} // pause
}


_________________
regards,
HaWe aka Ford
#define S sqrt(t+2*i*i)<2
#define F(a,b) for(a=0;a<b;++a)
float x,y,r,i,s,j,t,n;task main(){F(y,64){F(x,99){r=i=t=0;s=x/33-2;j=y/32-1;F(n,50&S){t=r*r-i*i;i=2*r*i+j;r=t+s;}if(S){PutPixel(x,y);}}}while(1)}


Last edited by Ford Prefect on Thu May 29, 2008 2:22 pm, edited 1 time in total.



Thu May 29, 2008 11:17 am
Profile
Rookie

Joined: Thu May 29, 2008 4:46 am
Posts: 3
Post 
Thank you for your reply, Ford Prefect. However, your idea restricts the function only to work with a fixed array size. I need a matrix multiplication for matrices of any size.


Thu May 29, 2008 11:29 am
Profile
Guru
User avatar

Joined: Sat Mar 01, 2008 12:52 pm
Posts: 1030
Post 
Hi Talavasu,
did you know that RobotC supports only 2 dimensional matrices?

if this fits, you may wish to define several multiplation functions depending on the sizes of the matrices (anyway, you may overload the definitions), since even dynamic memory addressing is not possible with RobotC...

_________________
regards,
HaWe aka Ford
#define S sqrt(t+2*i*i)<2
#define F(a,b) for(a=0;a<b;++a)
float x,y,r,i,s,j,t,n;task main(){F(y,64){F(x,99){r=i=t=0;s=x/33-2;j=y/32-1;F(n,50&S){t=r*r-i*i;i=2*r*i+j;r=t+s;}if(S){PutPixel(x,y);}}}while(1)}


Thu May 29, 2008 12:00 pm
Profile
Rookie

Joined: Thu May 29, 2008 4:46 am
Posts: 3
Post 
2D-arrays are enough for me. So that's fine.

Instead of multiple functions I decided to use the maximum number of rows and columns in the type definition. As my matrices don't vary much in their size, this would be okay. Surprisingly this works quite well as long as I use only an int array.

When I use an array with floating point values and change one value of an array element in the multiplication function, the display of my NXT freezes and the NXT can't be turned of anymore. Furthermore I hear a gentle knocking. The only solution in this situation is to push the reset button.

I wonder why this doesn't work with floating point values. Unfortunately I need them.

Edited: I fixed the problem by using RobotC 1.30 Beta 4. Now it works even with floating point values. :)


Last edited by Talavasu on Fri May 30, 2008 6:45 am, edited 1 time in total.



Fri May 30, 2008 6:07 am
Profile
Guru
User avatar

Joined: Sat Mar 01, 2008 12:52 pm
Posts: 1030
Post 
which release do you have?
I had the same trouble using 1.30 beta 2, but not any longer with 1.30 beta 4 .
(ok, there are other bugs with arrays as part of structures, or arrays of structures, or both, but not with arithmetic operations like multiplication or even exponential functions)

... and did you change the format string of the print command...?

[EDIT:] as I just have watched, you meanwhile got it... 8)

_________________
regards,
HaWe aka Ford
#define S sqrt(t+2*i*i)<2
#define F(a,b) for(a=0;a<b;++a)
float x,y,r,i,s,j,t,n;task main(){F(y,64){F(x,99){r=i=t=0;s=x/33-2;j=y/32-1;F(n,50&S){t=r*r-i*i;i=2*r*i+j;r=t+s;}if(S){PutPixel(x,y);}}}while(1)}


Fri May 30, 2008 6:44 am
Profile
Creator
Creator

Joined: Fri Feb 09, 2007 9:21 am
Posts: 615
Post 
There were compiler problems with array variables as function parameters in earlier releases. I think verison 1.37 has cleared all or most of these up.

There were also problems with struct elements when they weren't simple "ints". The compiler code generator was not properly looking at the variable type and they were defaulting to "int". I believe this was fixed around version 1.30.

Since many ROBOTC users are novice programmers there was an early decision not to support pointers in ROBOTC. And also a decision to extend "C" to support call by reference function parameters with the use of the "&" operator found in C++. This worked fine foir some time but I realized earlier this year that there really was a need for pointers. Pointer support has slowly been added to ROBOTC but is not yet totally bug-free. Sorry. : :?


Wed Jun 04, 2008 5:22 am
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 7 posts ] 

Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  



Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software for PTF.