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.
Re: how to find greatest input
Quote:
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.