#### Archived

This topic is now archived and is closed to further replies.

# how to transfer para of 2 dimension array?

This topic is 4958 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

i want to use 2 dynamic array to solve a problem , but there is sth. wrong about the programme (the programme as following): //--------------------------------------------------------------- #include #include #pragma hdrstop using namespace std; //--------------------------------------------------------------- #pragma argsused ifstream in("oil.in"); char oil[10][10]; int m,n,wait,oilnum=0; //------------------------------------------------- void check(char oil[j]) { if(oil[i][j]='@') //if it has oil { if(i>0 && j>0) check(oil[i-1][j-1]); //check 8 direction if( j>0) check(oil[i][j-1]); if(i0) check(oil[i+1][j-1]); if(i>0 ) check(oil[i-1][j]); if(i0 && j>m>>n && n) { for(int i=0;i>oil[i][j]; for(int i=0;i>wait; return 0; } //--------------------------------------------------------------- and here are the tips about wrong: [C++ Error] Unit1.cpp(15): E2451 Undefined symbol 'i' [C++ Error] Unit1.cpp(15): E2451 Undefined symbol 'j' [C++ Error] Unit1.cpp(46): E2034 Cannot convert 'int' to 'char ( *)[1]' [C++ Error] Unit1.cpp(46): E2342 Type mismatch in parameter 'oil' (wanted 'char ( *)[1]', got 'char') yes , i know where the mistakes are ,but i can't correct it by myself, if anyone know how to solve this problem ,please tell me......thx~ [edited by - littlewater on May 18, 2004 11:56:41 AM]

##### Share on other sites
I will answer with questions since the problem is very simple:
- Where are defined i and j in your check function ?
- What is the type of the check function parameter ?

Subsidiary questions raised from your post:
- What is a dynamic array ?
- What is a multi-dimension array ?
- What are the differences between a static and a dynamic array ?

Hope that helps.

Ghostly yours,
Red.

##### Share on other sites
i am so sorry that right now i wrote dimension as dynamic^^

dynamic array is an array that you define its dimension later

different from static array.

i know where the mistakes are ,but i wish the function that can

pass oil[j] for another check(oil[i][j-1]) and so on ......

i can''t ......

maybe i can pass i and j using as the 2nd and 3rd para??

##### Share on other sites
I have the feeling that you have no clue about how to correct your function call.
Let's step through it step by step.

your function description must declare all the types passed as parameters. Writing:
void check(char oil[ i][j])

is wrong since you tell you use i and j but you do not define them as parameters. The compiler doesn't understand what you are trying to do and doesn't know what the symbols i and j are if you do not define them. The compiler doesn't infer any meaning from pre- or post-existing code: it interprets each line separately into a stream of tokens.

Looking at your program you have global variables (oil, m, n, wait, oilnum). These can be accessed from everywhere. You seem to have a problem with local variables and scope of variables. Your use of i and j shows such a misunderstanding.

For your program, you define i and j within two for loops. The scope of i and j are thus restricted inside the curly braces of the for loops. When you call your function check, you enter another scope where i and j are not defined (from the compiler side, the i and j within the check function and the i and j within the for loops are not the same eventhough you use the same symbol to describe them). Thus you need to define i and j within the scope of your function. Since you need the position within the oil field, you need to pass as parameters the i and j variables. Your function header then becomes:

void check (int i, int j)

You do not need to pass oil as a parameter since it is a global variable.

Imagine now you want to pass oil as a parameter. What is the type of oil ? Oil is obviously an array but how does the computer store an array ? The computer has no understanding of dimension: its memory is just a stream of cells where you can store any kind of information. To be able to store or access that information, you need to tell the computer the type of the information so that it can correctly retrieve the data stored in memory.
You do not only store data in memory, but you also can store memory adresses.

Ok, now how does the computer store a multidimensional array ? When you look at your oil array, you are looking at a pointer to an area of memory. The type of oil is char**. What does that mean ? When you write
 a[ i] = x where a is an array of Type

the computer understands
*(a + i*sizeof(Type)) = x

it takes the starting adress of a, adds the index number times the size of the type to point to the correct memory area then retrieves the value stored at that memory adress. If you do not understand the notion, I would suggest you to look at pointers.

I strongly advise you to look at your manual to understand how everything works and to find out any documentation you can find (look on internet if you don't have access to introductory programming books).

Ghostly yours,
Red.

[edited by - Red Ghost on May 19, 2004 3:04:49 AM]

[edited by - Red Ghost on May 19, 2004 3:05:21 AM]

##### Share on other sites
thx~ i''ll try to find some...

À´×ÔÖÐ¹úµÄÇóÑ§Õß¡­¡­

##### Share on other sites
after my change it " void check(int i,int j) "

there is something wrong project raised exception class EStackOverflow with message overflow process stopped

and another information frame tell me that access violation
why?

##### Share on other sites
What happens when you do check(0, 0) ?
At the first condition, you look at oil[ i-1][j-1]: you are out of bounds.
What happens when you do check(max, max) ?
At the last condition, you look at oil[ i+1][j+1]: you also are out of bounds.

This is what creates your protection faults.

I will let you find out the rest for yourself (this is how one may learn). Besides through experiments you will ultimately develop the necessary skills. This will be my last answer in this thread: take courage, you are on the right track.

Ghostly yours,
Red.

##### Share on other sites
yes~ i considered that ,please look at my function check:

like "if(j>0 && i>0) check(i-1,j-1)" for keeping from [-1][-1] and so on...

maybe there are sth wrong in the first post ......

##### Share on other sites
YEAH£¡~

i find the problem which cause the loop endless:
for example: check (1,2) made check (0,1) but check(0,1) made check(1,2) and this cause endless check ^^

and my solve is change the position of ''#'' as following:

#include
#include
#pragma hdrstop
using namespace std;

#pragma argsused
ifstream in("oil.in");
char oil[10][10];
int m,n,wait,oilnum=0;

void check(int i,int j)
{
if(oil[j]==''@'') //if it has oil
{ //check 8 direction
oil[i][j]=''#'';
if(i>0 && j>0)
check(i-1,j-1);

if( j>0)
check(i,j-1);

if(i0)
check(i+1,j-1);

if(i>0 )
check(i-1,j);

if(i check(i+1,j);

if(i>0 && j check(i-1,j+1);

if( j check(i,j+1);

if(i check(i+1,j+1);
}
//id checked
}

//-------------------------------------------

int main(int argc, char* argv[])
{
while(in>>m>>n && n)
{
oilnum=0;
for(int i=0;i for(int j=0;j in>>oil[i][j];

for(int i=0;i for(int j=0;j {
if(oil[i][j]!=''#'') //if it isn''t checked
{
if(oil[i][j]!=''*'')
{
check(i,j);
oilnum++;
}
}
}
cout< }
cin>>wait;
return 0;
}

and following is the "oil.in":
1 1
*
3 5
*@*@*
**@**
*@*@*
1 8
@@****@*
5 5
****@
*@@*@
*@**@
@@@*@
@@**@
0 0

i''ll remember it(dom''t make the same mistake ) for ever~

À´×ÔÖÐ¹úµÄÇóÑ§Õß¡­¡­

• ### Forum Statistics

• Total Topics
628710
• Total Posts
2984324

• 23
• 11
• 9
• 13
• 14