4.17 LAB: Varied amount of input data ( C++)
Statistics are often calculated with varying amounts of input data. Write a program that takes any number of non-negative integers as input, and outputs the average and max. A negative integer ends the input and is not included in the statistics.
Ex: When the input is:
15 20 0 5 -1
the output is:
10 20
(Please use C++)

Respuesta :

Answer:

Written in C++

#include<iostream>

using namespace std;

int main()

{

int num;

int sum = 0;

cout<<"Input: ";

cin>>num;

int count = 0;

int max = 0;

while(num >= 0)

{

 sum+=num;

 cin>>num;

 count++;

 if(num>=max){

  max = num;

 }

}

cout<<(sum/count)<<endl;

cout<<max<<endl;

return 0;

}

Explanation:

I've added the full source code as an attachment where I use comments as explanation.

See Attachment

Ver imagen MrRoyal

The max and the average of the input data will be determined through iterations and conditional statements.

The program in C++ is as follows

#include<iostream>

using namespace std;

int main(){

//This declares num

int num;

//This initializes sum to 0

int sum = 0;

//This prompts user for input

cout<<"Input: ";

//This gets input from the user

cin>>num;

//This initializes counter to 0

int count = 0;

//This initializes max to 0

int max = 0;

//The following iteration gets user input until user enters negative

while(num >= 0)  {

 //This line calculates the sum

 sum+=num;

 //This gets another input from the user

 cin>>num;

 //This increases counter by 1

 count++;

 //The following if statement checks for maximum

 if(num>=max){

  max = num;

 }

}

//This prints the average

cout<<"Average: "<<(sum/count)<<endl;

//This prints the max

cout<<"Maximum: "<<max<<endl;

return 0;

}

See comments for program explanation

Read more about C++ programs at:

https://brainly.com/question/15410214