User Name Password Register
DaniWeb IT Discussion Community
All
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
Reply
Join Date: Nov 2007
Posts: 46
Reputation: manzoor is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 2
manzoor manzoor is offline Offline
Light Poster

min and maximum

  #1  
Jun 28th, 2008
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

#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
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jul 2007
Location: Mumbai(Bombay), India
Posts: 140
Reputation: stephen84s is on a distinguished road 
Rep Power: 2
Solved Threads: 21
stephen84s's Avatar
stephen84s stephen84s is offline Offline
Junior Poster

Re: min and maximum

  #2  
Jun 28th, 2008
  1.  
  2. // Line added here
  3. lowest=sales[0];
  4. for ( int x = 0; x < SZ; x++ ) {
  5. if( sales[x] < lowest ) {
  6. lowest = sales[x];
  7. }
  8. }

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
Reply With Quote  
Join Date: Apr 2008
Posts: 28
Reputation: dmanw100 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 2
dmanw100 dmanw100 is offline Offline
Light Poster

Re: min and maximum

  #3  
Jun 28th, 2008
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.
Reply With Quote  
Join Date: Nov 2007
Posts: 46
Reputation: manzoor is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 2
manzoor manzoor is offline Offline
Light Poster

Re: min and maximum

  #4  
Jun 28th, 2008
thanks that solved the problem
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb C++ Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 9:44 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC