0
Average of 5 inputted numbers
can you explain it?
6 Answers
+ 1
Yeh, Ipang, you are right, fixed
+ 1
Explain? Thats quite basic.
Firstly we include iostream library, so we can operate with console input and output streams.
Then with using directive we add an std namespace, that's not compulsory, but saves a bit time by preventing writing std::cin and std::cout instead of cin and cout.
Then we declare a main function, which will run when we we will call a compiled executive file.
In this function we declare two variables: sum where we will store the total of all input numbers and temp to which we will read the input from the console. Variables are of type float, so when we will calculate average it will not be rounded to integer.
Then we we set a for cycle that iterates 5 times. On each iteration it takes an input from a console into temp variable and adds it to sum variable. That's a very simple code without error handling, so if you input something, which is not a number, it will throw an exception and crash.
+ 1
After five numbers are read from the console and summed, the program will divide the sum by 5 and show a result in console. 5 is not just 5, but 5.0 because it is also is a floating point. Different compilers cast results of different number types differently, so if we want to ensure the result is float, its better all operands be float too.
+ 1
(__Start__)
||
____ v_________________
|_declare sum and avg_|
||
v
___ /\ ___
|__If i < 5__| -yes-+
|| no ^ |
|| | _____ v____
|| | | add input |
|| | |_to sum___|
|| |_______|
v
avg=sum/5
||
v
print avg
||
v
(__End__)
0
#include <iostream>
using namespace std;
int main()
{
float sum = 0;
float temp;
for (int i = 0; i<5; ++i)
{
cin>>temp;
sum+=temp;
}
cout<<sum/5.0;
}
0
How to make that as a flowchart?