ROBOTC.net forums
http://www.robotc.net/forums/

Using Arrays
http://www.robotc.net/forums/viewtopic.php?f=1&t=4385
Page 2 of 2

Author:  magicode [ Thu Apr 05, 2012 12:14 am ]
Post subject:  Re: Using Arrays

Well, that's not really a question on documentation. That's just regular, run of the mill programming. What exactly are you trying to do? If it's a different question, then start a new thread so we can keep knowledge in easy to find places.

Author:  sbpaabck [ Thu Apr 05, 2012 6:59 am ]
Post subject:  Re: Using Arrays

I am actually trying to do the same as the previous gentleman, the exact same project but am having trouble with the array to store the light, sound and sonar readings that are recorded each second, how would you make an array that would store those three variables in the same array, would it be a 3d array since there are 3 variables?

I cannot find an example of this anywhere :(

The array the previous gentleman describes seems to only store one varaible you see :)

any help much appreciated!!
arrays are hard to get your head around but an example is the best way i learn...

Author:  magicode [ Thu Apr 05, 2012 11:04 am ]
Post subject:  Re: Using Arrays

If you're trying to do the same thing as xiah, why are you using arrays? If you looked at the solutions provided, none of them used arrays.

Author:  sbpaabck [ Thu Apr 05, 2012 12:03 pm ]
Post subject:  Re: Using Arrays

well personally just because im a beginner and those solutions are very helpful and may work but I could not adapt them to my level of knowledge, which is beginner and no background in c, like my colleague.

All we need to know really is how to store variables from three sensors in an array for 60 second, once a second until the end where another function will call the array

in fact just how to store the variables in a syntaxically correct array would be a good start.

Here is what I have:

int light;
int sound;
int distance
int intReading [59] [59] [59];

for (light = 0; light < 60; light++)
{
intReading[light] = SensorValue[lightsensor];
}

for (sound = 0; sound < 60; sound++)
{
intReading[sound] = SensorValue[soundsensor];
}

for (sonar = 0; sonar < 60; sonar++)

{
intReading[sonar] = SensorValue[sonarsensor];
}

Basically I used the documentation to take a basic array and then tried to convert it into an array with three rows using a mixture of common sense and guesswork. As you know this approach will probably fail so we need the brains of real coders to lead us through the dark lol.

Thanks for taking the time to bother with us noobs :)

Author:  magicode [ Thu Apr 05, 2012 12:29 pm ]
Post subject:  Re: Using Arrays

If you want a single array, you will need to use structs like miki suggested. ROBOTC does not support 3D arrays. Here's some example code using miki's struct:
Code:
SAMPLE sampleArr[60];
for(int i = 0; i < 60; i++){
  sampleArr[i].light = SensorValue[lightsensor];
  sampleArr[i].sound = SensorValue[soundsensor];
  sampleArr[i].sonar = SensorValue[sonarensor];
  wait1MSec(1000); //1 second
}

This will work as long as you don't need to do anything else while the code is going on. If you do, then you will need to use either multitasking or timers.

Author:  RoboDesigners [ Thu Apr 05, 2012 12:48 pm ]
Post subject:  Re: Using Arrays

I agree with using structs, as it seems to be the simplest way of coding this. However, if you'd like to know about multidimensional arrays, I suggest you read this wikipedia section: http://en.wikipedia.org/wiki/Array_data ... nal_arrays

//Andrew

Author:  magicode [ Thu Apr 05, 2012 1:38 pm ]
Post subject:  Re: Using Arrays

And if you absolutely need one array, and you really, really, really don't want to use structs, you can put the information into a single array.
Code:
int sensorVals[180];
int i = 0;
while(i < 180){
  sensorVals[i++] = SensorValue[sensorlight];
  sensorVals[i++] = SensorValue[sensorsound];
  sensorVals[i++] = SensorValue[sensorsonar];
  wait1Msec(1000);
}


But this will make things a little more complicated when decoding the array. You'll need to access it like:
Code:
#define LIGHT 0
#define SOUND 1
#define SONAR 2

//value of kth sonar sensor reading = sensorVals[(3*k) + SONAR];

Author:  sbpaabck [ Thu Apr 05, 2012 2:19 pm ]
Post subject:  Re: Using Arrays

Wow you guys are good! thank you so much!

I used Miki's first array solution as I couldsee how it works and it fitted nicely into my code with no errors at last :)

If you gentleman would be so kind how could I draw the data out of the arrays (call it) so I can run different functions for my displays such as bubble sort etc etc. I believe this is the method I will use once task main has finished its 60 second wait loop a new section will handle all the display stuff but have no idea how to call the infor from these functions :)

Thanks again for your time :)

Author:  RoboDesigners [ Thu Apr 05, 2012 2:36 pm ]
Post subject:  Re: Using Arrays

magicode wrote:
And if you absolutely need one array, and you really, really, really don't want to use structs, you can put the information into a single array.
Code:
int sensorVals[180];
int i = 0;
while(i < 180){
  sensorVals[i++] = SensorValue[sensorlight];
  sensorVals[i++] = SensorValue[sensorsound];
  sensorVals[i++] = SensorValue[sensorsonar];
  wait1Msec(1000);
}

Doesn't RobotC support 2d arrays?

Couldn't you have an array like so?
Code:
int sensorVal[3][60]

Three deep, 60 long.

sensorVal[0][i] returns the ith light reading, sensorVal[1][i] returns ith sound reading, sensorVal[2][i] returns ith sonar reading...

//Andrew

Author:  magicode [ Thu Apr 05, 2012 3:08 pm ]
Post subject:  Re: Using Arrays

RoboDesigners wrote:
Doesn't RobotC support 2d arrays?
Yes it does. I was showing how it could fit into an array if he wanted just a regular 1D array.

sbpaabck wrote:
how could I draw the data out of the arrays

typing arrayname[indexvalue] will return the value at that index. So if: int myArray[5] = {6,7,8,9,10}; then myArray[2] = 8.

Author:  sbpaabck [ Thu Apr 05, 2012 3:45 pm ]
Post subject:  Re: Using Arrays

what would be the code for the 2d array? sil vous plait?

Author:  magicode [ Thu Apr 05, 2012 4:00 pm ]
Post subject:  Re: Using Arrays

As RoboDesigners said, you would use a 3 by 60 array. A 2D array implementation would look like this:
Code:
//There are two ways to set this up. 3 arrays that hold 60 values each, or 60 arrays that hold 3 values each.
//It doesn't really matter, but the order of the indexes will be flipped depending on which one you choose.
int sensorVals[3][60];
for(int i = 0; i < 60; i++){
  sensorVals[0][i] = SensorValue[sensorlight];
  sensorVals[1][i] = SensorValue[sensorsound];
  sensorVals[2][i] = SensorValue[sensorsonar];
  Wait1Msec(1000);
}

Author:  sbpaabck [ Fri Apr 06, 2012 9:39 am ]
Post subject:  Re: Using Arrays

thanks man you saved my bacon :0)

Author:  xiah [ Wed Apr 11, 2012 9:06 am ]
Post subject:  Re: Using Arrays

Sorry i've just checked this post and didn't realise people where still posting in it!
We've managed to get everything working now thanks to everyones help :)

Author:  ericsmalls [ Mon May 14, 2012 7:35 pm ]
Post subject:  Re: Using Arrays

Hey guys,

This link is a great way to learn about arrays, and other intermediate and advance programming topics in ROBOT-C
http://carrot.whitman.edu/Robots/notes.pdf

Page 2 of 2 All times are UTC - 5 hours [ DST ]
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/