positiveMax = 0.0;
for (i=0; i <= n-1; i++)
{
if(x[i] > 0.0)
x[i] = positiveMax;
}
This code goes through the entire array and if it finds a value bigger than zero, sets that element to zero. So afterwards, the array must contain only negative values, or zero.
Well, what did you want the code to do? I guess the intention was to leave the array unchanged, that is don't do x[i] = something , and instead you wanted to change the value stored in positiveMax, so i guess you might want positiveMax = something
I see what you're saying. I'm trying to find the maximum positive/negative and minimum positive/negative values in the array. I know I need to set the max value to zero in the for loop, but I'm not sure what to do after that.
You need to set the max value to some initial value, before the loop begins. Then inside the loop, if you find an element with a value greater than the current max, update the max with that new value.
The initial value of max (before the loop starts) can be set equal to the first element of the array.
Edit: sorry, I missed that you were dealing with positive and negative values separately. In that case, yes you would set the initial value to zero.
When it comes to finding the minimum positive value (in a separate loop), the initial value of the minimum could be set to INT_MAX.
I'm pretty sure everything you told me before was exactly correct because it gave me exactly the correct output. If I change it like you mentioned above, I get the maximum negative value. If I leave it like I had it, I get the value of the first value in the array. I don't understand why, however.
If I do it like that, I get the same value for my max and min negative values, which is understandable from the coding I have. I'm getting the correct output from my data files for the first three though. Any suggestions?
Just for the record, I disagree with catfish's argument. But I've explained my reasons before.. and won't waste OP's time with it now.
@Cryptik
That looks very good.
You will notice that a lot of your code is identical, except for the variable used and the condition being tested. You could combine those loops into one single loop, but I don't think that you need to worry about it.
That got it. Thanks a lot Chervil for all your help. Except for when every value in the array is negative, I get a very large number for the minimum positive value for some reason. Any ideas?