Ask coding questions

← Back to all posts
c++ for cicle

how i can write a program allows me to enter a sequence of float n numbers and print their sum and average. The sequence stops when 0 is entered.

Answered by TheDNAHero [earned 5 cycles]
View Answer

You need an array that can hold a lot of number and then a float variable. You want to ask the user for the number and then push it onto the beginning of the array. You can create a while loop to repeat the last step until the float is equal to 0. After that, you can create a for loop that cycles through all of the numbers and adds them to a variable until the variable is equal to undefined, and then print that. Then, you can take that and divide it by using a for loop to add one to a new variable for every item in the array until that equals undefined. divide both of the numbers and you have the average. I hope this was helpful. If you want a repl, just reply.