MATLAB programming assignment (for loop)

Click For Summary
SUMMARY

The discussion focuses on solving a MATLAB programming assignment involving the calculation of a checksum for a 9-digit ISBN number using a for loop. The user initially struggles with accumulating values within the loop but receives guidance on correctly implementing a running total for the checksum. The final solution involves initializing a variable, using str2double for digit conversion, and employing the modulus function to determine the checksum. Key corrections include ensuring the variable 'check' accumulates values rather than being overwritten and clarifying the use of string concatenation for output formatting.

PREREQUISITES
  • Understanding of MATLAB syntax and functions
  • Familiarity with for loops in programming
  • Knowledge of the str2double function for string to number conversion
  • Basic understanding of checksum calculations and modulus operations
NEXT STEPS
  • Learn about MATLAB string concatenation techniques
  • Explore error handling in MATLAB to manage undefined variables
  • Study the use of the mod function in MATLAB for various applications
  • Investigate advanced looping techniques in MATLAB for more complex problems
USEFUL FOR

Students learning MATLAB programming, particularly those working on assignments involving loops and checksum calculations, as well as educators seeking to provide clear examples of coding best practices.

GE2014
Messages
11
Reaction score
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,169
Last edited:
Physics news on Phys.org
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.
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 10 ·
Replies
10
Views
2K
Replies
2
Views
2K
Replies
3
Views
3K
  • · Replies 12 ·
Replies
12
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 20 ·
Replies
20
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 10 ·
Replies
10
Views
3K