how to make a 2D Dyanmic array in function without using pointers

#include<iostream>
using namespace std;

int addition(int sohaib[100][100], int waleed[100][100]) {
    int row, column;
    int sum[100][100];
    for (int i = 0; i < 5; i++)
    {
        for (int j = 0; j < 5; j++) {
            sum[i][j] = sohaib[i][j] + waleed[i][j];
        }
    }
    for (int i = 0; i < row; i++)
    {
        for (int j = 0; j < column; j++) {
            cout << sum[i][j] << "\t";
        }
    }
}


int main() {
    int row, column;
    cout << "Enter the Rows and columns.   "; cin >> row >> column;
    int sohaib[100][100];
    int waleed[100][100];
    for (int i = 0; i < row; i++) {
        for (int j = 0; j < column; j++) {
            cin >> sohaib[i][j];
            cin >> waleed[i][j];
        }
    }
    addition(sohaib, waleed);
    return 0;
}

Please help with this code i finally managed to make it work but it doesnt display. And before this it was int addition(int sohaib[row][column], int waleed[row][column]) not working tho. Help.

1 answer

  • answered 2022-01-21 13:57 AudioDroid

    Ok. So here is my suggested solution. This code is not using C++ containers such as std::vector or others. But it works dynamically, i.e. your matrices can be any size. If I read things correctly in the comments and assuming this is to do with a class, teaching C/C++, I think this is to get a deeper understanding of pointers, before making use of the convenient containers in C++. This way you get an understanding of what they do under the surface. It is not as easy as it might seem and I left some commented out code so you can see how I found a quick way of testing it without having to type in numbers every time. To understand how the code actually works read about and play with pointers! You will not learn by just copying this code.

    #include<iostream>
    using namespace std;
    
    int** createMatrix(int numRows, int numColumns)
    {
        int** matrix;
        matrix = new int *[numRows];
    
        for (int r = 0; r < numRows; r++)
            matrix[r] = new int[numColumns];
    
        return matrix;
    }
    
    void deleteMatrix(int **matrix)
    {    
        for (int r = 0; r < numRows; r++)
           delete[] matrix[r];
    
        delete[] matrix;
    }
    
    void sumMatrix(int** matrixA, int** matrixB, int** const sum, int numRows, int numColumns)
    {
        for (int i = 0; i < numRows; i++)
        {
            for (int j = 0; j < numColumns; j++) {
                sum[i][j] = matrixA[i][j] + matrixB[i][j];
            }
        }
    }
    
    void printMatrix(int **matrix, int numRows, int numColumns)
    {
        cout << endl;
        for (int i = 0; i < numRows; i++)
        {
            for (int j = 0; j < numColumns; j++) {
                cout << matrix[i][j] << "\t";
            }
            cout << endl;
        }
    }
    
    int main() {
        
        //int numRows = 2, numColumns = 2; 
        int numRows, numColumns;
        cout << "Enter the number of rows: "; 
        cin >> numRows;
    
        cout << "Enter the number of columns: ";
        cin >> numColumns;
    
        int** sohaib = createMatrix(numRows, numColumns);
        int** waleed = createMatrix(numRows, numColumns);
        int** sum = createMatrix(numRows, numColumns);
    
        for (int i = 0; i < numRows; i++) {
            for (int j = 0; j < numColumns; j++) {
                //sohaib[i][j] = 1;
                //waleed[i][j] = 2;
                cin >> sohaib[i][j];
                cin >> waleed[i][j];
            }
        }
    
        sumMatrix(sohaib, waleed, sum, numRows, numColumns);
    
        printMatrix(sum, numRows, numColumns);
    
        deleteMatrix(sohaib);
        deleteMatrix(waleed);
        deleteMatrix(sum);
        
        return 0;
    }
    

How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum