0

My code is supposed to count how many negative elements are within my array. However, I keep getting hit with this error " cannot convert 'double ()[3]' to 'int' for argument '1' to 'double numberNegative(int*, int, int)' "

#include <iostream>
using namespace std;


int numberNegative(double arr[], int r, int c);
int counter = 0;

int main(){
    double data[2][3] = {{-3.0, 1, 4.5}, {-2.2, 7, 1.4}};
    cout<<numberNegative(data, 2, 3)<<endl;
    return 0;
}


int numberNegative(double arr[], int r, int c){
    for (int i = 0; i < r; i++){
        for (int j = 0; j <= c; j++){
            if (arr[i] < 0) counter++;
            if (arr[i][j] < 0) counter++;
        }
        return counter;
    }
}
RoiMinuit
  • 404
  • 4
  • 17
  • Your function takes `double[]` and you feed it `double[2][3]`. Perhaps call that twice, once for each bit of `data`? – tadman Apr 24 '20 at 01:50
  • 1
    Reflection: Is treating a `double` as an `int` what you want? Why did you choose one type and not the other in that case? Why do you forward declare `numberNegative`? Put `main` last and get over it. – Ted Lyngmo Apr 24 '20 at 02:14
  • 1
    @TedLyngmo completely agree. If using a single source, just declare the functions above `main()` in the order they are needed if the functions reference each other and get rid of the forward declarations (though many courses are taught introducing forward declarations above `main()` to make the transition to creating a header file for them a bit easier -- they just don't explain that part very well....) – David C. Rankin Apr 24 '20 at 02:43
  • @DavidC.Rankin I think we're on the same page but with different approaches. If I find myself in a translation unit where `main` is and that makes me forward declaring functions...? No. Forward declarations in "main" for "main"s sake is just wrong. – Ted Lyngmo Apr 24 '20 at 03:03

1 Answers1

1

Well -- you should listen to your compiler -- it is correct.

When you have a 2D array (which is actually and array of array double [3]), on access, the first level of indirection (the first [..]) is converted to a pointer to the first element. So double arr[2][3]; is converted to a pointer-to-array double[3]. (a pointer to the first array of the 2 double[3] arrays). The type for a pointer-to-array double[3] is double (*)[3] -- just as your compiler is telling you.

To make things work correctly, your prototype should be:

int numberNegative (double (*arr)[3], int r, int c);

Then within your function, you can count the number of negative values with:

    for (int i = 0; i < r; i++) {
        for (int j = 0; j < c; j++) {
            if (arr[i][j] < 0)
                counter++;
        }
    }
    return counter;

Note: j < c NOT j <= c (which exceeds the bounds of the array by 1) and no if (arr[i] < 0) counter++; which attempts to compare an pointer to double to 0.

Further, you cannot have return counter; within your for loop and expect to return the correct value for all negative values in your 2D array. Where you do:

for (int i = 0; i < r; i++){
    ...
    return counter;
}

You need to move return counter; out of the loops to the end (last statement) in your function.

There is no need for a global int counter; and see Why is “using namespace std;” considered bad practice?

Putting it altogether, you would have:

#include <iostream>

int numberNegative (double (*arr)[3], int r, int c);

int main (void) {

    double data[2][3] = {{-3.0, 1, 4.5}, {-2.2, 7, 1.4}};

    std::cout << numberNegative (data, 2, 3) << '\n';
}

int numberNegative (double (*arr)[3], int r, int c)
{
    int counter = 0;

    for (int i = 0; i < r; i++) {
        for (int j = 0; j < c; j++) {
            if (arr[i][j] < 0)
                counter++;
        }
    }
    return counter;
}

See also: C++: “std::endl” vs “\n”

Example Use/Output

$ ./bin/count_double_neg
2

Which corresponds to -3.0 and -2.2. Look things over and let me know if you have any questions.

David C. Rankin
  • 81,885
  • 6
  • 58
  • 85
  • Hi David, this was a very detailed answer. I am a freshman so all of this is very hard to comprehend, but I am getting there. Your correction works, but I get 3 instead of 2. I have two negative elements in the array. – RoiMinuit Apr 24 '20 at 02:50
  • Huh? How do you get `3`?? So you initialize `int counter = 1;` or do you have the `<=` where the `<` goes in `j < c`? You can copy/paste what I have above and compile it and confirm you get `2`. You have some subtle issue in your code. Go line-by-line and look for any differences. This is also a great opportunity to learn how to *Talk to the Duck* - see [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). No kidding -- it helps `:)` – David C. Rankin Apr 24 '20 at 02:53
  • Got it! The 'j<=' was the mistake. Thanks a lot! Very tedious but we pulled it off! – RoiMinuit Apr 24 '20 at 02:55
  • Good deal! Good luck with your coding! Learning C++ (or C) isn't a race -- there is a lot to both languages. It is more a journey -- so enjoy it. – David C. Rankin Apr 24 '20 at 02:56