Thread: min and maximum number

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    164

    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. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    You're using sales[0] before sales[0] has a value.

  3. #3
    Registered User
    Join Date
    Nov 2007
    Posts
    164
    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
    Last edited by manzoor; 06-28-2008 at 06:50 AM.

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    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.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    Registered User
    Join Date
    Nov 2007
    Posts
    164
    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. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Hmm...? gin's Avatar
    Join Date
    Jun 2008
    Location
    Glasgow, Scotland
    Posts
    51
    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.

  8. #8
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Max Min Problem
    By smithc2005 in forum C Programming
    Replies: 7
    Last Post: 10-22-2008, 10:38 AM
  2. Max Min
    By jhwebster1 in forum C Programming
    Replies: 6
    Last Post: 02-16-2006, 02:17 PM
  3. Please help to check.
    By nicoleha in forum C Programming
    Replies: 16
    Last Post: 12-07-2005, 03:29 PM
  4. Counting number from a random file
    By kamisama in forum C Programming
    Replies: 42
    Last Post: 02-22-2005, 05:16 PM
  5. binary trees-finding the maximum and min. value
    By sachitha in forum C++ Programming
    Replies: 6
    Last Post: 09-23-2004, 01:09 AM