MATLAB programming assignment (for loop)

AI Thread 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 successfully iterates through each digit but struggles with accumulating the total for the checksum, needing to add values rather than overwrite them. A correct approach is shared, emphasizing the importance of initializing the checksum variable before the loop and using addition to accumulate values. The conversation also touches on the potential use of string concatenation for formatting the output, which may include dashes and an 'X' for certain conditions. Overall, the thread highlights key programming concepts essential for completing the assignment effectively.
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,138
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
Views
2K
Replies
10
Views
2K
Replies
12
Views
3K
Replies
10
Views
3K
Replies
5
Views
7K
Replies
3
Views
3K
Replies
16
Views
2K
Back
Top