![remyboy1938](/avatars/24408.jpg)
remyboy1938
14.09.2019 •
Computers and Technology
Write a program that continues to read positive integer values until user enters a negative value. the program should print average of entered values, maximum value and minimum value
Solved
Show answers
More tips
- S Society and Politics Will Japan become Russia s Military Enemy?...
- P Philosophy Is Everything We Strive for Eventually Achieved and Destroyed?...
- S Society and Politics Understanding Politics and Its Role in the Development of Civilization...
- P Philosophy Why Did God Create Man and Place Him in Obscurity?...
- S Society and Politics Skoptsy: Who They Are and How They Perform Castration?...
- O Other Childhood Fears: What Many of Us Experienced...
- P Philosophy What is Something for you?...
- H Health and Medicine Why Do Humans Have One Heart?...
- P Philosophy Unbelievable stories of encounters with otherworldly forces...
- O Other How to Accidentally Get a Rare Coin with Your Change and How to Know Its Value?...
Ответ:
// here is code in C++.
#include <bits/stdc++.h>
using namespace std;
int main()
{
// variable
int n;
int mi=INT_MAX;
int mx=INT_MIN;
double avg;
int count=0;
double sum=0;
cout<<"enter a positive integer(Negative number to stop):";
// read the input first time
cin>>n;
// read the input until user enter a negative number
while(n>=0)
{
// calculate minimum
if(n<mi)
mi=n;
// calculate maximum
if(n>mx)
mx=n;
// count of entered numbers
count++;
// total sum of all number
sum=sum+n;
cout<<"enter a positive integer(Negative number to stop):";
// read the input again
cin>>n;
}
// calculate the average
avg=sum/count;
// print the average
cout<<"average of "<<count<<" number is:"<<avg<<endl;
// print the maximum
cout<<"maximum of all number is:"<<mx<<endl;
// print the minimum
cout<<"minimum of all number is:"<<mi<<endl;
return 0;
}
Explanation:
Declare and initialize "mi" with maximum integer value.Similarly "mx" with minimum integer value.Read user input until user enter a negative number.Add all the number to variable "sum" and keep count of positive number entered with "count".When user enter a negative number then it will stop taking input and calculate average of all number by dividing sum with count. Also it will check for each and find the maximum and minimum among the all input.
Output:
enter a positive integer(Negative number to stop):23
enter a positive integer(Negative number to stop):10
enter a positive integer(Negative number to stop):34
enter a positive integer(Negative number to stop):22
enter a positive integer(Negative number to stop):5
enter a positive integer(Negative number to stop):-2
average of 5 number is:18.8
maximum of all number is:34
minimum of all number is:5
Ответ:
70000
Step-by-step explanation: