2D array... I need help! (kinda urgent...).
Author |
Message |
newspaper
Rookie
Joined: Sun May 12, 2013 3:17 pm Posts: 21
|
 2D array... I need help! (kinda urgent...).
Hi guys, First of all, I have to mention that I'm not from the U.S. and I'm not from England. Therfore, I might have some mistakes, I'll try to write as better as I can. I have to do some project with Lego Mindstroms, which I want to program with ROBOTC (otherwhise, I wouldn't write her at all). Anyway, I need 2D array code but I don't know in 100% how to do that. something like that:
int array [3][5] = {0,0,0,0,0,0,1,1,0,0,1,0,1,0,0} now when the program reads the whole code, when it read "0" than do X action and when it "1" do "y" action (doesn't matter for now what are the actions).
So how can I do it? Thanks
Last edited by newspaper on Mon May 20, 2013 2:26 am, edited 1 time in total.
|
Sun May 12, 2013 3:38 pm |
|
 |
mightor
Site Admin
Joined: Wed Mar 05, 2008 8:14 am Posts: 3654 Location: Rotterdam, The Netherlands
|
 Re: 2D array... I need help
To create and initialise a 2D array, you would type the following: Now that you have that information, how do you think you would achieve the next part? = Xander
_________________| Professional Conduit of Reasonableness| (Title bestowed upon on the 8th day of November, 2013) | My Blog: I'd Rather Be Building Robots| ROBOTC 3rd Party Driver Suite: [ Project Page]
|
Sun May 12, 2013 4:04 pm |
|
 |
newspaper
Rookie
Joined: Sun May 12, 2013 3:17 pm Posts: 21
|
 Re: 2D array... I need help
Hi, First of all thank you for your answer. Actually, I did something very samiliar:  |  |  |  | Code: task main() { int i,j; char image[6][4] = {{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,1,0}}; for (i = 0; i < 6; i++) { for (j = 0; j < 4; j++) { if (image[i][j] == 1) { When it recognize "1" Do something X } Do something else which is not X when it's "0" } }
|  |  |  |  |
It should work, right?
|
Wed May 15, 2013 3:18 pm |
|
 |
mightor
Site Admin
Joined: Wed Mar 05, 2008 8:14 am Posts: 3654 Location: Rotterdam, The Netherlands
|
 Re: 2D array... I need help
Try this bit of code. Make sure you open the debugstream window  = Xander  |  |  |  | Code: task main() { int i,j; char image[6][4] = {{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,1,0}}; for (i = 0; i < 6; i++) { for (j = 0; j < 4; j++) { if (image[i][j] == 1) { writeDebugStreamLine("%d, %d: we have a 1!", i, j); } else { writeDebugStreamLine("%d, %d: we have something else", i, j); } } } } |  |  |  |  |
_________________| Professional Conduit of Reasonableness| (Title bestowed upon on the 8th day of November, 2013) | My Blog: I'd Rather Be Building Robots| ROBOTC 3rd Party Driver Suite: [ Project Page]
|
Thu May 16, 2013 1:51 am |
|
 |
newspaper
Rookie
Joined: Sun May 12, 2013 3:17 pm Posts: 21
|
 Re: 2D array... I need help
 |  |  |  | mightor wrote: Try this bit of code. Make sure you open the debugstream window  = Xander  |  |  |  | Code: task main() { int i,j; char image[6][4] = {{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,1,0}}; for (i = 0; i < 6; i++) { for (j = 0; j < 4; j++) { if (image[i][j] == 1) { writeDebugStreamLine("%d, %d: we have a 1!", i, j); } else { writeDebugStreamLine("%d, %d: we have something else", i, j); } } } } |  |  |  |  |
|  |  |  |  |
First of all, I just want to be sure: I know the differences between the definitions of "char" and "int" but why when I'm doing the 2D array I need to write "char" and not "int"? Secondly, that was written in the debugstream window:  |  |  |  | Code: , 1: we have something else 2, 2: we have something else 2, 3: we have something else 3, 0: we have something else 3, 1: we have something else 3, 2: we have something else 3, 3: we have something else 4, 0: we have something else 4, 1: we have something else 4, 2: we have something else 4, 3: we have something else 5, 0: we have something else 5, 1: we have something else 5, 2: we have a 1! 5, 3: we have something else 0, 0: we have something else 0, 1: we have something else 0, 2: we have something else 0, 3: we have something else 1, 0: we have something else 1, 1: we have something else 1, 2: we have something else 1, 3: we have something else 2, 0: we have something else 2, 1: we have something else 2, 2: we have something else 2, 3: we have something else 3, 0: we have something else 3, 1: we have something else 3, 2: we have something else 3, 3: we have something else 4, 0: we have something else 4, 1: we have something else 4, 2: we have something else 4, 3: we have something else 5, 0: we have something else 5, 1: we have something else 5, 2: we have a 1! 5, 3: we have something else 0, 0: we have something else 0, 1: we have something else 0, 2: we have something else 0, 3: we have something else 1, 0: we have something else 1, 1: we have something else 1, 2: we have something else 1, 3: we have something else 2, 0: we have something else 2, 1: we have something else 2, 2: we have something else 2, 3: we have something else 3, 0: we have something else 3, 1: we have something else 3, 2: we have something else 3, 3: we have something else 4, 0: we have something else 4, 1: we have something else 4, 2: we have something else 4, 3: we have something else 5, 0: we have something else 5, 1: we have something else 5, 2: we have a 1! 5, 3: we have something else 0, 0: we have something else 0, 1: we have something else 0, 2: we have something else 0, 3: we have something else 1, 0: we have something else 1, 1: we have something else 1, 2: we have something else 1, 3: we have something else 2, 0: we have something else 2, 1: we have something else 2, 2: we have something else 2, 3: we have something else 3, 0: we have something else 3, 1: we have something else 3, 2: we have something else 3, 3: we have something else 4, 0: we have something else 4, 1: we have something else 4, 2: we have something else 4, 3: we have something else 5, 0: we have something else 5, 1: we have something else 5, 2: we have a 1! 5, 3: we have something else 0, 0: we have something else 0, 1: we have something else 0, 2: we have something else 0, 3: we have something else 1, 0: we have something else 1, 1: we have something else 1, 2: we have something else 1, 3: we have something else 2, 0: we have something else 2, 1: we have something else 2, 2: we have something else 2, 3: we have something else 3, 0: we have something else 3, 1: we have something else 3, 2: we have something else 3, 3: we have something else 4, 0: we have something else 4, 1: we have something else 4, 2: we have something else 4, 3: we have something else 5, 0: we have something else 5, 1: we have something else 5, 2: we have a 1! 5, 3: we have something else 0, 0: we have something else 0, 1: we have something else 0, 2: we have something else 0, 3: we have something else 1, 0: we have something else 1, 1: we have something else 1, 2: we have something else 1, 3: we have something else 2, 0: we have something else 2, 1: we have something else 2, 2: we have something else 2, 3: we have something else 3, 0: we have something else 3, 1: we have something else 3, 2: we have something else 3, 3: we have something else 4, 0: we have something else 4, 1: we have something else 4, 2: we have something else 4, 3: we have something else 5, 0: we have something else 5, 1: we have something else 5, 2: we have a 1! 5, 3: we have something else 0, 0: we have something else 0, 1: we have something else 0, 2: we have something else 0, 3: we have something else 1, 0: we have something else
|  |  |  |  |
Not good... Why it's reapiting too many times? And if its should reapiting over and over again, then why it stopped? I really need your help... Thanks.
|
Thu May 16, 2013 10:56 am |
|
 |
JohnWatson
Site Admin
Joined: Thu May 24, 2012 12:15 pm Posts: 722
|
 Re: 2D array... I need help
Try adding in a clearDebugStream(); command before you enter the 'for' loops; this will clear the debug stream each time the program is run. Otherwise, the data will be persistent between compiles.  |  |  |  | Code: task main() { int i,j; char image[6][4] = {{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,1,0}};
clearDebugStream();
for (i = 0; i < 6; i++) { for (j = 0; j < 4; j++) { if (image[i][j] == 1) { writeDebugStreamLine("%d, %d: we have a 1!", i, j); } else { writeDebugStreamLine("%d, %d: we have something else", i, j); } } } } |  |  |  |  |
_________________Check out our Blog! And our Facebook page! Need help? Take a look at our updated help documentation and the ROBOTC Forums.
|
Thu May 16, 2013 12:05 pm |
|
 |
newspaper
Rookie
Joined: Sun May 12, 2013 3:17 pm Posts: 21
|
 Re: 2D array... I need help
 |  |  |  | JohnWatson wrote: Try adding in a clearDebugStream(); command before you enter the 'for' loops; this will clear the debug stream each time the program is run. Otherwise, the data will be persistent between compiles.  |  |  |  | Code: task main() { int i,j; char image[6][4] = {{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,1,0}};
clearDebugStream();
for (i = 0; i < 6; i++) { for (j = 0; j < 4; j++) { if (image[i][j] == 1) { writeDebugStreamLine("%d, %d: we have a 1!", i, j); } else { writeDebugStreamLine("%d, %d: we have something else", i, j); } } } } |  |  |  |  |
|  |  |  |  |
 |  |  |  | Code: 0, 0: we have something else 0, 1: we have something else 0, 2: we have something else 0, 3: we have something else 1, 0: we have something else 1, 1: we have something else 1, 2: we have something else 1, 3: we have something else 2, 0: we have something else 2, 1: we have something else 2, 2: we have something else 2, 3: we have something else 3, 0: we have something else 3, 1: we have something else 3, 2: we have something else 3, 3: we have something else 4, 0: we have something else 4, 1: we have something else 4, 2: we have something else 4, 3: we have something else 5, 0: we have something else 5, 1: we have something else 5, 2: we have a 1! 5, 3: we have something else
|  |  |  |  |
Good, it's better! Thanks, but it's not should be "6,4"? But what should I write instead of "clearDebugStream"? Or I need to keep it there even when I want other actions (like turning on motors etc) will be instead of "writeDebugStreamLine("%d, %d: we have a 1!", i, j)"? Moreover, I know the differences between the definitions of "char" and "int" but why when I'm doing the 2D array I need to write "char" and not "int"?
|
Sat May 18, 2013 3:19 pm |
|
 |
Ernest3.14
Professor
Joined: Sat May 18, 2013 1:24 pm Posts: 271 Location: Olympia, WA
|
 Re: 2D array... I need help
`int` should work as well. Does it crash? In RobotC an `int` has a larger range than a `char` (-32768~32767 vs -128~127, respectively).
_________________FTC Team 6424, the 'Oly Cow - Chief programmer. FRC Team 4450, Olympia Robotics Federation (ORF). and also quadrotors. Quadrotors!
|
Sat May 18, 2013 3:30 pm |
|
 |
newspaper
Rookie
Joined: Sun May 12, 2013 3:17 pm Posts: 21
|
 Re: 2D array... I need help
No, it's not crashing. Thanks buddy! I changed it. Do you know the answer to the other question?
|
Sat May 18, 2013 3:46 pm |
|
 |
newspaper
Rookie
Joined: Sun May 12, 2013 3:17 pm Posts: 21
|
 Re: 2D array... I need help! (kinda urgent...).
So... guys, I still need your help. Please. Well, what should I write instead of "clearDebugStream"? Or I need to keep it there even when I want other actions (like turning on motors etc) instead of "writeDebugStreamLine("%d, %d: we have a 1!", i, j)"?
|
Wed May 22, 2013 2:14 am |
|
 |
mightor
Site Admin
Joined: Wed Mar 05, 2008 8:14 am Posts: 3654 Location: Rotterdam, The Netherlands
|
 Re: 2D array... I need help! (kinda urgent...).
The part you put in the "we got a 1" should be what you'd like the robot to do when it sees an obstacle. Is there anything specific you had in mind? As for char versus int, I just uses chars because it didn't look like you were going to hold very large numbers in there. An int is 2 bytes big and a char is only one byte big. An int's range goes from −32,768 to 32,767, a char is unsigned and goes from 0 to 255. Edit: I would leave the debugstream there as well as whatever actions you end up putting there. It makes debugging easier  = Xander
_________________| Professional Conduit of Reasonableness| (Title bestowed upon on the 8th day of November, 2013) | My Blog: I'd Rather Be Building Robots| ROBOTC 3rd Party Driver Suite: [ Project Page]
|
Wed May 22, 2013 2:49 am |
|
 |
newspaper
Rookie
Joined: Sun May 12, 2013 3:17 pm Posts: 21
|
 Re: 2D array... I need help! (kinda urgent...).
The thing is that as you saw at the first time when you asked me to do the debug stream it reapted too many times. Therefore, if I just write the action X and Y, then it's happening over and over again like the program doesn't care about the 0's and the 1's, again. So maybe I need to write something like "cleardebugstream" in the beginning to avoid the repeat.
|
Wed May 22, 2013 2:59 am |
|
 |
mightor
Site Admin
Joined: Wed Mar 05, 2008 8:14 am Posts: 3654 Location: Rotterdam, The Netherlands
|
 Re: 2D array... I need help! (kinda urgent...).
I see one of your Qs has not been answered. Why is it not 6,4 when that's the array dimension? That's an easy one, you see in C (no pun intended), counters start counting at 0, not 1. This may appear a little weird at first, but it explains why you're only going as far as 5,3, which is really the 6th and 4th entry  = Xander This program should work properly:  |  |  |  | Code: task main() { int i,j; char image[6][4] = {{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,1,0}};
clearDebugStream();
for (i = 0; i < 6; i++) { for (j = 0; j < 4; j++) { if (image[i][j] == 1) { writeDebugStreamLine("%d, %d: we have a 1!", i, j); PlaySound(soundBeepBeep); while(bSoundActive) wait1Msec(10); } else { writeDebugStreamLine("%d, %d: we have something else", i, j); PlaySound(soundShortBlip); while(bSoundActive) wait1Msec(10); } } } } |  |  |  |  |
_________________| Professional Conduit of Reasonableness| (Title bestowed upon on the 8th day of November, 2013) | My Blog: I'd Rather Be Building Robots| ROBOTC 3rd Party Driver Suite: [ Project Page]
|
Wed May 22, 2013 5:24 am |
|
 |
newspaper
Rookie
Joined: Sun May 12, 2013 3:17 pm Posts: 21
|
 Re: 2D array... I need help! (kinda urgent...).
 |  |  |  | mightor wrote: I see one of your Qs has not been answered. Why is it not 6,4 when that's the array dimension? That's an easy one, you see in C (no pun intended), counters start counting at 0, not 1. This may appear a little weird at first, but it explains why you're only going as far as 5,3, which is really the 6th and 4th entry  = Xander This program should work properly:  |  |  |  | Code: task main() { int i,j; char image[6][4] = {{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,1,0}};
clearDebugStream();
for (i = 0; i < 6; i++) { for (j = 0; j < 4; j++) { if (image[i][j] == 1) { writeDebugStreamLine("%d, %d: we have a 1!", i, j); PlaySound(soundBeepBeep); while(bSoundActive) wait1Msec(10); } else { writeDebugStreamLine("%d, %d: we have something else", i, j); PlaySound(soundShortBlip); while(bSoundActive) wait1Msec(10); } } } } |  |  |  |  |
|  |  |  |  |
It's not that simple...  |  |  |  | Code: task main() { int i,j; int image[6][4] = {{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,1,0}};
clearDebugStream();
Turn on motor1 and move it.
for (i = 0; i < 6; i++) { for (j = 0; j < 4; j++) {
switch (image[i][j]) { case 1: Stop motor1 and move motor2 for some seconds. Then, stop motor 2 and continute moving motor1 untill there are other 1's. break;
case 0: Keep moving the motor1 untill the second touch sensor is pressed. When the second touch sensor is pressed, move back the motor1 untill the first touch sensor is pressed. Then stop motor1. break;
default: }
} } }
|  |  |  |  |
This is the code that I need and still it's not working. Instead of words, I wrote the functions of the motors and the loop, of course but it's not working. In this code, it stays on the case 0. HELLLLLLLLLLLLLP!!! So much important and urgant! please!
|
Thu May 23, 2013 2:26 pm |
|
 |
JohnWatson
Site Admin
Joined: Thu May 24, 2012 12:15 pm Posts: 722
|
 Re: 2D array... I need help! (kinda urgent...).
We will need to see the full code (including what you are doing with the motors); remember, you will be encounter the 0 condition 18 times before it gets to the 1 condition, so whatever you have in the 'case:0' will repeat 18 times in a row. Please provide us with the full code (motor commands included) and we will continue to help debug these issues. I tried your provided code (with debug commands instead of psuedocode) and receive the expected result:  |  |  |  | Code: task main() { int i,j; int image[6][4] = {{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,1,0}};
clearDebugStream(); //Turn on motor1 and move it for (i = 0; i < 6; i++) { for (j = 0; j < 4; j++) {
switch (image[i][j]) { case 1: writeDebugStreamLine("The value of %d,%d is 1", i, j); break; case 0: writeDebugStreamLine("The value of %d,%d is 0", i, j); break; default: break; } } } }
|  |  |  |  |
Result:  |  |  |  | Quote: The value of 0,0 is 0 The value of 0,1 is 0 The value of 0,2 is 0 The value of 0,3 is 0 The value of 1,0 is 0 The value of 1,1 is 0 The value of 1,2 is 0 The value of 1,3 is 0 The value of 2,0 is 0 The value of 2,1 is 0 The value of 2,2 is 0 The value of 2,3 is 0 The value of 3,0 is 0 The value of 3,1 is 0 The value of 3,2 is 0 The value of 3,3 is 0 The value of 4,0 is 0 The value of 4,1 is 0 The value of 4,2 is 0 The value of 4,3 is 0 The value of 5,0 is 0 The value of 5,1 is 0 The value of 5,2 is 1 The value of 5,3 is 0
|  |  |  |  |
Edit: On a side note, Xander's provided code works the exact same way (using if statements instead of a switch/case). In this instance, there is no 'superior' method of coding, but it is rather a matter of preference. Just wanted to throw that out there for reference.
_________________Check out our Blog! And our Facebook page! Need help? Take a look at our updated help documentation and the ROBOTC Forums.
|
Thu May 23, 2013 3:05 pm |
|
|
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
|
|