passing 2D array to a function using double pointers?

So I have to write a program that will count the cubic values of two-dimensional array elements. The main program should load matrix dimensions (the user must re-enter it until the values are between 2 and 10). Then dynamically allocate the memory for the two-dimensional array.

I know how to do this until this part.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
  #include <iostream>
const int r = 100;
const int c = 100;

using namespace std;

int main()
{
    int  r, c;
    do {
        cout<<"Size of rows and columns in between [2,10]"<<endl;
        cin>>r>>c;
    } while (r<2 || r>10 || c<2 || c>10);


    int ** mat;
    mat = new int*[r];
	for(int i = 0; i < r; i++)
		mat[i] = new int[c];

    for(int j = 0; j < r; j++)
		for(int i = 0; i < r; i++)
			mat[i][j] = 0;


    return 0;
}


Now I have to call the function where the array values will be loaded (the user inputs), call a function that calculates the cubic values of the matrix values, save them into a newly dynamically allocated two-dimensional array and return the pointer to that new two-dimensional field.

Help plss

closed account (48T7M4Gy)
Here is a start to how you can declare you dynamic 2D array and pass it around for processing in functions. One function you will need and see results straight away is a display() function using appropriate parameters.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>


void populate(int**, int, int);


int main()
{
    int  rows, cols;
    
    std::cout << "Enter no. of rows and columns:\n";
    std::cin >> rows >> cols;
    
    int **matrix;
    
    matrix = new int*[rows];
    for(int i = 0; i < rows; i++)
        matrix[i] = new int[cols];
    
    populate(matrix, rows,cols);

    return 0;
}


void populate(int** aMatrix, int no_rows, int no_cols){
    for( int i = 0; i < no_rows; ++i)
        for( int j = 0; j < no_cols; ++j)
            aMatrix[i][j] = i + 2*j - 1;
}
Topic archived. No new replies allowed.