| Author |
Message |
|
magicode
Moderator
Joined: Tue Sep 14, 2010 9:19 pm Posts: 495
|
 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.
_________________ sudo rm -rf /
|
| Thu Apr 05, 2012 12:14 am |
|
 |
|
sbpaabck
Rookie
Joined: Wed Apr 04, 2012 11:50 pm Posts: 6
|
 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...
|
| Thu Apr 05, 2012 6:59 am |
|
 |
|
magicode
Moderator
Joined: Tue Sep 14, 2010 9:19 pm Posts: 495
|
 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.
_________________ sudo rm -rf /
|
| Thu Apr 05, 2012 11:04 am |
|
 |
|
sbpaabck
Rookie
Joined: Wed Apr 04, 2012 11:50 pm Posts: 6
|
 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 
|
| Thu Apr 05, 2012 12:03 pm |
|
 |
|
magicode
Moderator
Joined: Tue Sep 14, 2010 9:19 pm Posts: 495
|
 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: 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.
_________________ sudo rm -rf /
|
| Thu Apr 05, 2012 12:29 pm |
|
 |
|
RoboDesigners
Novice
Joined: Sat Jul 10, 2010 3:06 pm Posts: 86 Location: Roanoke, VA
|
 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
_________________Check out my website! www.RoboDesigners.comVRC Team 2190 Twitter: @RoboDesigners
|
| Thu Apr 05, 2012 12:48 pm |
|
 |
|
magicode
Moderator
Joined: Tue Sep 14, 2010 9:19 pm Posts: 495
|
 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. But this will make things a little more complicated when decoding the array. You'll need to access it like:
_________________ sudo rm -rf /
|
| Thu Apr 05, 2012 1:38 pm |
|
 |
|
sbpaabck
Rookie
Joined: Wed Apr 04, 2012 11:50 pm Posts: 6
|
 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 
|
| Thu Apr 05, 2012 2:19 pm |
|
 |
|
RoboDesigners
Novice
Joined: Sat Jul 10, 2010 3:06 pm Posts: 86 Location: Roanoke, VA
|
 Re: Using Arrays
Doesn't RobotC support 2d arrays? Couldn't you have an array like so? 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
_________________Check out my website! www.RoboDesigners.comVRC Team 2190 Twitter: @RoboDesigners
|
| Thu Apr 05, 2012 2:36 pm |
|
 |
|
magicode
Moderator
Joined: Tue Sep 14, 2010 9:19 pm Posts: 495
|
 Re: Using Arrays
Yes it does. I was showing how it could fit into an array if he wanted just a regular 1D array. typing arrayname[indexvalue] will return the value at that index. So if: int myArray[5] = {6,7,8,9,10}; then myArray[2] = 8.
_________________ sudo rm -rf /
|
| Thu Apr 05, 2012 3:08 pm |
|
 |
|
sbpaabck
Rookie
Joined: Wed Apr 04, 2012 11:50 pm Posts: 6
|
 Re: Using Arrays
what would be the code for the 2d array? sil vous plait?
|
| Thu Apr 05, 2012 3:45 pm |
|
 |
|
magicode
Moderator
Joined: Tue Sep 14, 2010 9:19 pm Posts: 495
|
 Re: Using Arrays
As RoboDesigners said, you would use a 3 by 60 array. A 2D array implementation would look like this:
_________________ sudo rm -rf /
|
| Thu Apr 05, 2012 4:00 pm |
|
 |
|
sbpaabck
Rookie
Joined: Wed Apr 04, 2012 11:50 pm Posts: 6
|
 Re: Using Arrays
thanks man you saved my bacon :0)
|
| Fri Apr 06, 2012 9:39 am |
|
 |
|
xiah
Rookie
Joined: Thu Mar 22, 2012 12:12 pm Posts: 6
|
 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 
|
| Wed Apr 11, 2012 9:06 am |
|
 |
|
ericsmalls
Moderator
Joined: Tue Dec 21, 2010 1:38 pm Posts: 24
|
 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
|
| Mon May 14, 2012 7:35 pm |
|
|