Problem 44455. Find the average of a random sequance
Write a function that generates random integers within a loop, and calculates the mean of the positive numbers only.
At each iteration a random integer between 0 and 10 is generated.
Only if the number is positive, it is added to the sequence.
The loop terminates when the number 0 is generated. This number is not considered for the mean.
You are not allowed to use the functions sum() and mean()
Examples:
Random sequence is: 4, 3, 5, 10, 3, 0 Output is: 5
Random sequence is: 0 Output is 0
Random sequence is: 5, 8, 1, 9, 3, 4, 6, 2, 2, 3, 1, 0 Output is: 4
Note: the function does not have an input. The output depends on the state of the random numbers generator.
Solution Stats
Problem Comments
-
2 Comments
Hello, Shaul Salomon.
. . .
Could you please double-check the consistency of your test 7 with the problem statement? The problem statement specifies that "[...] when the number 0 is generated. This number is not considered for the mean." . . .
In test 7, I get only one random number value, which is zero, and this should be discarded. Therefore the set of numbers to average is an empty set: this empty set has a sum of zero (or undefined?) and — most importantly — a count of zero. So therefore the mean there should be undefined, or NaN, as far as I can understand. This is how I implemented Solution 1391527. Please let me know if I am overlooking some important point. . . . —Thanks, David.
Interesting problem.
Solution Comments
Show commentsProblem Recent Solvers411
Suggested Problems
-
2452 Solvers
-
Project Euler: Problem 7, Nth prime
1427 Solvers
-
1408 Solvers
-
478 Solvers
-
Sum the numbers on the main diagonal
581 Solvers
More from this Author25
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!