Question
MATLAB Programming worksheet 4 Q1. while loops a) Write a script that asks for a series of numbers and calculates the sum and the mean.
MATLAB Programming worksheet 4
Q1. while loops
a) Write a script that asks for a series of numbers and calculates the sum and the mean. When the user enters a value of 0, the script stops and performs the calculation. For example:
Enter a number (type 0 to end) : 5
Enter a number (type 0 to end) : 6
Enter a number (type 0 to end) : 7
Enter a number (type 0 to end) : 8
Enter a number (type 0 to end) : 0
4 numbers entered. Sum=26. Mean=6.5.
b) Write a script which allows the user to play the "what number am I thinking of?" game. Your script should do the following :
- generate a random integer between 0 and 100 (use the rand(n) function)
- keeps asking the user for another guess if they are incorrect
- allows the user to quit by entering a number outside the range 0 to 100
- tells the user if their guess is too high or too low
- tells the user if they are getting close (within 5 on either side)
- keeps count of how many guesses have been made
- tells the user when they have guessed correctly and how many guesses they made
(see example output screen below)
Example Output Screen :
WELCOME TO GUESS-A-NUMBER
I am thinking of a number between 0 and 100 Please enter a guess (any number outside this range to quit
Make a guess : 50
Too High, another try?
Make a guess : 12
You are getting close but ... Too Low, have another go.
Make a guess : 16
You guessed my number and it only took you 3 tries!
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started