View unanswered posts | View active topics It is currently Wed Jul 23, 2014 12:10 pm






Reply to topic  [ 30 posts ]  Go to page Previous  1, 2
Using Arrays 
Author Message
Moderator
Moderator
User avatar

Joined: Tue Sep 14, 2010 9:19 pm
Posts: 496
Post 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
Profile
Rookie

Joined: Wed Apr 04, 2012 11:50 pm
Posts: 6
Post 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
Profile
Moderator
Moderator
User avatar

Joined: Tue Sep 14, 2010 9:19 pm
Posts: 496
Post 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
Profile
Rookie

Joined: Wed Apr 04, 2012 11:50 pm
Posts: 6
Post 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
Profile
Moderator
Moderator
User avatar

Joined: Tue Sep 14, 2010 9:19 pm
Posts: 496
Post 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.

_________________
sudo rm -rf /


Thu Apr 05, 2012 12:29 pm
Profile
Novice
User avatar

Joined: Sat Jul 10, 2010 3:06 pm
Posts: 86
Location: Roanoke, VA
Post 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.com

VRC Team 2190

Twitter: @RoboDesigners


Thu Apr 05, 2012 12:48 pm
Profile WWW
Moderator
Moderator
User avatar

Joined: Tue Sep 14, 2010 9:19 pm
Posts: 496
Post 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];

_________________
sudo rm -rf /


Thu Apr 05, 2012 1:38 pm
Profile
Rookie

Joined: Wed Apr 04, 2012 11:50 pm
Posts: 6
Post 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
Profile
Novice
User avatar

Joined: Sat Jul 10, 2010 3:06 pm
Posts: 86
Location: Roanoke, VA
Post 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

_________________
Check out my website! www.RoboDesigners.com

VRC Team 2190

Twitter: @RoboDesigners


Thu Apr 05, 2012 2:36 pm
Profile WWW
Moderator
Moderator
User avatar

Joined: Tue Sep 14, 2010 9:19 pm
Posts: 496
Post 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.

_________________
sudo rm -rf /


Thu Apr 05, 2012 3:08 pm
Profile
Rookie

Joined: Wed Apr 04, 2012 11:50 pm
Posts: 6
Post Re: Using Arrays
what would be the code for the 2d array? sil vous plait?


Thu Apr 05, 2012 3:45 pm
Profile
Moderator
Moderator
User avatar

Joined: Tue Sep 14, 2010 9:19 pm
Posts: 496
Post 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);
}

_________________
sudo rm -rf /


Thu Apr 05, 2012 4:00 pm
Profile
Rookie

Joined: Wed Apr 04, 2012 11:50 pm
Posts: 6
Post Re: Using Arrays
thanks man you saved my bacon :0)


Fri Apr 06, 2012 9:39 am
Profile
Rookie

Joined: Thu Mar 22, 2012 12:12 pm
Posts: 6
Post 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
Profile
Moderator
Moderator
User avatar

Joined: Tue Dec 21, 2010 1:38 pm
Posts: 24
Post 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

_________________
Eric Smalls
About Me: http://www.stanford.edu/~esmalls/
My Projects: http://www.ericsmalls.com/p/my-projects.html


Mon May 14, 2012 7:35 pm
Profile WWW
Display posts from previous:  Sort by  
Reply to topic   [ 30 posts ]  Go to page Previous  1, 2

Who is online

Users browsing this forum: jerrykern 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.