Help with Matlab Problem: Commenting Code & Finding Errors

Click For Summary

Discussion Overview

The discussion revolves around a Matlab code snippet provided by a user seeking help with understanding its functionality and identifying errors. The scope includes code commenting, error detection, and general clarification of programming concepts related to loops and code structure.

Discussion Character

  • Homework-related
  • Technical explanation
  • Conceptual clarification
  • Debate/contested

Main Points Raised

  • One participant questions the logic of the condition "0==0" and its implications for the loop's execution.
  • Another participant asks for clarification on the original poster's question, indicating a need for more context.
  • There is a suggestion that the original poster should analyze the code themselves to understand its function, as this is a common practice among programmers.
  • A participant emphasizes the importance of improving code readability through proper indentation and commenting, suggesting that this will aid in understanding.
  • One participant expresses skepticism about the code's purpose, describing it as "nonsense" and encouraging the original poster to articulate their expectations of the code's behavior.
  • Another participant discusses the concept of replacing expressions in code, using "1+1" and "0==0" as examples, and raises the question of defining an infinite loop.

Areas of Agreement / Disagreement

Participants generally agree on the need for the original poster to engage with the code to understand it better. However, there is no consensus on the specific functionality of the code or its correctness, and multiple interpretations of its purpose are present.

Contextual Notes

Limitations include the lack of clarity on the expected behavior of the code and the potential for misunderstanding due to the original poster's limited context. The discussion also reflects varying levels of familiarity with Matlab programming among participants.

Who May Find This Useful

Individuals interested in Matlab programming, particularly those seeking assistance with code analysis, commenting, and debugging, may find this discussion beneficial.

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   Reactions: 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.
 

Similar threads

Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 23 ·
Replies
23
Views
9K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
2
Views
2K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K