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

Matt Fig
Matt Fig 2012년 9월 29일
편집: Matt Fig 2012년 9월 29일
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.
Jan
Jan 2012년 9월 29일
Your professor knows this forum also. If you want to criticize him, I suggest to do this personally, but not in a public forum.
Image Analyst
Image Analyst 2012년 9월 29일
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 2012년 9월 29일
편집: Matt Fig 2012년 9월 29일
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)
Matt Fig
Matt Fig 2012년 9월 29일
There is a little button that looks like this '{} Code' please highlight your code and the press that button in the future. Thanks!

댓글을 달려면 로그인하십시오.

 채택된 답변

Matt Fig
Matt Fig 2012년 9월 29일
편집: Matt Fig 2012년 9월 29일

0 개 추천

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

Nathan
Nathan 2012년 9월 30일
thank you for your help and explanation

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

질문:

2012년 9월 29일

Community Treasure Hunt

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

Start Hunting!

Translated by