Computing.Net > Forums > Programming > Multidimentional Arrays

Multidimentional Arrays

Reply to Message Icon

Original Message
Name: xscript
Date: April 6, 2005 at 10:36:57 Pacific
Subject: Multidimentional Arrays
OS: Windows XP
CPU/Ram: NA
Comment:

Hi,

I have a query regarding multidimentional arrays in c programming.

I have a number of functions, one which fills a 2D array with *'s in each container.

I am trying to create a turtle type program, where the turtle is told which direction to travel in the 2D array, and to draw .'s as it travels.
I am having trouble with one specific part:

I am calling a function from the main section and passing a few values, for instance Direction of travel, steps to move in the 2D array.

The problem is, I am returning to this array again and again, each time with a different command, either change direction, and/or travel a few steps.

I need to know the previous status of the turtles location in the array, but I don't want to set up a couple of global variables for this because it's a bit messy.

I can't use local variables in the function to save the current location of the turtle in the array because the first time it enters the function, these variables are not set and it causes an error message?

Any ideas how I might overcome this problem?

If you haven't heard of a turtle program, this won't make any sense.

Thanks for any help

Xscript


Report Offensive Message For Removal

Response Number 1
Name: borelli35
Date: April 11, 2005 at 00:44:31 Pacific
Subject: Multidimentional Arrays
Reply: (edit)

====================================================================
Try adding a static local variable called int last_status for example and don't update that variable until the very end of the function. As to the first run through this function, create another variable named char *first_time and assign it at the end of the function with last_status. When this variable is equal to "first_time" and only if it is equal, will you allow the check on last_status.

borelli34



Report Offensive Follow Up For Removal

Response Number 2
Name: xscript
Date: April 11, 2005 at 09:42:46 Pacific
Subject: Multidimentional Arrays
Reply: (edit)

Hi borelli34,

There are two integers that I have declared globally at the moment (above main). They are only used in one function. The reason I have set these up globally is that I want the variables to be initialised with a value of 0 before entering the function. When the function runs, the variables will change dynamically depending on certain factors.

The problem I have is that I cannot initialise the variables at the top of the function with a value of 0, because each time the function is called it will reset them back to 0 and not retain the previous values they have been assigned from the last function call. Does that make sense?

The variables are used as soon as they enter the function and so they need to be initialised before they are used.

I kind of need a way of programming the following:

"If the function has not been run before, set the values of the two variables to 0, otherwise, leave them with the current values".

I could pass a boolean to the function ie void movePen(firstrun), but this seems messy to me, I'd rather keep everything relating to the local variables inside the function, and not have to pass a value to the function.

Thanks

Xscript


Report Offensive Follow Up For Removal

Response Number 3
Name: borelli35
Date: April 11, 2005 at 23:08:36 Pacific
Subject: Multidimentional Arrays
Reply: (edit)

=====================================================================
That is what I was talking about when I mentioned using a string type variable (char *) and then assigning it a very specific value. There is no way, even in the most unlikely of circumstances, that a char * would ever have the value of say "this is not the first pass". let me make this easier:

char *first_pass

void myproc()
{
if first_pass="this is not the first pass" then{
.
.
.
} else {
.
.
.
}
first_pass=="this is not the first pass";
.
.
.
}

follow?

borelli35


Report Offensive Follow Up For Removal

Response Number 4
Name: xscript
Date: April 27, 2005 at 12:03:22 Pacific
Subject: Multidimentional Arrays
Reply: (edit)

Hi Borelli,

I tried your way and it worked well, however, I've found another way to do it,

I've set the variables as static integers in the function and it works. It sets the values to 0 and then retains any changed as the function is called over and over,

Thanks for your help

Xscript


Report Offensive Follow Up For Removal







Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: Multidimentional Arrays

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software