how to find greatest input

This is a discussion on how to find greatest input within the C++ Programming forums, part of the General Programming Boards category; quick question...in the loop below, what kind of code is neccessary to find the largest input entered by the user? ...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    33

    how to find greatest input

    quick question...in the loop below, what kind of code is neccessary to find the largest input entered by the user?

    from my understanding, every iteration the variable changes and that is why I'm having a dilema?

    Code:
    //PURPOSE: This program reads an arbitrary number of real numbers.
    //The input sequence is ended by a sentinel value.
    //The program then reports the quantity, sum, and average
    //of all numbers entered; how many of them are positive negative or 
    //zero;how many seven and elevens were entered, whether positive or 
    //negative; and the largest and smallest numbers entered.
    //*********************************************************************
    
    #include<iostream.h> // for I/O tools
    
    int main()
    {   
        //declaring variables
        double num; //number the user will enter
        
        //declaring counter variables to count number of times
        //certain events occur.
        int  count; 
        int  countpositives; 
        int  countnegatives;
        int  countzeros;  
        int  countpositivesevens;
        int  countnegativesevens;
        int  countpositiveelevens;
        int  countnegativeelevens;
        
        //variables that will be assigned the total numbers
        int  totalsevens;
        int  totalelevens;
        
        //varibles for largest and smallest number entered by user
        float largestnumber;
        float smallestnumber;
        
        float sum;      //Computes the sum of user inputs
        float average;  //Computes the average of user inputs
        
        //Setting these counts to zero because they all equal zero at 
        //this point in the program and so that the number of times the 
        //loop body is true corresponds to the number of the particular 
        //item the program keeps track of.
        count=0;
        countpositives=0;
        countnegatives=0;
        countzeros=0;
        countpositivesevens=0;
        countnegativesevens=0;
        countpositiveelevens=0;
        countnegativeelevens=0;
         
        //So that my following sum assign.
        //statements keep track of sum. 
        sum=0;
       
        
        
        //Describing the program to user and also prompting the user.
        //Also telling the user to input sentinel value when done.
        cout<<"This program reports things about an arbitrary ";
        cout<<"number of real numbers that\nyou will be asked " ;
        cout<<"to enter."<<endl<<endl;
        cout<<"Enter any number or real numbers.";
        cout<<"  Following the last value you input,"<<endl;
        cout<<"input 45.8931. Thank you!"<<endl<<endl;
        
        cin>>num;
        //If statements within a loop. If the input doesn't equal
        //the sentinel value,45.8931, the sum will be computed and
        // count variable value will increment 1.
        //Next, the If statements test to see if an event is true, and 
        //if so it increases the count of the variable name for that event
        //Finally, we get the next user input just before the ned of the 
        //loop.
        while(num!=45.8931)
            {
            
            
            sum=sum+num;
            count++;
            if(num==0)
            countzeros++;
            if(num>0)
            countpositives++;
            if(num<0)
            countnegatives++;
            if(num==7)
            countpositivesevens++;
            if(num==-7)
            countnegativesevens++;
            if(num==11)
            countpositiveelevens++;
            if(num==-11)
            countnegativeelevens++;
            
            cin>>num;
            
            }
            
            average=sum/count; //computing the average
            
            //Computing total sevens and elevens
            totalsevens=countpositivesevens+countnegativesevens;
            totalelevens=countpositiveelevens+countnegativeelevens;
            
            //Formatting the output; reporting to the user the
            // things about the number the user entered.
            cout<<endl;
            cout<<"The number of real numbers you entered is ";
            cout<<count<<"."<<endl;
            cout<<"The sum of them is "<<sum<<"."<<endl;
            cout<<"The average "<<average<<"."<<endl<<endl;
            cout<<"You entered in "<<countpositives<<" positive ";
            cout<<"numbers, "<<countnegatives<<" negative numbers, ";
            cout<<"and "<<countzeros<<" zeros."<<endl<<endl;
            cout<<"You entered in "<<countpositivesevens;
            cout<<" positive sevens and "<<countnegativesevens;
            cout<<" negative sevens, making a total of\n";;
            cout<<totalsevens<<" sevens,whether positive or ";
            cout<<"negative.";
            cout<<"  You entered in "<<countpositiveelevens;
            cout<<" positive elevens and "<<countnegativeelevens;
            cout<<" negative elevens, making a total of ";
            cout<<totalelevens<<" elevens, whether positive ";
            cout<<"or negative.";
            
            
     return 0;          
    }
    [edit]Code tags added by Hammer.
    Last edited by m712; 10-14-2002 at 05:21 AM.

  2. #2
    Lead Moderator kermi3's Avatar
    Join Date
    Aug 1998
    Posts
    2,595
    I am posting this because you did not use code tags on this thread. In the furture please use Code Tags. They make your code MUCH easier to read and people will be much more likely to help you if you do. And they'll be happy about helping you


    For example:

    Without code tags:

    for(int i=0;i<5;i++)
    {
    cout << "No code tags are bad";
    }

    With Code Tags:
    Code:
    for(int i=0;i<5;i++)
    {
         cout << "This code is easy to read";
    }
    This is of course a basic example...more complicated code is even easier to read with code tags than without.

    Hammer added code tags for you this time. They can be added by putting [code] at the beginning of your code and [/code] at the end. More information on code tags may be found at the link in my signature. Any further questions or ways I can help please feel free to PM me.

    Good Luck,
    Kermi3
    Kermi3

    If you're new to the boards, welcome and reading this will help you get started.
    Information on code tags may be found here

    - Sandlot is the highest form of sport.

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231

    Re: how to find greatest input

    Originally posted by m712
    quick question...in the loop below, what kind of code is neccessary to find the largest input entered by the user?

    from my understanding, every iteration the variable changes and that is why I'm having a dilema?

    First - check your PMs (Private Messages)

    Now to the code.... to determine the highest value, simply have a variable called something like "Highest", initialise it to 0, when the user enters a number, compare it to Highest, and if it's greater, update Highest to have the new value. At the end, simply print the value of Highest...... job done.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    362
    Actually, all you have to do is introduce another variable like 'double bigNum = 0;' and compare it against 'num'.

    For example:
    Code:
    #include <iostream>
    #include <cstdlib>
    
    inline void pause() { system("PAUSE"); }
    
    int main(void)
    {
    double num = 0, bigNum = 0;
    
    while ( num != 45.8931 ){
        std::cout << "Enter a number: -> ";
        std::cin >> num;
    
            if ( num > bigNum)
                bigNum = num;
    }
    
    std::cout << bigNum << std::endl;
    
    pause();
    return 0;
    }
    -Skipper

    P.S. Thanks, Hammer. My timing, as usual, is impeccable!
    Last edited by skipper; 10-14-2002 at 06:03 AM.
    "When the only tool you own is a hammer, every problem begins to resemble a nail." Abraham Maslow

  5. #5
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    Posts
    953
    Hi,
    Do you mean something like this:
    Code:
    int number, greatest=1;     //you have to initialize greatest according to what you need in your program
    for ( int=0; i<5; i++)
    {
         cin >> number;
         if ( number > greatest )
              greatest = number;
    }
    I hope this helps.
    you can first read a value of number then make greatest = number then enter the loop.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Input class project (again)
    By Elysia in forum C++ Programming
    Replies: 41
    Last Post: 02-13-2009, 09:52 AM
  2. fscanf in different functions for the same file
    By bchan90 in forum C Programming
    Replies: 5
    Last Post: 12-03-2008, 08:31 PM
  3. Printing Length of Input and the Limited Input
    By dnguyen1022 in forum C Programming
    Replies: 33
    Last Post: 11-29-2008, 03:13 PM
  4. File input question
    By Beast() in forum C Programming
    Replies: 16
    Last Post: 07-09-2004, 03:23 PM
  5. Won't Return pointer, i can't find why...
    By ss3x in forum C++ Programming
    Replies: 2
    Last Post: 02-28-2002, 07:50 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21