50.4k views
4 votes
Write a fragment of code that reads in integers from standard input, until end-of-file and sends their (floating point ) average to standard output . If no numbers are input, the message "no values to average" is printed instead (while loops , end-of-file processing, mixed-mode arithmetic, basic conditional )

User Xiao Hanyu
by
7.9k points

1 Answer

4 votes

Answer:

Following are the fragment of code which is written in C++ Programming Language:

float number1=-1; // variable declaration float type

float count1=0; // // variable declaration float type

float sum1=0; // // variable declaration

while(cin.eof( )==false) // iterating the loop

{

cin >> number1; // read the number by user

sum1 += number1; // performing sum operation

count1++; // increment of count by 1

}

if(number1==-1) // checking condition

{

cout << "no values to average";

}

else

{

if (count1 > 0 )

cout << sum1/count1;

}

Step-by-step explanation:

Here, set three float data type variable "number" to 1, "count" to 0 and "sum" to 0. Then, we set the while loop, pass the condition "cin.eof( )==false". Then, we set the if statement, pass the condition "number==-1" and then, we set the else statement, inside it pass if statement.

User IntStarFoo
by
9.2k points
Welcome to QAmmunity.org, where you can ask questions and receive answers from other members of our community.