#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Location
    Vancouver, WA
    Posts
    2
    Rep Power
    0

    Dynamic 2D Array as Function Argument


    I am tying figure out how to properly code the function definitions printArray and modifyArray below. As I have the code written, it appears that I must use the literal constant (in this case 2). My goal is to be able to pass col to the function rather than "2".

    As is likely obvious, I new to C++ and any push in the right direction will be appreciated.

    #include <iostream>

    using namespace std;

    main() {

    // Array size
    int const col = 2;
    int row = 5;

    void printArray(int, int const, double [][col]);
    void modifyArray(int, int const, double [][col]);

    // Create array
    double (*arr)[col] = new double[row][col];
    for(int i=0;i<row;i++){
    arr[i][0] = i;
    arr[i][1] = i+1;
    }

    // Print original array
    cout << "Original array" << endl;
    printArray(row, col, arr);

    // Modify array
    modifyArray(row, col, arr);

    // Print modified array
    cout << "Modified array" << endl;
    printArray(row, col, arr);

    // Free memory
    delete[] arr;

    }

    // Modify array
    void modifyArray(int row, int const col, double arr[][2]){
    for(int i=0;i<row;i++){
    for(int j=0;j<col;j++){
    arr[i][j] += 5;
    }
    }
    return;
    }

    // Print array
    void printArray(int row, int const col, double arr[][2]){
    for(int i=0; i<row; i++){
    for(int j=0; j<col; j++){
    cout << arr[i][j] << '\t';
    }
    cout << endl;
    }
    return;
    }
  2. #2
  3. I'm Baaaaaaack!
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    5,538
    Rep Power
    243
    First, please use "code" tags when posting code (see http://forums.devshed.com/misc.php?action=bbcode&s=). Second, while many compilers will not argue about declaring a function within another function, it is very confusing to most programmers, you should put your declarations at the top of the file, outside main(). Third, in your function declaration and definition, if you use "double **arr" you should be able to get the compiler to happily compile and use your 2d array (a 3d array would be "double ***arr", and so on). Here is some (mostly) working code:

    Code:
    #include <iostream> 
    
    using namespace std; 
    
    void printArray(int rows, int cols, double **arr); 
    void modifyArray(int rows, int cols, double **arr); 
    
    int main() { 
        // Array size 
        int col = 2; 
        int row = 5; 
        int i, j;
        double **arr;
    
        // allocate memory for array 
        arr = new double*[row]; 
        for(i=0;i<row;i++){
            arr[i] = new double[col];
        }
        for(i=0;i<row;i++){ 
            for(j=0;j<row;j++){ 
                arr[i][j] = i+j; 
            }
        } 
    
        // Print original array 
        cout << "Original array" << endl; 
        printArray(row, col, arr); 
    
        // Modify array 
        modifyArray(row, col, arr); 
    
        // Print modified array 
        cout << "Modified array" << endl; 
        printArray(row, col, arr); 
    
        // Free memory 
        // as far as I can tell, this should be correct
        // but I am getting a crash here.
        for(i=0;i<row;i++){
            delete[] arr[i];
        }
        delete[] arr;
    
        return 0;
    } 
    
    // Modify array 
    void modifyArray(int rows, int cols, double **arr){ 
        for(int i=0;i<rows;i++){ 
            for(int j=0;j<cols;j++){ 
                arr[i][j] += 5; 
            } 
        } 
        return; 
    } 
    
    // Print array 
    void printArray(int rows, int cols, double **arr){ 
        for(int i=0; i<rows; i++){ 
            for(int j=0; j<cols; j++){ 
                cout << arr[i][j] << '\t'; 
            } 
            cout << endl; 
        } 
        return; 
    }

    My blog, The Fount of Useless Information http://sol-biotech.com/wordpress/
    Free code: http://sol-biotech.com/code/.
    Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
    Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.
    LinkedIn Profile: http://www.linkedin.com/in/keithoxenrider

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Location
    Vancouver, WA
    Posts
    2
    Rep Power
    0
    mitakeet

    My apologies regarding the code tags.

    Thanks for the help. Using your changes I'll now be able to go back to the books to figure out why it works.

IMN logo majestic logo threadwatch logo seochat tools logo