View unanswered posts | View active topics It is currently Fri Feb 12, 2016 8:26 am

 Page 1 of 1 [ 7 posts ]
 Print view Previous topic | Next topic
Array as function parameter?
Author Message
Rookie

Joined: Thu May 29, 2008 4:46 am
Posts: 3
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
Guru

Joined: Sat Mar 01, 2008 12:52 pm
Posts: 1030

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)

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
Rookie

Joined: Thu May 29, 2008 4:46 am
Posts: 3

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
Guru

Joined: Sat Mar 01, 2008 12:52 pm
Posts: 1030

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)

Thu May 29, 2008 12:00 pm
Rookie

Joined: Thu May 29, 2008 4:46 am
Posts: 3

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
Guru

Joined: Sat Mar 01, 2008 12:52 pm
Posts: 1030

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...

_________________
regards,
HaWe aka Ford
#define S sqrt(t+2*i*i)<2
#define F(a,b) for(a=0;a<b;++a)

Fri May 30, 2008 6:44 am
Creator

Joined: Fri Feb 09, 2007 9:21 am
Posts: 616

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
Display posts from previous:  Sort by
 Page 1 of 1 [ 7 posts ]

Who is online

Users browsing this forum: No registered users and 1 guest

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

Search for:
 Jump to:  Select a forum ------------------ ROBOTC Applications    ROBOTC for LEGO MINDSTORMS       Third-party sensors    ROBOTC for CORTEX & PIC    ROBOTC for VEX IQ    ROBOTC for Arduino    Robot Virtual Worlds    Multi-Robot Communications    Issues and Bugs Competitions & Partners    Mini Urban Challenge    2014 Robotics Academy Summer of Learning    CS2N Robot Virtual Worlds Competitions       VEX Skyrise Competition 2014-2015       VEX Toss Up 2013-2014       FTC Block Party! 2013-2014    Competitions using VEX - BEST, TSA, VEX, and RoboFest!    FTC Programming    RoboCup Junior and Other ROBOT Competitions Virtual Brick Robotics Discussions    General Discussions    Project Discussions Off-Topic ROBOTC Forum & ROBOTC.net Suggestions/Feedback    ROBOTC Forums Suggestions/Comments    ROBOTC.net Suggestions/Comments       NXT Programming: Tips for Beginning with ROBOTC       VEX Programming: Tips for Beginning with ROBOTC    2013 Robotics Summer Of Learning       VEX Toss Up Programming Challenge       FTC Ring It Up! Programming Challenge    International Forums       Spanish Forums          ROBOTC for MINDSTORMS          ROBOTC for VEX       French Forums          ROBOTC pour Mindstorms          ROBOTC pour IFI VEX       Japanese Forums （日本語のフォーラム）       German Forums    2015 Spring Carnival Event    PLTW (Project Lead The Way)    Robotics Merit Badge