0

Started my adventure with programing fairly recently so please dont be too harsh.

I planed to create a 2D array that is filled with 0 (ultimately I will fil it with various numbers but not important right now). The size of array should be input by user. So I use pointers. I found some of the examples here on StackOverflow, tried using it and the code was buggy - sometimes it worked, sometimes it didnt.

It never works when rows = columns. It also never works when with second iteration of code You switch columns with rows eg:

Run code first time - call rows 3, columns 9, end program.

Run code second time - call rows 9, columns 3 - no execution.

Source I was using:Declaring a pointer to multidimensional array and allocating the array

#include <iostream> 

using namespace std;

void dynamicArray(int** array, int columns, int rows)

{
    
cout << " i am working" << endl;

        for (int i=0; i<rows ; ++i)
        {
            for (int j=0; j <columns ;j++)
            {
                array [columns][rows] = 0;
                cout << array [columns][rows] << "  "  << flush ;
            }
            cout << endl;
        }

}

int main() {

    int columns = 0;
    int rows = 0;
    cout << " state the number of rows: " << endl;
    cin >> rows;
    cout << " state the number of columns: " << endl;
    cin >> columns;

        int **array = new int*[rows];                   
        for(int i = 0; i < rows; ++i)
        {
            array[i] = new int[columns];
        }

        dynamicArray(array, columns, rows);

        for(int l = 0; l < rows; l++)

        delete [] array[l];
        delete [] array;

        return 0;

    }

I quite frankly have no idea how to fix it, hope for your help

BR, Mark

MikeCAT
  • 73,922
  • 11
  • 45
  • 70
  • The best thing you can learn on your quest to C++ guru-dom, is learn how to use your debugger. If you used your debugger to run your program, your debugger would've easily shown you why your program crashes, and why. Knowing how to effectively use a debugger is a mandatory skill for every C++ developer. Although I immediately saw one of the bugs, I did not immediately see the other one. So, I simply ran your program in my debugger, and got my answer in just a few seconds. C++ is just too complicated, this won't be your first mysterious crash. This is what a debugger is for. – Sam Varshavchik Jul 25 '22 at 23:23
  • 1
    Note that there are no 2D arrays in the posted code. What there is instead is a 1D array of pointers, each of which points to a different 1D array. – Jeremy Friesner Jul 25 '22 at 23:28
  • 1
    Side note: either use the optional braces or make sure you properly indent the code. The `for` loop near the end of `main` looks goofy, and goofy looking code often results in goofy errors, sooner or later. – user4581301 Jul 25 '22 at 23:33
  • 1
    Since this is C++ you can use std::vector> array(rows, std::vector(cols, 0)); – QuentinUK Jul 25 '22 at 23:35

1 Answers1

4

Generally using the size as the index can lead to out-of-range access.

Also the order of rows and columns looks swapped.

array [columns][rows] in the function dynamicArray should be array[i][j].

MikeCAT
  • 73,922
  • 11
  • 45
  • 70