MATLAB: I have no idea how to do this

for loophomeworkif statementsumwhile loop

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?

Best Answer

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