I have no idea how to do this

My professor just started going over programming in MATLAB however he is not doing anything close to a good job of explaining it. my current assignment is
Given the array Y from the last homework assignment. Write a program that will add the terms of the array, one at a time, until the total is equal to or greater than 3500. Out put the value of the last element that was added.
Y = [95 93 69 75 74 88 83 91 90 78 89 88 86 79 80 55 32 100 97 81 70 ...
75 77 96 83 86 74 91 89 92 79 70 83 88 91 100 76 41 73 71 79 94 ...
105 92 89 89 87 66 94 97 92 78 84 89 ]
I have absolutely no idea how to go about this and i have no experience with programming. Does anyone know how to do this?

5 Comments

Matt Fig
Matt Fig on 29 Sep 2012
Edited: Matt Fig on 29 Sep 2012
Please show at least some effort for your homework problem. I understand it is hard when learning something new, but you need to show some effort... Even give some pseudo-code that shows you know how to begin.
Your professor knows this forum also. If you want to criticize him, I suggest to do this personally, but not in a public forum.
Have you taken any programming at all before? Adding (accumulating) something in a loop is reall, really basic stuff and is almost the same in every language, so you'd already know that if you took any kind of programming course at all before now. To get out of a loop, you call "break" - just like you do in C and some other languages. The "if" test you'll use is also pretty much the same except that there are no parentheses needed around the condition, and there is nothing to start the block, like no "{" or no "then" or no "begin" needed. And all loops end with the same single word "end".
Nathan
Nathan on 29 Sep 2012
Edited: Matt Fig on 29 Sep 2012
I have never taken any programming before. this is what ive got and it isnt doing anything remotely close to what i want, ive played around with it quite a bit and gotten nowhere
Y = [95 93 69 75 74 88 83 91 90 78 89 88 86 79 80 55 32 100 97 81 70 ...
75 77 96 83 86 74 91 89 92 79 70 83 88 91 100 76 41 73 71 79 94 ...
105 92 89 89 87 66 94 97 92 78 84 89 ];
Sum(Y) = 0; ctr = 0;
while Sum(Y) >= 3500
ctr = ctr + 1;
Sum(Y) = Sum(Y) + Y;
end
Sum(Y)
There is a little button that looks like this '{} Code' please highlight your code and the press that button in the future. Thanks!

Sign in to comment.

 Accepted Answer

Matt Fig
Matt Fig on 29 Sep 2012
Edited: Matt Fig on 29 Sep 2012
You were very close, actually. A couple of things. One is, Sum holds your sum, so don't treat it like a vector by indexing into it. Second is your conditional. You want to only go through the loop while Sum is LESS than 3500, not greater! Since you showed some good effort, I will help you out by fixing these simple errors. Study the code to see what does what.
Y = [95 93 69 75 74 88 83 91 90 78 89 88 86 79 80 55 32 100 97 81 70 ...
75 77 96 83 86 74 91 89 92 79 70 83 88 91 100 76 41 73 71 79 94 ...
105 92 89 89 87 66 94 97 92 78 84 89 ];
Sum = 0; ctr = 0;
while Sum <= 3500
ctr = ctr + 1;
Sum = Sum + Y(ctr);
end
Y(ctr)
Sum

1 Comment

thank you for your help and explanation

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Asked:

on 29 Sep 2012

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!