How to add numbers without using "+" sign?
3 views (last 30 days)
Show older comments
write a function which takes two inputs and returns the sum. Matlab inbuilt function are not allowed, you can only use for,if and while loop if necessary.
0 Comments
Answers (1)
Image Analyst
on 6 Nov 2015
I think the intent is that you can't use the sum() function and the length() or size() functions, not that you can't use mathematical operators like +.
So you can have a for loop to add the vectors. Since you can't use the length() or size() function, it's hard to know when to quit adding elements. I'd use a try catch inside the for loop. It will go to the catch when you try to index beyond the length of the array. In the catch, have a "break" command to exit the loop. Don't use sum as the name of your accumulator variable since it's the name of a built in function.
0 Comments
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!