MATLAB Answers

can some xxplain what the following program does? Thank youuuu!: peso=5000; while (peso > 0) disp (‘I order another coffee’); dollar = dollar – 250; end disp (‘I finished drinking

1 view (last 30 days)
Auldrey Laurente
Auldrey Laurente on 29 Feb 2020
Answered: Les Beckham on 29 Feb 2020
can someone explain what the following program does? Thank youuuu!
peso=5000;
while (peso > 0)
disp (‘I order another coffee’);
dollar = dollar – 250;
end
disp (‘I finished drinking coffee’)
x = sym('x')
length(char(x))
for k = 1:10
x =sqrt (1+x)
length(char(x))
end
  2 Comments

Sign in to comment.

Answers (1)

Les Beckham
Les Beckham on 29 Feb 2020
Per Stephens comment, I'm repeating my comment as an answer (with some additions).
This script does pretty much nothing other than generate a lot of errors when you run it. I'm not sure if it was a result of how you pasted it into the forum or if it is in the original code, but the single quote characters are not the simple apostrophe characters expected by Matlab (they are the "fancy" ones that might be inserted by a word processor, where the open and close quotes are different).
After replacing those, we get down to the real errors in the actual code.
On the fourth line of code you decrement 'dollar' which is a variable which was not previously defined. This generates an error.
If I replace 'dollar' on this line with 'peso' we get a little bit further and get a result that starts to make sense. What it does (after the previously described changes) is repeatedly reduce the value of 'peso' by 250 until it is not greater than zero, printing ‘I order another coffee’ on each pass through the loop. When 'peso' becomes less than or equal to zero, the loop exits and ‘I finished drinking coffee’ is displayed.
Note that, to be more realistic (assuming coffee costs 250 pesos), you should probably change the while condition to 'while (peso >= 250)'. In your specific example it doesn't make a difference since your initial value of 5000 is evenly divisible by 250. I thought I would mention this anyway.
The second part of your code where you start using symbolics appears to be totally unrelated to the code before that. It pretty much makes no sense and I don't have the symbolic toolbox, so I won't even try to comment further on that part of the code.

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!