•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 391,988 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 4,237 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser:
Views: 155 | Replies: 3
![]() |
•
•
Join Date: Nov 2007
Posts: 46
Reputation:
Rep Power: 1
Solved Threads: 2
how do you find the minimum and maximum number from a given set of numbers without sorting them
?
i have the code but the it isnt working properly
the highest number is working fine the lowest number im getting errors on it
it shows a weird number please help
?
i have the code but the it isnt working properly
#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();
}the highest number is working fine the lowest number im getting errors on it
it shows a weird number please help
•
•
Join Date: Jul 2007
Location: Mumbai(Bombay), India
Posts: 140
Reputation:
Rep Power: 2
Solved Threads: 21
C++ Syntax (Toggle Plain Text)
// Line added here lowest=sales[0]; for ( int x = 0; x < SZ; x++ ) { if( sales[x] < lowest ) { lowest = sales[x]; } }
Your "lowest" variable needs to be initialized to the first element in the array, else you will always have a problem when your lowest value is greater that 0.
Same also applies to how you find the highest value in the array, if the largest value is less than 0, you will always get 0.
Also you can combine the two loops in to one and just do both the checks in the same loop
Last edited by stephen84s : Jun 28th, 2008 at 9:43 am.
"One had to cram all this stuff into one's mind for the examinations, whether one liked it or not. This coercion had such a deterring effect on me that, after I had passed the final examination, I found the consideration of any scientific problems distasteful to me for an entire year." -Einstein
•
•
Join Date: Apr 2008
Posts: 28
Reputation:
Rep Power: 0
Solved Threads: 2
Make two variables, one for min and one for max. Assign them to the first two numbers in your sequence. Compare each new element that you encounter with the min and the max. If it is larger than the max or smaller than the min, assign it. When you have looped through the list, you have the min and max.
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
•
•
•
•
DaniWeb C++ Marketplace
Similar Threads
- LC-3 Maximum & Minimum (Assembly)
- Procedure for MAX/MIN (Pascal and Delphi)
- min/max of a mixed type list (Python)
- Need some help with min/max in arrays (C++)
- min and max values (C++)
- array max min (C++)
- beginners program; can't get min value (C++)
Other Threads in the C++ Forum
- Previous Thread: USB Library
- Next Thread: Can't debug/compile visual c++


Linear Mode