0
#include <iostream>
#include <vector>
#include <ctime>
#include <stdlib.h>



using namespace std;  

void displayArray(int elements[][5], int numCol, int numRow) {
    for (int row = 0; row < numRow; row++) {
        for (int col = 0; col < numCol; col++) {
            cout << elements[row][col] << " ";
        }
        cout << endl;
    }

}

int findMin(int arr[], int n) {
    
    int mini = arr[0];


    for (int i = 0; i < n; i++) {
        if (arr[i] < mini) {
            mini = arr[i];
        }

        return { mini };
    }
}

    int findMax(int arr[], int n) {
        
        int maxi = arr[0];

        for (int i = 0; i < n; i++) {
            if (arr[i] > maxi) {
                maxi = arr[i];
            }
        }
        return { maxi };
    }

 int main()
 {
     const int numCol = 5;
     const int numRow = 5;
     int elements[numCol][numRow] = { { 81, 27, 83, 89, 92}, {87, 76, 84, 98, 99}, {83, 47, 89, 42, 48}, {75, 96, 76, 34 ,38}, {98, 83, 76, 27, 29} };

     int N = sizeof(elements) / sizeof(elements[0]);
    
     displayArray(elements, numCol, numRow);
     

     cout << "Minimum is: " << findMin(elements, N);

 }

So currently, when executing this code I get an error specifying that my parameters are incompatible C++ argument of type is incompatible with parameter of type. I'm currently stuck as to why I'm getting this issue. My hypothesis is that I cannot do this with a normal array, and may have to use a vector array in order to, achieve the end goal of producing the smallest number in the array and the largest.

John
  • 1
  • 1
    Recommendation: Reproduce the error message verbatim as text. It speed things up greatly and makes the question easer to find for future askers with the same problem. – user4581301 Aug 24 '22 at 13:54
  • 1
    You are attempting to pass a two-dimensional array to a function that takes a single dimension array, or a plain pointer, as a parameter. This is not allowed in C++, C++ simply does not work this way. Additionally, once you fix this compilation error, you will discover that `N` is not 25, but 5. Perhaps this will be a useful hint to you as to what the underlying fundamental problem is. – Sam Varshavchik Aug 24 '22 at 13:56
  • A 2D array does not decay to a pointer to `int`. It decays to a pointer to an array of `int` (`int (*)[5]` in this case) – user4581301 Aug 24 '22 at 13:56
  • 3
    `return { mini };` is inside the for() loop. Meaning it ends your function on the first iteration. I voted to close as a trivial typo. – drescherjm Aug 24 '22 at 13:58
  • *I'm having trouble with my function for finding the lowest value in a 5x5 two dimensional array* `std::cout << *std::min_element(&elements[0][0], &elements[numCol-1][numRow]);`. A 2D-array lays out the data contiguously. – PaulMcKenzie Aug 24 '22 at 14:00
  • [The full compiler error makes the problem reasonably clear](https://godbolt.org/z/Pq1dE5d5s), though the suggestion from Visual Studio of using a cast is a questionable. Examine the diagnostic messages carefully and your job is usually easier. Much easier once you get more used to interpreting the diagnostics, but that comes with practice. – user4581301 Aug 24 '22 at 14:01
  • 1
    `std::array` can be passed to functions without such pain – 463035818_is_not_an_ai Aug 24 '22 at 14:08
  • 1
    Either you have a 2D array, or you have a 1D array. You cannot have the same array both ways. – n. m. could be an AI Aug 24 '22 at 14:31

1 Answers1

0

Since two-dimensional arrays have a contiguous memory layout, they are actually one-dimensional arrays under the the hood.

Thus you can still call your findMin and findMax functions without changing their signatures. The trick is to pass the address of the first element of the 2D array:

int findMin(int arr[], int n) 
{
  //...
}

int main()
{
     const int numCol = 5;
     const int numRow = 5;
     int elements[numCol][numRow] = { { 81, 27, 83, 89, 92}, {87, 76, 84, 98, 99}, {83, 47, 89, 42, 48}, {75, 96, 76, 34 ,38}, {98, 83, 76, 27, 29} };
     int N = sizeof(elements) / sizeof(elements[0]);
     displayArray(elements, numCol, numRow);

     // Note that we are using the address of the first item
     cout << "Minimum is: " << findMin(&elements[0][0], N);
}

The second issue is that your findMin function is wrong. You are returning prematurely instead of returning after the for loop is completed.

Instead of doing all of that work, use std::min_element:

#include <algorithm>
//...
int findMin(int arr[], int n) 
{
    return *std::min_element(arr, arr + n);
}

The same with findMax, except use std::max_element:

#include <algorithm>
//...
int findMax(int arr[], int n) 
{
    return *std::max_element(arr, arr + n);
}

Note, there were no std::vector's being used -- everything used the original 2D array.

Live Example

PaulMcKenzie
  • 34,698
  • 4
  • 24
  • 45
  • 1
    It is Undefined Behaviour to tread a 2D array as one big 1D array. – n. m. could be an AI Aug 24 '22 at 14:31
  • Maybe I'm mistaken, but I always thought that C++ would follow C in terms of memory layout of 2D arrays. Here is [an answer](https://stackoverflow.com/questions/2565039/how-are-multi-dimensional-arrays-formatted-in-memory) suggesting as such. If I'm wrong, then ok, I learned something today. – PaulMcKenzie Aug 24 '22 at 18:51
  • It is UB in C too. The C standard text has a very explicit mention of it in the list of all the UBs: "An array subscript is out of range, even if an object is apparently accessible with the given subscript (as in the lvalue expression `a[1][7]` given the declaration `int a[4][5]`) (6.5.6)." – n. m. could be an AI Aug 24 '22 at 19:48