Please help with insights

Problem: Assuming you have n=[16,18,23,24,39,40] values, how would you create a funtionality in Matlab to output the number of values needed to make a sum of 100, every time you call out a random value amongst them.
To eplain further: Assume it's a dart board, and the question is, how many darts do I need to throw and hit any number and after a number of throws the hit numbers add up to a 100?
I'm new to Matlab but I would solve the same with C/C++. Any insights are highly appreciated.

Answers (1)

Azzi Abdelmalek
Azzi Abdelmalek on 19 Apr 2013
Edited: Azzi Abdelmalek on 19 Apr 2013
n=[16,18,23,24,39,40]
find(cumsum(n)>=100,1)
%or
a=0;
k=1
while a<100 & k<numel(n)
a=a+n(k);
k=k+1;
end
out=k-1

Categories

Find more on Programming in Help Center and File Exchange

Asked:

on 19 Apr 2013

Community Treasure Hunt

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

Start Hunting!