max and min
Oct 14, 2016 at 9:10pm UTC
im writing a program that will give me the sum of any given inputs the average as well as the highest number and the smallest, this is what i have but I'm having trouble displaying the highest and smallest , i would appreciate any help thanks :)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
#include <iostream>
#include <cmath>
using namespace std;
int main() {
float numb;
float numbsum = 0;
float numbofentries = 0;
cout << "This program analyzes numerical input.\n\n" ;
cout << "Enter a number (0 to exit): " ;
cin >> numb;
while (numb != 0) {
numbsum = numbsum + numb;
numbofentries++;
cout << "Enter a number (0 to exit): " ;
cin >> numb;}
double high = numb;
double low = numb;
if (numb > high) {
high = numb;
} else {
if (numb < low) {
low = numb;
}
cout << "\n\n" ;
cout << "Summation: " << numbsum << endl;
cout << "Average value: " << numbsum / numbofentries << endl;
cout << "Smallest value: " << low << endl;
cout << "Largest Value: " << high << endl;
return 0;
}
}
Oct 14, 2016 at 9:43pm UTC
if you declare variables inside the loop, they have a limited scope and therefore are destroyed at each iteration, otherwise if the state after the loop, just take the last value entered in numb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
double high = 1 , low = 0 , numb = 1 , numb_sum = 0 ;
int numb_of_entries = 0 ;
while ( numb != 0 )
{
cin >> numb ;
numb_sum += numb ;
if ( high < numb )
{
high = numb ;
}
else if ( low > numb )
{
low = numb ;
}
++numb_of_entries;
}
cout << low << " -- " << high << " -- " << numb_sum / numb_of_entries <<'\n' ;
Last edited on Oct 14, 2016 at 9:57pm UTC
Oct 15, 2016 at 4:34pm UTC
its any given inputs until the user puts "0"
Oct 15, 2016 at 4:56pm UTC
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
#include <iostream>
#include <limits>
using namespace std;
int main()
{
cout << "This program analyzes numerical input.\n\n" ;
cout << "Enter a number (0 to exit): " ;
double numb;
cin >> numb;
double numbsum = 0;
double numbofentries = 0;
double high = numeric_limits<double >::min();
double low = numeric_limits<double >::max();
while (numb != 0)
{
if (numb > high)
high = numb;
if (numb < low)
low = numb;
numbsum += numb;
numbofentries++;
cout << "Enter a number (0 to exit): " ;
cin >> numb;
}
if (numbofentries)
{
cout << "\n\n" ;
cout << "Summation: " << numbsum << endl;
cout << "Average value: " << numbsum / numbofentries << endl;
cout << "Smallest value: " << low << endl;
cout << "Largest Value: " << high << endl;
}
}
Last edited on Oct 15, 2016 at 4:56pm UTC
Topic archived. No new replies allowed.