-1

I have a question as below C++ code.

this is the code(No function) that can successful execute.

#include <iostream>
using namespace std;


int main()
{
    int size_3 = 3;
    //arr1 is a static array
    int arr1[3][3] = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} };

    //arr2 is a dynamic array
    int** arr2 = new int* [size_3];
    for (int i = 0; i < size_3; i++) {
        arr2[i] = new int[size_3];
    }

    int val = 9;
    for (int i = 0; i < size_3; i++) {
        for (int j = 0; j < size_3; j++) {
            arr2[i][j] = val--;
        }
    }

    cout << "Elements in matrix 1: " << endl;
    for (int i = 0; i < size_3; i++) {
        for (int j = 0; j < size_3; j++) {
            cout << arr1[i][j] << " ";
        }
        cout << endl;
    }
    cout << endl;
    cout << "Elements in matrix 2: " << endl;
    for (int i = 0; i < size_3; i++) {
        for (int j = 0; j < size_3; j++) {
            cout << arr2[i][j] << " ";
        }
        cout << endl;
    }
    cout << endl;

    int** prod_arr = new int* [size_3];
    for (int i = 0; i < size_3; i++) {
        prod_arr[i] = new int[size_3];
    }


    for (int i = 0; i < size_3; i++) {
        for (int j = 0; j < size_3; j++) {
            prod_arr[i][j] = 0;
            for (int k = 0; k < size_3; k++) {
                prod_arr[i][j] += arr1[i][k] * arr2[k][j];
            }
        }
    }

    cout << "Elements in the product of 2 matrices: " << endl;
    for (int i = 0; i < size_3; i++) {
        for (int j = 0; j < size_3; j++) {
            cout << prod_arr[i][j] << " ";
        }
        cout << endl;
    }

}

However, I am gonna to use the function for the multiplication of the 2 matrices. how could I move it to function, since the first array is static array, the second array is dynamic array.

I have try different way to use pointer , pass-by reference in the function, but still can't work.

My invalid code as below.

#include <iostream>
using namespace std;

int** function(int farr1, int farr2, int size3) {

    int** prod_arr = new int* [size3];
    for (int i = 0; i < size3; i++) {
        prod_arr[i] = new int[size3];
    }

    for (int i = 0; i < size3; i++) {
        for (int j = 0; j < size3; j++) {
            prod_arr[i][j] = 0;
            for (int k = 0; k < size3; k++) {
                prod_arr[i][j] += farr1[i][k] * farr2[k][j];
            }
            return prod_arr[i][j];
        }
    }
}


int main()
{
    int size_3 = 3;
    //arr1 is a static array
    int arr1[3][3] = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} };

    //arr2 is a dynamic array
    int** arr2 = new int* [size_3];
    for (int i = 0; i < size_3; i++) {
        arr2[i] = new int[size_3];
    }

    int val = 9;
    for (int i = 0; i < size_3; i++) {
        for (int j = 0; j < size_3; j++) {
            arr2[i][j] = val--;
        }
    }

    cout << "Elements in matrix 1: " << endl;
    for (int i = 0; i < size_3; i++) {
        for (int j = 0; j < size_3; j++) {
            cout << arr1[i][j] << " ";
        }
        cout << endl;
    }
    cout << endl;
    cout << "Elements in matrix 2: " << endl;
    for (int i = 0; i < size_3; i++) {
        for (int j = 0; j < size_3; j++) {
            cout << arr2[i][j] << " ";
        }
        cout << endl;
    }
    cout << endl;
    int** prod_arr = function(farr1, farr2, size_q3);
    cout << "Elements in the product of 2 matrices: " << endl;
    for (int i = 0; i < size_3; i++) {
        for (int j = 0; j < size_3; j++) {
            cout << prod_arr[i][j] << " ";
        }
        cout << endl;
    }
    cout << endl;
}

I want to use the function to execute the code. I got 2 array, one is static and another is dynamic, How to use pointer and pass to function with this different arrays.

Thanks a lot for your help.

  • 2
    Use std::array and std::vector, forget about all this old style "C" stuff. Dynamically allocated array : std::vector values(8); will allocate 8 ints and you don't even have to remember to call delete[]. A function accepting an array : `void f(const std::vector& values)`... bonus values will know its size and you can easily use it in range based for loops. Returning an array : `std::vector get_values()` – Pepijn Kramer Feb 10 '23 at 09:54
  • it makes no difference whether its a static or dynamically allocated array when passing it to a function. – 463035818_is_not_an_ai Feb 10 '23 at 09:55
  • 1
    In the function `function`, when and where do you `return` from the function? Remember that the `return` statement returns *immediately*. Some quick [rubber duck debugging](https://en.wikipedia.org/wiki/Rubber_duck_debugging) would have been helpful. – Some programmer dude Feb 10 '23 at 09:56
  • you say you want to pass it to the function but the function has only `int` parameter – 463035818_is_not_an_ai Feb 10 '23 at 09:56
  • Whether the array is statically or dynamically allocated doesn't matter, the former will decay to a pointer when you'll pass it to a function. The issue is that your function take `int` instead of `int**`. Same thing in the `main()`, the function call makes no sense, you give arguments that don't exist. – Fareanor Feb 10 '23 at 09:56
  • 2
    It looks like you are learning C++ from an outdated source. Good sources to learn cpp from are : [cppreference](https://en.cppreference.com/w/). A [recent C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) or have a go at https://www.learncpp.com/ (that's pretty decent, and pretty up-to-date). When you've mastered the C++ basics from those sources, look at the [C++ coreguidelines](https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines) regularely to keep up-to-date. – Pepijn Kramer Feb 10 '23 at 09:57
  • I think it is a duplicate of https://stackoverflow.com/questions/8767166/passing-a-2d-array-to-a-c-function but then I dont actually understand what is asked here. Do you get errors? What is "invalid" about the second code? Compiler errors? they should be included in the question – 463035818_is_not_an_ai Feb 10 '23 at 09:57
  • please read about [mcve]. I am certain that your problem can be demonstrated with less code. You posted quite a lot of code, but still I don't see the main problem in it – 463035818_is_not_an_ai Feb 10 '23 at 09:59
  • when you call the function `function(farr1, farr2, size_q3);` what is `farr1` and `farr2` ? They are not declared – 463035818_is_not_an_ai Feb 10 '23 at 10:00
  • I think you need to review the basics about functions and their arguments in your favourite C++ book. – molbdnilo Feb 10 '23 at 10:15

1 Answers1

0

Some C++ array examples to get you started : A 2D dynamically array can be modeled as std::vector<std::vector<int>>

#include <array>        // static array
#include <vector>       // dynamic array, can resize at runtime
#include <iostream>

// https://en.cppreference.com/w/cpp/container/array
// accept a modifiable static array (content can be changed
// the & means by reference, the array will not get copied
// https://isocpp.org/wiki/faq/references
void func1(std::array<int, 4>& values)  
{
    values[2] = 3;
}

// array cannot be modified in body of func2 only used : const
void func2(const std::array<int, 4>& values)
{
    std::cout << values[2] << "\n";
}

// https://en.cppreference.com/w/cpp/container/vector
// return a dynamically allocated array of integers
// it will have a size of 5 when returned
std::vector<int> get_values()
{
    return std::vector<int>{1, 2, 3, 4, 5};
}


int main()
{
    auto values = get_values();
    values.push_back(6); // add another value

    // https://en.cppreference.com/w/cpp/language/range-for
    // prefer to use those if you don't need indices 
    // (which is not as often as you think)
    for (const int value : values)
    {
        std::cout << value << " ";
    }

    return 0;
}
Pepijn Kramer
  • 9,356
  • 2
  • 8
  • 19