how to transfer para of 2 dimension array?

Started by
7 comments, last by littlewater 19 years, 11 months ago
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[j]='@') //if it has oil { if(i>0 && j>0) check(oil[i-1][j-1]); //check 8 direction if( j>0) check(oil[j-1]); if(i<m && j>0) check(oil[i+1][j-1]); if(i>0 ) check(oil[i-1][j]); if(i<m ) check(oil[i+1][j]); if(i>0 && j<n) check(oil[i-1][j+1]); if( j<n) check(oil[j+1]); if(i<m && j<n) check(oil[i+1][j+1]); } oil[j]='#'; //id checked } //——————————————- int main(int argc, char* argv[]) { while(in>>m>>n && n) { for(int i=0;i<m;i++) for(int j=0;j<n;j++) in>>oil[j]; for(int i=0;i<m;i++) for(int j=0;j<n;j++) { if(oil[j]!='#') //if it isn't checked { check(oil[j]); oilnum++; } } cout<<oilnum<<endl; } cin>>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~ </i> <SPAN CLASS=editedby>[edited by - littlewater on May 18, 2004 11:56:41 AM]</SPAN>
À´×ÔÖйúµÄÇóѧÕß¡­¡­
Advertisement
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.
Ghostly yours,Red.
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[j-1]) and so on ……<br><br>i can''t …… <br><br>maybe i can pass i and j using as the 2nd and 3rd para?? </i>
À´×ÔÖйúµÄÇóѧÕß¡­¡­
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[j])<br>   </pre>   <br>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.<br><br>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.<br><br>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:<br><br>void check (int i, int j)<br><br>You do not need to pass oil as a parameter since it is a global variable.<br><br>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.<br>You do not only store data in memory, but you also can store memory adresses.<br><br>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<br><pre> <br>a = x where a is an array of Type<br>   </pre>   <br>the computer understands <br><pre><br>*(a + i*sizeof(Type)) = x<br>   </pre>   <br>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.<br><br>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).       <br><br>Ghostly yours,<br>Red.   <br><br><SPAN CLASS=editedby>[edited by - Red Ghost on May 19, 2004 3:04:49 AM]</SPAN>   <br><br><SPAN CLASS=editedby>[edited by - Red Ghost on May 19, 2004 3:05:21 AM]</SPAN>
Ghostly yours,Red.
thx~ i''ll try to find some...

À´×ÔÖйúµÄÇóѧÕß¡­¡­
À´×ÔÖйúµÄÇóѧÕß¡­¡­
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?

À´×ÔÖйúµÄÇóѧÕß¡­¡­
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.
Ghostly yours,Red.
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 ......
À´×ÔÖйúµÄÇóѧÕß¡­¡­
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[j]=''#'';<br> if(i>0 && j>0)<br> check(i-1,j-1);<br><br> if( j>0)<br> check(i,j-1);<br><br> if(i<m-1 && j>0)<br> check(i+1,j-1);<br><br> if(i>0 )<br> check(i-1,j);<br><br> if(i<m-1 )<br> check(i+1,j);<br><br> if(i>0 && j<n-1)<br> check(i-1,j+1);<br><br> if( j<n-1)<br> check(i,j+1);<br><br> if(i<m-1 && j<n-1)<br> check(i+1,j+1);<br> }<br> //id checked<br>}<br><br>//——————————————-<br><br>int main(int argc, char* argv[])<br>{<br> while(in>>m>>n && n)<br> {<br> oilnum=0;<br> for(int i=0;i<m;i++)<br> for(int j=0;j<n;j++)<br> in>>oil[j];<br><br> for(int i=0;i<m;i++)<br> for(int j=0;j<n;j++)<br> {<br> if(oil[j]!=''#'') //if it isn''t checked<br> {<br> if(oil[j]!=''*'')<br> {<br> check(i,j);<br> oilnum++;<br> }<br> }<br> }<br> cout<<oilnum<<endl;<br> }<br> cin>>wait;<br> return 0;<br>}<br><br>and following is the "oil.in":<br>1 1<br>*<br>3 5<br>*@*@*<br>**@**<br>*@*@*<br>1 8<br>@@****@*<br>5 5<br>****@<br>*@@*@<br>*@**@<br>@@@*@<br>@@**@<br>0 0<br><br>i''ll remember it(dom''t make the same mistake ) for ever~ </i> <br><br> À´×ÔÖйúµÄÇóѧÕß¡­¡­
À´×ÔÖйúµÄÇóѧÕß¡­¡­

This topic is closed to new replies.

Advertisement