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
}
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...
}
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
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.
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)
{
returntrue;
}
returnfalse;
}
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
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
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.
bool CheckForNegativeCosts(int myarray[][4], int m)
{
for (int i=0; i<m; i++)
if (myarray[i][2]<0)
{
returntrue;
}
returnfalse;
}
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