Maximum and Minimum in Arrays

Pages: 123
cin.sync(); need not do anything at all
http://en.cppreference.com/w/cpp/io/basic_istream/sync

What cin.rdbuf()->in_avail() returns is also entirely up to the implementation.
http://www.cplusplus.com/forum/general/74355/

sync() followed by in_avail() works with the Microsoft implementation.
Here is what I have now for the max and min part. For some reason every value I'm getting is 0. Any suggestions?

void maxMin (double x[], int n)
{
int i;

double positiveMax;
double positiveMin;
double negativeMax;
double negativeMin;

positiveMax = 0.0;
for (i=0; i <= n-1; i++)
{
if(x[i] > 0.0)
x[i] = positiveMax;
}
cout << " Maximum positive value = " << fixed << setprecision(2) << positiveMax << endl;

positiveMin = 0.0;
for (i=0; i <= n-1; i++)
{
if(x[i] < positiveMax)
if(x[i] > 0.0)
x[i] = positiveMin;
}
cout << " Minimum positive value = " << fixed << setprecision(2) << positiveMin << endl;

negativeMax = 0.0;
for (i=0; i <= n-1; i++)
{
if(x[i] < 0.0)
x[i] = negativeMax;
}
cout << " Maximum negative value = " << fixed << setprecision(2) << negativeMax << endl;

negativeMin = 0.0;
for (i=0; i <= n-1; i++)
{
if(x[i] > negativeMax)
if(x[i] < 0.0)
x[i] = negativeMin;
}
cout << " Minimum negative value = " << fixed << setprecision(2) << negativeMin << endl << endl;
}
1
2
3
4
5
6
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.
Okay, that makes sense. Would you have any idea on how to fix it? I'm assuming it's within the if statement.
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.

http://www.cplusplus.com/reference/climits/
Last edited on
Okay, I got it all except for the minimum negative part. Here is what I have but it keeps giving me the wrong output.

1
2
3
4
5
6
7
8
negativeMin = x[0];
   for (i=0; i <= n-1; i++)
   {
      if(x[i] < 0.0)
      if(x[i] > negativeMin)
	     negativeMin = x[i];
   }
   cout << "     Minimum negative value       = " << fixed << setprecision(2) << setw(8) << right << negativeMin << endl << endl;
Last edited on
I think I wasn't paying attention previously. I missed that the values were double, not int.

In your most recent code, line 5 if(x[i] > negativeMin) should be if(x[i] < negativeMin)

Sorry about any confusion, as i think I've said some contradictory things previously.
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.
These are the sort of results I'm getting:
 Maximum positive value = 496.32
 Minimum positive value = 6.70
 Maximum negative value = -84.35
 Minimum negative value = -493.90

from this data, (note it is sorted only for display purposes, the actual data was random):
 -493.904
 -456.206
 -368.476
 -361.189
 -351.106
 -248.434
 -241.257
 -224.692
 -207.738
 -152.292
  -84.346
    0.000
    6.696
  205.443
  221.372
  221.625
  260.443
  304.587
  411.025
  496.318

using this code:
1
2
3
4
5
6
7
8
    double negativeMin = 0.0;
    
    // NEGATIVE MIN
    for (i=0; i <= n-1; i++)
    {
        if (x[i] <0.0 && x[i] < negativeMin)
            negativeMin = x[i];
    }
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?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
void maxMin (double x[], int n)
{
   int i;
   
   double positiveMax;
   double positiveMin;
   double negativeMax;
   double negativeMin;
   double max;
   double min;
  
   positiveMax = x[0];
   for (i=0; i <= n-1; i++)
   {
      if(x[i] > positiveMax)
	     positiveMax = x[i];
   }  
   cout << "     Maximum positive value       = " << fixed << setprecision(2) << setw(8) << right << positiveMax << endl;
   
   positiveMin = x[0];
   for (i=0; i <= n-1; i++)
   {
      if(x[i] < positiveMin)
	  if(x[i] > 0.0)
	     positiveMin = x[i];
   }
   cout << "     Minimum positive value       = " << fixed << setprecision(2) << setw(8) << right << positiveMin << endl;
   
   negativeMax = x[0];
   for (i=0; i <= n-1; i++)
   {
      if(x[i] < negativeMax)
      if(x[i] < 0.0)
	     negativeMax = x[i];
   }
   cout << "     Maximum negative value       = " << fixed << setprecision(2) << setw(8) << right << negativeMax << endl;
   
   negativeMin = x[0];
   for (i=0; i <= n-1; i++)
   {
      if(x[i] < 0.0)
      if(x[i] < negativeMin)
	     negativeMin = x[i];
   }
   cout << "     Minimum negative value       = " << fixed << setprecision(2) << setw(8) << right << negativeMin << endl << endl;
   
   max = x[0];
   for (i=0; i <= n-1; i++)
   {
      if(x[i] > max)
	     max = x[i];
   }
   
   min = x[0];
   for (i=0; i <= n-1; i++)
   {
      if(x[i] < min)
	     min = x[i];
   }
   
   if(max < 0.0)
	   cout << "     No positive values" << endl;
   if(min >= 0.0)
	   cout << "     No negative values" << endl << endl;  
   
}
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.

Hope this helps.
This is the way I did it, using the constants specified in the <cfloat> header.
http://www.cplusplus.com/reference/cfloat/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <cfloat>

void maxMin (double x[], int n)
{
    double positiveMax = 0.0;
    double positiveMin = DBL_MAX;
    double negativeMax = -DBL_MAX;
    double negativeMin = 0.0;

    for (int i=0; i<n; i++)
    {
        // POSITIVE MAX
        if (x[i] > positiveMax)
            positiveMax = x[i];
   
        // POSITIVE MIN
        if (x[i] > 0.0 && x[i] < positiveMin)
            positiveMin = x[i];
 
        // NEGATIVE MAX
        if (x[i] < 0.0 && x[i] > negativeMax)
            negativeMax = x[i];
 
        // NEGATIVE MIN
        if (x[i] < negativeMin)
            negativeMin = x[i];
    }
    
    cout << " Maximum positive value = " << fixed << setprecision(2) << positiveMax << endl;
    cout << " Minimum positive value = " << fixed << setprecision(2) << positiveMin << endl;
    cout << " Maximum negative value = " << fixed << setprecision(2) << negativeMax << endl;
    cout << " Minimum negative value = " << fixed << setprecision(2) << negativeMin << endl;
}
Last edited on
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?
Last edited on
Alternate possible solution that takes lack of positives or negatives into account. Didn't compile it, so I'm not 100% sure about the output.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
void maxMin(double x[], int n)
{
    int positiveMax = -1;
    int positiveMin = -1;
    int negativeMax = -1;
    int negativeMin = -1;

    for(int i = 0; i < n; i++)
    {
        if(x[i] >= 0.0)
        {
            if(positiveMax < 0)
            {
                positiveMax = i;
                positiveMin = i;
            }
            else
            {
                if(x[i] > x[positiveMax])
                    positiveMax = i;
                else if(x[i] < x[positiveMin])
                    positiveMin = i;
            }
        }
        else
        {
                if(negativeMax < 0)
                {
                    negativeMax = i;
                    negativeMin = i;
                }
                else
                {
                    if(x[i] > x[negativeMax])
                        negativeMax = i;
                    else if(x[i] < x[negativeMin])
                        negativeMin = i;
                }
        }
    }

    cout << setiosflags(ios::fixed) << setprecision(2);

    if(positiveMax >= 0)
    {
        cout << "Maximum positive value = " << x[positiveMax] << endl;
        cout << "Minimum positive value = " << x[positiveMin] << endl;
    }
    else
        cout << "No positive values\n";

    if(negativeMax >= 0)
    {
        cout << "Maximum negative value = " << x[negativeMax] << endl;
        cout << "Minimum negative value = " << x[negativeMin] << endl;
    }
    else
        cout << "No negative values\n";
}
Last edited on
Uh... why can't OP do his own work?

[edit] He had done a very good job so far...
Last edited on
I still don't understand why this is giving me the wrong output. It's giving the first value in the array.

1
2
3
4
5
6
7
8
9
negativeMin = x[0];
   for (int i=0; i<n; i++)
   {
	   // NEGATIVE MIN
       if (x[i] < 0.0 && x[i] > negativeMin)
            negativeMin = x[i];
   }
   if(x[i] < 0.0)
   cout << "     Minimum negative value = " << fixed << setprecision(2) << setw(8) << right << negativeMin << endl << endl;
Look at line 5 very closely.
I still don't understand why it's giving a positive value though. Shouldn't the if (x[i] < 0.0) fix that?
Pages: 123