MATLAB programming assignment (for loop)

In summary, the conversation discusses a coding exercise involving calculating the checksum for a 9-digit ISBN number. The code provided in the conversation is missing a running total for the checksum, and the use of string concatenation may be necessary to create the desired output. The suggested solution involves defining check as 0 before entering the loop and using the code check = check + number to calculate the running total.
  • #1
GE2014
11
0

Homework Statement


(see attached file)


Homework Equations


(see attached file)


The Attempt at a Solution


isbn=input('Enter 9 Letter ISBN number: ','s');
for i=1:1:length(isbn)

(%indent)num=str2num(cat);
(%indent)check=num*i

%somewhere in here I need code to add all the checks found in the loop



end
checksum=mod(check%not correct,11)




So I have gotten the loop to visit all the digits separately and multiply it by the loop variable. Now all I need is some way to add all of those values into one. From there I use the modulus function and that's basically it. My prof gave a few hints. She said an if statement would be used, but that's probably for when the mod of the value will equal 10 and I need to display an X.

Also, I'm assuming I need to use a string concatenation at some point since it reminds me about it. Will that be used to put together the output at the end?

I realize this is probably a very simple exercise but this is my first effort using loops so I'm still trying to get a grasp on it
 

Attachments

  • Capture.PNG
    Capture.PNG
    29.9 KB · Views: 1,075
Last edited:
Physics news on Phys.org
  • #2
You need a running total for the checksum. In your current for loop, each increment of your loop variable, you erase what was originally in check and replace it with num*i. Also, cat seems undefined. Take a look at this piece of code that correctly gives the checksum:

Code:
isbn=input('Enter 9 Digit ISBN number: ','s');
check = 0;
for k = 1:length(isbn)
    check = check + str2double(isbn(k))*k;
end

checksum = mod(check,11) %correct now
The difference is I say check = check + number. So I say the new check is equal to whatever was in there before plus this additional number. It is important to realize that check = check + number isn't the same kind of equal sign as in algebra. Also, I define check = 0 before entering the loop. Otherwise, I will get an error, because the code will say new check = old check + number. But old check doesn't exist!

On a side note, the default increment is 1, so you don't need to put it in your for loop if you want to leave it out.

I imagine her idea behind mentioning string concatenation was to provide a tool for you to create the output. I notice in the screenshot there are dashes, numbers, and potentially an X. So you can create this output string with a few concatenations.
 

Related to MATLAB programming assignment (for loop)

What is a for loop in MATLAB?

A for loop in MATLAB is a programming construct that allows you to repeatedly execute a block of code for a specified number of times. It is used for automating repetitive tasks and iterating through data structures such as arrays.

How do you write a for loop in MATLAB?

To write a for loop in MATLAB, you need to specify the loop variable, the range of values to iterate through, and the code to execute inside the loop. The general syntax is:
for variable = start_value : end_value
    code to be executed
end
You can also use the "step" keyword to specify the increment or decrement value for the loop variable.

What is the difference between a for loop and a while loop in MATLAB?

A for loop is used when you know the number of iterations in advance, whereas a while loop is used when the number of iterations is not known beforehand. A for loop is also useful for iterating through data structures, while a while loop is generally used for checking a condition and performing an action based on that condition.

Can a for loop be nested in MATLAB?

Yes, a for loop can be nested within another for loop or any other kind of loop or conditional statement in MATLAB. This can be useful for more complex programming tasks that require multiple levels of iteration.

How can I optimize my for loop in MATLAB for better performance?

To optimize your for loop in MATLAB, you can preallocate any arrays or variables that will be used inside the loop, as this can significantly improve the speed of the code. You can also vectorize your code by using array operations instead of individual element operations, which can also improve performance.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
10
Views
1K
  • Programming and Computer Science
Replies
16
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
12
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
10
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
Back
Top