Another array question - checking for negative values

Maybe my brain is just done for the night, but I am having trouble with this simple concept: check the values in a specific column of an array - IF a negative value exists DO x, ELSE DO y.

I am able to get the program to recognize when a negative number is found and go to the right function. Any help on how to recognize if no negative values are found?

1
2
3
4
5
6
7
8
for (int i=0; i<m; i++)
        if (myarray[i][2] < 0)  // check if any value in the 3rd column is negative                    
        {
           cout << "Negative Costs Found." << endl;
           // run function x;
           break;   // no need to check if more than 1 negative value exists
        }

1
2
3
4
5
6
7
8
9
10
11
for (int i=0; i<m; i++)
        if (myarray[i][2] < 0)  // check if any value in the 3rd column is negative                    
        {
           cout << "Negative Costs Found." << endl;
           // run function x;
           break;   // no need to check if more than 1 negative value exists
        }
        else
        {
          // do something...
        }
closed account (D80DSL3A)
or perhaps
1
2
3
4
5
6
7
8
9
10
11
12
13
14
bool negFound = false;
for (int i=0; i<m; i++)
{
       if (myarray[i][2] < 0)  // check if any value in the 3rd column is negative                    
       {
          cout << "Negative Costs Found." << endl;
          // run function x;
         negFound = true;
       }
	if(negFound)
		break;   // no need to check if more than 1 negative value exists
}
if(negFound)
	// run function y 
@coder777: that's not what the OP wants.

@fun2code: you could do away with the second if statement and just break out of the loop when you set negFound to true, as the OP was doing.

This really belongs in a function:

1
2
3
4
5
6
7
8
bool negInColumn(int arr[][], int rows, int col)
{
    for(int i = 0; i < rows; ++i)
    {
        if(arr[i][col] < 0) return true;
    }
    return false;
}
I like the way filipe approaches the problem, but I'm quite sure that what he posted won't compile :P
1
2
if( is_there_a_negative(myarray, rows, col) )  func1();
else func2();

Or if the functions have the same prototype
1
2
3
4
5
6
7
8
9
10
11
Func ptr;
ptr=pos_func;
for (int i=0; i<m; i++)
{
       if (myarray[i][2] < 0)  // check if any value in the 3rd column is negative                    
       {
          ptr = neg_func;
          break;
       }
}
ptr(); //run the function  


Edit: I can't remember why it took me so long to write the code. I agree with filipe approach.
Last edited on
Damn you "multidimensional" array.
Is this safe?
1
2
3
4
5
6
7
bool negInColumn(int *arr, int rows, int cols, int C){
	for(int i = 0; i < rows; ++i)
		if( (arr+cols*i)[C] < 0) return true;
	return false;
}

negInColumn( *myarray, rows, cols, 2);
Yes, I think this will work.

If the size of the array is known at compile time, you can also do something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
using namespace std;

template <int Rows, int Cols>
void print(int arr[Rows][Cols], int c)
{
     for (int i=0; i<Rows; i++)
          cout << arr[i][c] << endl;
}

int main()
{
     int arr[2][3];

     for (int i=0; i<2; i++)
          for (int j=0; j<3; j++)
               arr[i][j]=j;

     print<2,3>(arr,1);
     return 0;
}

If not, you can use a vector of vectors.
Last edited on
Thanks for the correction.
I would like to use this as a function, similar to what filipe and ne555 did. (The size of the array is not known at compile time since it is based on user input.)

Currently I have my function similar to filepe's, but it always returns TRUE. I don't quite understand the difference between filipe's and ne555's functions. This is what I now have:
1
2
3
4
5
6
7
8
9
bool CheckForNegativeCosts(int myarray[][4], int n, int m)
{
    for (int i=0; i<m; i++)
        if (myarray[i][2]<0)
        {
             return true;
        }
        return false;
}


then, in my function...
1
2
3
4
5
6
7
CheckForNegativeCosts(ForwardStar,n,m); 
      if (CheckForNegativeCosts)
           cout << "Negative costs found";
           // run function x
      else
           cout << "No Negative costs found";
           // run function y 
Last edited on
If not, you can use a vector of vectors.


How about just using a matrix class?

@zach2123:
I know this seems obvious, but if your array contained even one negative number in that row then true would always be returned.
1
2
CheckForNegativeCosts(ForwardStar,n,m); 
      if (CheckForNegativeCosts)
In the if you are checking if the pointer to the function is or not null.
if( CheckForNegativeCosts(ForwardStar,n,m) ) Here is checking the value returned by the function.

bool CheckForNegativeCosts(int myarray[][4], int n, int m) That will only work if your matrix has 4 columns. Why are you passing n if you aren't using it?
bool negInColumn(int *arr, int rows, int cols, int C) Here I'm making a conversion from a two dimensional matrix to a 1 dimensional array (note that I call negInColumn(*array, rows, cols, 2);) This is suppose to work with two dimensional matrix with any numbers of columns
Last edited on
Thanks so much! That did it.

You were right, I did not need int n passed, this was left over from another function call I had. As for my matrix, it will always be mx4. It is good though to see how I can modify it to work with any size matrix.

Thanks again for the help!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
bool CheckForNegativeCosts(int myarray[][4], int m)
{
    for (int i=0; i<m; i++)
        if (myarray[i][2]<0)
        {
             return true;
        }
        return false;
}

int main()

// ,,,

CheckForNegativeCosts(myarray,m); 
      if (CheckForNegativeCosts(myarray,m))
           cout << "Negative costs found";
           // run function x
      else
           cout << "No Negative costs found";
           // run function y  
Topic archived. No new replies allowed.