# Thread: how to pass 2D array into function..?

1. ## how to pass 2D array into function..?

prototype of function is something like

/*this program takes in input from a file...iterates thru line by line(or char by char depending on how you implement it)...looks for text only...and then prints out a list of each word(TEXT ONLY) and their frequencies. The way I thought to do it was to use a 2D array to store everything. But the memory for the array has to be dynamically allocated. That's where my problems are coming in.*/

void growArray(char *array, int x, int y);

int main()
{
int MAX_SIZE=300;
int x, y;
x=y=0;
char newChar;
char *array = {0};
while ((newChar = cin.get()) != EOF){
if(y>=MAX_SIZE || x>=MAX_SIZE){
growArray(array, x, y);
MAX_SIZE+=MAX_SIZE; /*i fully realize if input line is 700 chars long i'm screwed. so help me. */
}
if(isspace(newChar) || ispunct(newChar)){
x=0; //goes back to element zero
y++; //but of the next row!
}
array[x][y]=newChar;
x++;
}

/*here's supposed to be the code to iterate thru 2D array, compare each one, SORT, and then print out the list. so it'll look like:
As 1
Bye 1
aloha 3
wassup 5
You get the idea.... help down for this part is appreciated too, especially the sorting. I have no idea how to bubble sort a 2D array */

return 0;
}

void growArray(char *array, int x, int y)
{
char *temp = new char[x][y]; /*err...don't quite remember what i put... */

for(int nCount=0; nCount < x; nCount++)
for(int mCount=0; mCount < y; mCount++)
temp[nCount][mCount] = array[nCount][mCount];

free(array ) /*this line was supposed to clean out old array. but i don't know how. help. */

array = temp;
}

/*So basically I got error saying i can't pass in char(*) to function char *array or something like that. I'm doing this on Unix by the way. Any help is much much appreciated. Thanks.*/ 2. Have you considered the use of linked lists, or are you using arrays because thats all you know, or are allowed to use in this case? 3. if you must use 2D array then you can declare a 2D array in several different ways.

char array1[][]
char *array2[][]
char ** array3

however, this is a 3D array
char * array4[][]

If you are going to declare the array dynamically then you can do this:
int i, x, y
cin x, y

char ** array5

array5 = new char * [x];
for(i = 0; i < x; i++)
{
array5[i] = new char[y];
}

and when you are done with the dynamically created 2D array you use this to release the memory

for(i = 0; i < x; i++)
{
delete [] array5[i];
}
delete [] array5

to use the 2D array as a function parameter you can do this:

void display(char [row][col]) where row and column are constants. Technically I believe you can leave the last set of [] blank if you wish.

then to call the function passing the array you can do this:
char array6[row][col]
display(array6);

However, I agree with Salem, an expandable container such as a list or a vector, etc. would probably be a better choice for this project. In addition I would probably use a struct with two data members, one for the unique word and one as a counter, for this project. Popular pages Recent additions 