How does this code sum the first 100 even integers?
15 次查看(过去 30 天)
显示 更早的评论
n=200;
sum=0;
for ctr=0:1:n
if mod(ctr,2)==0
sum = sum+ctr;
end
end
disp(sum)
Apparently this code sums the first 100 integers. I get the first three lines (n= # loop will stop after, sum= initialization value, ctr=0:1:n means that loop will begin at 0, and repeat in increments of 1 until it reaches 200). But, I'm confused on the mod part. Shouldn't it be "mod(sum,2)==0"? why is it ctr? Okay, wait. So, what is ctr exactly? Ctr is 1,2,3,4,5, etc? But then isn't "sum" also 1,2,3,4,5, etc? Can someone explain the difference between sum & ctr and why mod uses the latter? Thank you. I just dug myself into a hole of confusion.
3 个评论
Jan
2018-2-4
@Sofia: Because we find a corresponding question every week. A user redefined a Matlab function by a variable and is surprised, if the original function does not work anymore. Example:
clear('sum') % undefined at first - just to be sure...
sum(1:10) % Matlab's SUM command
sum = 17 % Shadowing the built-in function SUM
sum(1:10) % ERROR! Index exceed array dimensions
Walter Roberson
2018-2-5
It is quite common for people who use sum as a variable name to later end up trying to also use the function sum() in the same workspace, forgetting that when they assigned a value to the variable sum that that disables calling the function sum() because variables always have higher priority when trying to figure out what a particular name is referring to.
Your code also has to be read by other people, who are going to expect that sum refers to the function rather than to a variable.
By the way, in languages which avoid the problem with sum by calling the function total() instead, it is common that people start using total for the variable name. The name used for the function pre-conditions people's thoughts and they tend to use the same name.
回答(3 个)
James Tursa
2018-2-4
The code picks off the values of ctr that are even and adds them into the variable called sum (a lousy name for a variable btw because it shadows an important function of the same name). So sum will be 2, then 2+4, then (2+4)+6, etc.
0 个评论
Image Analyst
2018-2-4
It should not be "mod(sum,2)==0" because the horribly-named "sum" is ALWAYS an even number even when the loop iterator, ctr (another bad name), is odd. You don't want to add every single number. That's why you need to check ctr rather than sum.
0 个评论
Jan
2018-2-5
Apparently this code sums the first 100 integers.
No, it does not do this. As mentioned in the title of your question, it adds the first 101 even elements only. If you consider 0 to be an even integer, the results equals the sum of the first 100 even integers > 0.
what is ctr exactly? Ctr is 1,2,3,4,5, etc?
Almost correct. It is one of the elements 0,1,2,3,4,5...
But then isn't "sum" also 1,2,3,4,5, etc?
No, because the if command excludes all odd elements.
By the way: The code can be simplified:
n = 200;
s = 0;
for ctr = 0:2:n % Instead of 0:1:n
s = s + ctr;
end
Or
s = sum(0:2:n)
or according to Gauss:
s = 100 * (2 + 200) / 2
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!