Use while-end, if-end, break commands to solve the numerical problem

1 view (last 30 days)
im having problems figuring out a part of my uni assignment: Use while-end, if-end, break commands to solve the numerical problem Write a script file named PBTask3p5.m that sums a sequence of random numbers (use randn) until the sum is greater than 10. If the random number generated is greater than 3, display a message “The random number is greater than 3” and then exit the loop immediately without doing the sum. Display the sum of the random numbers and the last random number generated in command window when the program terminates. The display format may like this: >> PBTask3p5 The sum of the random numbers is: 10.571232 The last random number generated is: 1.618112
here is a very basic attempt i made before realising i didnt know what i was doing
x=[randn randn randn randn randn randn randn randn randn randn];
sum=0;
k=1;
while sum<10
k=k+1;
sum=sum+x(k);
end
disp(sum);
disp(k);
if anyone could point me in the right direction that would be appreciated
  1 Comment
Stephen23
Stephen23 on 1 Oct 2018
sum=0;

Do NOT use sum as a variable name: this shadows the inbuilt, very important sum function, and is likely to break a lots of code.

Sign in to comment.

Answers (1)

Stephen23
Stephen23 on 1 Oct 2018
Edited: Stephen23 on 1 Oct 2018

"if anyone could point me in the right direction that would be appreciated"

Your assignment points you in the right direction. Start by reading it again:

"Use while-end, if-end, break commands to solve the numerical problem..."

So you need to generate random numbers inside a while loop until some condition is met, testing another condition using if and leaving the loop using break... this is pretty easy to transfer into code:

tot = 0; % Do NOT use SUM as a name.
while ???
    num = randn(???)
    if ???
        ???
        break
    end
    tot = ???
end
  2 Comments
shelly
shelly on 1 Oct 2018

ive been playing around with it a bit and now i have this

tot=0;
k=1;
while tot<10
    num=randn(1:k);
    k=k+1;
    if num>3
    disp('the random number is greater than 3')
    break
    end
    tot=tot+num;
    if tot>10        
    break
    end
end
fprintf('the sum of the random numbers is: %8.7f', tot)
fprintf('The last random number generated is: %f',num)
Stephen23
Stephen23 on 1 Oct 2018
Edited: Stephen23 on 1 Oct 2018
Basically it looks okay. But you need to fix the randn call. This
randn(1:k);
will create a matrix with size 1*2*3*4*5...*k, which is unlikely to be what you want. Read the randn documentation again (hint: you don't need to call randn with any input arguments, and you don't need k at all).
Also get rid of these lines:
if tot>10
break
end
because that is exactly what the while-loop condition already tests for. There is no point in testing the same condition twice.

Sign in to comment.

Categories

Find more on Creating and Concatenating Matrices 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!