View unanswered posts | View active topics It is currently Tue Sep 02, 2014 5:44 am

 Page 1 of 3 [ 39 posts ] Go to page 1, 2, 3  Next
 Print view Previous topic | Next topic
2D array... I need help! (kinda urgent...).
Author Message
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
Moderator

Joined: Wed Mar 05, 2008 8:14 am
Posts: 3209
Location: Rotterdam, The Netherlands
Re: 2D array... I need help
To create and initialise a 2D array, you would type the following:
 Code:int foo[2][4] = {{7, 3, 5, 1}, {8, 1,  23, 5}};

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
Rookie

Joined: Sun May 12, 2013 3:17 pm
Posts: 21
Re: 2D array... I need help
mightor wrote:
To create and initialise a 2D array, you would type the following:
 Code:int foo[2][4] = {{7, 3, 5, 1}, {8, 1,  23, 5}};

Now that you have that information, how do you think you would achieve the next part?

= Xander

Hi,
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
Moderator

Joined: Wed Mar 05, 2008 8:14 am
Posts: 3209
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
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 else2, 2: we have something else2, 3: we have something else3, 0: we have something else3, 1: we have something else3, 2: we have something else3, 3: we have something else4, 0: we have something else4, 1: we have something else4, 2: we have something else4, 3: we have something else5, 0: we have something else5, 1: we have something else5, 2: we have a 1!5, 3: we have something else0, 0: we have something else0, 1: we have something else0, 2: we have something else0, 3: we have something else1, 0: we have something else1, 1: we have something else1, 2: we have something else1, 3: we have something else2, 0: we have something else2, 1: we have something else2, 2: we have something else2, 3: we have something else3, 0: we have something else3, 1: we have something else3, 2: we have something else3, 3: we have something else4, 0: we have something else4, 1: we have something else4, 2: we have something else4, 3: we have something else5, 0: we have something else5, 1: we have something else5, 2: we have a 1!5, 3: we have something else0, 0: we have something else0, 1: we have something else0, 2: we have something else0, 3: we have something else1, 0: we have something else1, 1: we have something else1, 2: we have something else1, 3: we have something else2, 0: we have something else2, 1: we have something else2, 2: we have something else2, 3: we have something else3, 0: we have something else3, 1: we have something else3, 2: we have something else3, 3: we have something else4, 0: we have something else4, 1: we have something else4, 2: we have something else4, 3: we have something else5, 0: we have something else5, 1: we have something else5, 2: we have a 1!5, 3: we have something else0, 0: we have something else0, 1: we have something else0, 2: we have something else0, 3: we have something else1, 0: we have something else1, 1: we have something else1, 2: we have something else1, 3: we have something else2, 0: we have something else2, 1: we have something else2, 2: we have something else2, 3: we have something else3, 0: we have something else3, 1: we have something else3, 2: we have something else3, 3: we have something else4, 0: we have something else4, 1: we have something else4, 2: we have something else4, 3: we have something else5, 0: we have something else5, 1: we have something else5, 2: we have a 1!5, 3: we have something else0, 0: we have something else0, 1: we have something else0, 2: we have something else0, 3: we have something else1, 0: we have something else1, 1: we have something else1, 2: we have something else1, 3: we have something else2, 0: we have something else2, 1: we have something else2, 2: we have something else2, 3: we have something else3, 0: we have something else3, 1: we have something else3, 2: we have something else3, 3: we have something else4, 0: we have something else4, 1: we have something else4, 2: we have something else4, 3: we have something else5, 0: we have something else5, 1: we have something else5, 2: we have a 1!5, 3: we have something else0, 0: we have something else0, 1: we have something else0, 2: we have something else0, 3: we have something else1, 0: we have something else1, 1: we have something else1, 2: we have something else1, 3: we have something else2, 0: we have something else2, 1: we have something else2, 2: we have something else2, 3: we have something else3, 0: we have something else3, 1: we have something else3, 2: we have something else3, 3: we have something else4, 0: we have something else4, 1: we have something else4, 2: we have something else4, 3: we have something else5, 0: we have something else5, 1: we have something else5, 2: we have a 1!5, 3: we have something else0, 0: we have something else0, 1: we have something else0, 2: we have something else0, 3: we have something else1, 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?
Thanks.

Thu May 16, 2013 10:56 am

Joined: Thu May 24, 2012 12:15 pm
Posts: 566
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 Wiki and our Forums.

I just met you,
And this is crazy,
But here's my code now,
So fix it, maybe?
~ Carly Rae Jepsen parody

Thu May 16, 2013 12:05 pm
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 else0, 1: we have something else0, 2: we have something else0, 3: we have something else1, 0: we have something else1, 1: we have something else1, 2: we have something else1, 3: we have something else2, 0: we have something else2, 1: we have something else2, 2: we have something else2, 3: we have something else3, 0: we have something else3, 1: we have something else3, 2: we have something else3, 3: we have something else4, 0: we have something else4, 1: we have something else4, 2: we have something else4, 3: we have something else5, 0: we have something else5, 1: we have something else5, 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
Professor

Joined: Sat May 18, 2013 1:24 pm
Posts: 272
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).

Sat May 18, 2013 3:30 pm
Rookie

Joined: Sun May 12, 2013 3:17 pm
Posts: 21
Re: 2D array... I need help
 Ernest3.14 wrote:`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).

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
Rookie

Joined: Sun May 12, 2013 3:17 pm
Posts: 21
Re: 2D array... I need help! (kinda urgent...).
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
Moderator

Joined: Wed Mar 05, 2008 8:14 am
Posts: 3209
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
Rookie

Joined: Sun May 12, 2013 3:17 pm
Posts: 21
Re: 2D array... I need help! (kinda urgent...).
 mightor wrote: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?Edit: I would leave the debugstream there as well as whatever actions you end up putting there. It makes debugging easier = Xander

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
Moderator

Joined: Wed Mar 05, 2008 8:14 am
Posts: 3209
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
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

Joined: Thu May 24, 2012 12:15 pm
Posts: 566
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.

 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 0The value of 0,1 is 0The value of 0,2 is 0The value of 0,3 is 0The value of 1,0 is 0The value of 1,1 is 0The value of 1,2 is 0The value of 1,3 is 0The value of 2,0 is 0The value of 2,1 is 0The value of 2,2 is 0The value of 2,3 is 0The value of 3,0 is 0The value of 3,1 is 0The value of 3,2 is 0The value of 3,3 is 0The value of 4,0 is 0The value of 4,1 is 0The value of 4,2 is 0The value of 4,3 is 0The value of 5,0 is 0The value of 5,1 is 0The value of 5,2 is 1The 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 Wiki and our Forums.

I just met you,
And this is crazy,
But here's my code now,
So fix it, maybe?
~ Carly Rae Jepsen parody

Thu May 23, 2013 3:05 pm
Display posts from previous:  Sort by
 Page 1 of 3 [ 39 posts ] Go to page 1, 2, 3  Next

#### Who is online

Users browsing this forum: No registered users and 2 guests

 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    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    Robotics Merit Badge Virtual NXT Robotics Discussions    General Discussions    Project Discussions International Forums    Spanish Forums       ROBOTC for MINDSTORMS       ROBOTC for VEX    French Forums       ROBOTC pour Mindstorms       ROBOTC pour IFI VEX    Japanese Forums （日本語のフォーラム）    German Forums 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