Help with Matlab Problem: Commenting Code & Finding Errors

AI Thread Summary
The discussion focuses on a Matlab code that the user needs help understanding and commenting on, as well as identifying errors. Participants emphasize the importance of improving code readability through proper indentation and commenting on each line's function. There is a suggestion to analyze the code step-by-step to grasp its purpose, as this is a common practice among programmers. The code is critiqued for potentially being nonsensical, and participants encourage the user to express their expectations of the code's functionality in the comments. Overall, the thread highlights the collaborative nature of coding assistance and the learning process involved in debugging and understanding code.
Aydashka
Messages
4
Reaction score
0
New poster has been reminded to show their work on schoolwork problems
Hi! I really need help with the following Matlab problem.
What does this code do? I need to comment what the important lines of this code do. Also the code contains several errors which i need to find. Here is the program:
Matlab:
clear all; close all; clc;
Maxi=50; tab=[1 :Maxi] ; tab(1)=0;

while 0==0
  i=1;
  while tab(i)==0
     i=i+1;
  end
  p=tab(i) ; disp( [num2str(p)] ) ;
  for k=p:p:Maxi
     tab(k)=0;
  end
end
Thank you so much in advance!
 
Last edited by a moderator:
Physics news on Phys.org
When is 0==0 ever not true?
 
Can you clarify your question please?
 
While 0==0. Means do the action for as long as zero equals zero
 
Right! Could you explain what this code does?
 
Aydashka said:
Right! Could you explain what this code does?
It's your schoolwork question -- what do you think it does and why?
 
If i knew what it did, i would'n ask about it here :biggrin:
 
If I see the literal expression 1+1 in my code what could I replace it with?

I would replace it with 2 as its equivalent and there’s little need to complicate my code with 1+1 even though I know the compiler, kindhearted though it is, would replace it with 2 for me,

Similarly, If I see 0==0 in my code what can I replace it with?

How can you define an infinite loop?
 
Aydashka said:
If i knew what it did, i would'n ask about it here :biggrin:

Poor answer.

I adjusted your code with some spacing to make it easier to read.

You have MATLAB and so you need to step through your code and discover what it does. This is what programmers do all the time with new unknown code.
 
  • Like
Likes FactChecker
  • #10
Since this is a probably homework problem, we are only allowed to give hints and advice to lead you to the solution. The first thing I would advise you to do is to improve the indentation so that the loops are easy to see where they begin and end. Then I would advise you to write some comments in the code about what each line and loop would do. After you post that, we can review your comments and give you some more advice.

PS. If there is something that you expected the code to do, you should describe that in comments at the appropriate places in the code. As far as I can see, the current code is nonsense.
 
Back
Top