0

I'm trying to return the third highest number from a function by sending a 2D array created through smart pointers as a parameter but the problem is, I don't know how to do it. I have been searching through the internet for a possible solution but I couldn't seem to find one, Here's what I did,

int thirdhigh(int array[4][5])
{}

int main()
{
    std::shared_ptr<std::shared_ptr<int[]>[]>array(new std::shared_ptr<int[]>[4]);//creates an array of pointers

    //creates an array of size 5 and sends it addressess to the previous created array of pointers
    for (int rows{ 0 }; rows < 4; ++rows) {
        array[rows] = std::shared_ptr<int[]>(new int[5]);
    }

    //assigns random values to the 2D array created from 1-100
    for (int r{ 0 }; r < 4; ++r) {
        for (int c{ 0 }; c < 5; ++c) {
            array[r][c] = ((rand() % 100) + 1);
        }
    }

    //Shows the values assigned to the 2D array
    for (int r{ 0 }; r < 4; ++r) {
        for (int c{ 0 }; c < 5; ++c) {
            std::cout << array[r][c] << "\t";
        }
        std::cout << std::endl;
    }
    //Returns the third highest number
    thirdhigh(array[4][5]);
}

Also I need to use smart pointers only as I'm trying to learn it. Any suggestions would be helpful. Thanks!

Swordfish
  • 12,971
  • 3
  • 21
  • 43
  • 1
    `std::vector>` seems more appropriate. – Jarod42 Oct 12 '18 at 09:09
  • 1
    Ignoring the smartness of your pointers for a moment, youi probably need to understand the [difference between dimensional arrays and arrays of pointers](https://stackoverflow.com/questions/17823058/difference-between-multi-dimensional-arrays-and-array-of-pointers) – Gem Taylor Oct 12 '18 at 11:01
  • @GemTaylor Thanks for the suggestion ! – Muhammad Ali Khan Oct 12 '18 at 12:43

1 Answers1

1

std::shared_ptr<std::shared_ptr<int[]>[]> is the type of the thing you want to pass to the function, not int[4][5]. Also, since a std::shared_ptr<> doesn't know how many consecutive thingies there are at the memory location it points to, you also have to pass the dimensions to the function.

Swordfish
  • 12,971
  • 3
  • 21
  • 43