how to pass 2D array into function..?

This is a discussion on how to pass 2D array into function..? within the C++ Programming forums, part of the General Programming Boards category; prototype of function is something like /*this program takes in input from a file...iterates thru line by line(or char by ...

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    2

    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[128][128] = {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(*)[128] 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. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    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?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Unregistered
    Guest
    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 subscribe to a feed

Similar Threads

  1. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  2. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  3. Passing a 2d Array of pointers to a Function
    By miclus in forum C Programming
    Replies: 6
    Last Post: 09-11-2004, 07:34 AM
  4. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  5. returning a 2D array in a function
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 01-05-2002, 07:56 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21