Wrote this max min problem. The output however gives me the correct max but not the correct min. It simply returns 0 for the min everytime. Any ideas what I need to fix?

#include "stdafx.h"
#include <iostream.>
#include <conio.h>
using namespace std;



int main()
{
const int NUM_DAYS = 6;
double sales[NUM_DAYS];
int a;
double min = 0;
double max = 0;

for(a = 0; a <= NUM_DAYS; ++a)
{
	cout<<"DEBUG: a is currently = " <<a;
	cout<<"Enter sales value "<<a + 1<<" "<<endl;
	cin>>sales[a];
}

for (a = 0; a <= 6; ++a)
{
    if (max < sales[a]) max = sales[a];
    else if (min > sales[a]) min = sales[a];
}
cout<<"The highest daily sales figure is: "<<max<<endl;

cout<<"The lowest daily sales figure is: "<<min<<endl;

getche();
}

First of all a few recommendations:

- Use '#include <iostream>' instead of '#include <iostream.h>' unless your compiler doesn't support it

- Your main function should return a value: for example put 'return 0;' at the end of the main function

Ok, now we're taking a look at what exactly you did wrong...

-> You declared an array with 6 elements: that means you've index 0 until 5 and index 6 doesn't exist, that's just an override (you're overwriting memory)

-> In the first for-loop the condition you mentioned was: 'a <= NUM_DAYS', you should change it to 'a <= NUM_DAYS-1', you should do the same in the second for-loop
(You hard-coded the value 6?)

-> If we interpret the the for-loop you wrote in your code:

for (a = 0; a <= 6; ++a)
{
    if (max < sales[a]) max = sales[a];
    else if (min > sales[a]) min = sales[a];
}

The max value works, but the min value not, why?
Well, in the beginning of your program you did the following:

double min = 0;
double max = 0;

To let the code work, you should set the max value as low as possible (zero) and the min value as high as possible (Maybe you say: how do I know the value min has to get assigned?)

Simply look at the following code:

for (a = 0; a <= NUM_DAYS-1; a++)
 {
     if (max < sales[a])
       {
              max = sales[a];
              min = max; // I always set the min value to the max value found
       } else if (min > sales[a]) {
              min = sales[a];
       }
    
 }

Your code isn't working well here, because 0 is never becoming higher than a positive number...
Thus the result is always zero, the variable min never changes in its value...

The code below is the final code (which is working):

#include <iostream>
#include <conio.h>

using namespace std;

int main()
{
 const int NUM_DAYS = 6;
 double sales[NUM_DAYS];
 int a;
 double min = 0;
 double max = 0;

 for(a = 0; a <= NUM_DAYS-1; a++)
 {
	cout << "DEBUG: a is currently = " << a << " ";
	cout << "Enter sales value " << a + 1 << " " << endl;
	cin >> sales[a];
 }

 for (a = 0; a <= NUM_DAYS-1; a++)
 {
     if (max < sales[a])
       {
              max = sales[a];
              min = max;
       } else if (min > sales[a]) {
              min = sales[a];
       }
    
 }

 cout << "The highest daily sales figure is: " << max <<endl;

 cout << "The lowest daily sales figure is: " << min <<endl;

 getche();
 
 return 0;
}

I hope I explained everything correct and clear (cause I'm not very good at explaining things clearly) and well, sorry if my English is poor, please remember that my native language is Dutch...

P.S.: Why are you using '<conio.h>' ? I know for the function 'getche();', but you could also use: 'cin.get();' instead...

Thanks for the help I really appreciate it. I didnt intentionally use <iostream.h>. I would never do that on purpose. That was exactly what I needed to get this done. Thank you.

An additional note on what to set min/max to at the start - set them both the first data value in the set.

That way, you don't have to be concerned with what range the data may be, and you will always have a correct answer as you compare min/max to the rest of the values.

Consider the simplest problem - a set with one value. Isn't that value both the min and max?

This question has already been answered. Start a new discussion instead.