# Thread: min and maximum number

1. ## min and maximum number

hey how do you find the minimum and maximum of entered numbers

suppose some one enters 5,4,9,2,3,1,7,8

and then find out the maximum and minimum from them

i have the program coded but I'm getting errors in the maximum number

Code:
```#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
const int SZ = 7;
double sales[SZ];
int x;
double lowest = sales[0];
double highest = sales[0];
for(x = 0; x < SZ; x++)
{
cout<<"Enter sales for day "<<(x + 1)<<" >> ";
cin>>sales[x];

}
cout<<"The entered sales are: ";
for(x = 0; x< SZ; x++) {
cout<<sales[x]<<"  ";
}

for ( int x = 0; x < SZ; x++ ) {
if ( highest < sales[x] ) {
highest = sales[x];
}

}
for ( int x = 0; x < SZ; x++ ) {

if( sales[x] < lowest ) {
lowest = sales[x];
}

}
cout<<"\nThe lowest sale is "<<lowest<<endl;
cout<<"\nThe highest sale is "<<highest<<endl;

getch();
}```
the minimum number is fine but a wierd number appears in the highest number

2. You're using sales[0] before sales[0] has a value.

3. ok now I changed both but now there is a problem with the lowest number argh ...

Code:
```#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
const int SZ = 7;
double sales[SZ];
int x;
double lowest = 0;
double highest = 0;
for(x = 0; x < SZ; x++)
{
cout<<"Enter sales for day "<<(x + 1)<<" >> ";
cin>>sales[x];

}
cout<<"The entered sales are: ";
for(x = 0; x< SZ; x++) {
cout<<sales[x]<<"  ";
}

for ( int x = 0; x < SZ; x++ ) {
if ( sales[x] > highest ) {
highest = sales[x];
}

}
for ( int x = 0; x < SZ; x++ ) {

if( sales[x] < lowest ) {
lowest = sales[x];
}

}
cout<<"\nThe lowest sale is "<<lowest<<endl;
cout<<"\nThe highest sale is "<<highest<<endl;

getch();
}```
If I use sales[0] in place of 0 in the double lowest variable the answer is fine then but if i use 0 then its wrong

please help I want to know the reason and how to do it If I assign the variable as 0

4. lowest needs to be bigger than any value in the array initially, since you are searching for values that are smaller than that.

However, a smart thing might be to set both highest and lowest to the first item in the array initially and start comparing from the second.

5. you meant this ??

Code:
```#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
const int SZ = 7;
double sales[SZ];
int x;
double lowest = sales[0];
double highest = sales[0];
for(x = 0; x < SZ; x++)
{
cout<<"Enter sales for day "<<(x + 1)<<" >> ";
cin>>sales[x];

}
cout<<"The entered sales are: ";
for(x = 0; x< SZ; x++) {
cout<<sales[x]<<"  ";
}

for ( int x = 0; x < SZ; x++ ) {
if ( sales[x+1] > highest ) {
highest = sales[x];
}

}
for ( int x = 0; x < SZ; x++ ) {

if( sales[x+1] < lowest ) {
lowest = sales[x];
}

}
cout<<"\nThe lowest sale is "<<lowest<<endl;
cout<<"\nThe highest sale is "<<highest<<endl;

getch();
}```
however the largest number isn't still working

6. Sales is uninitialized. It contains junk data because you have not read anything into it.
Is this a difficult aspect to grasp? When a variable is created, it contains junk data. Do not use it until you initialize it with a sane value.

7. Here:
Code:
```#include<iostream>
#include<conio.h>

using namespace std;

int main()
{
const int SZ = 7;
double sales[SZ];

//Don't define x here

double highest = 0; //Initialize with 0, we will be assigning it a value later

for(int x = 0; x < SZ; x++) //Define x in each of the for statements
{
cout << "Enter sales for day " << (x + 1) << " >> ";
cin >> sales[x];
}

cout<<"The entered sales are: ";

for(int x = 0; x< SZ; x++)
{
cout<< sales[x] << "  ";
}

for ( int x = 0; x < SZ; x++ )
{
if ( sales[x] > highest ) //Don't put sales[x] + 1, you want to scroll through ALL the values
{
highest = sales[x];
}
}

double lowest = highest; //Lowest goes here because we need the highest value to initially go in it

for ( int x = 0; x < SZ; x++ )
{
if( sales[x] < lowest ) //Again, no + 1
{
lowest = sales[x];
}
}

cout<<"\nThe lowest sale is "<<lowest<<endl;
cout<<"\nThe highest sale is "<<highest<<endl;

getch();
}```
I fixed it up, I commented the errors so you should understand. I tested it. And it was only a couple of careless things you did wrong. Hopefully you learn from it.