basic while loop help
hello. my homework assignment is this:
1. In a single m-file use "while" loops to do the following (your m-file will have 5 separate pieces of code for (a) thru (e)) – first, display your name:
a. sum random integers between 1 & 13 (playing card numbers) until the sum is greater than 40. Let the sum echo to the command window.
b. repeat (a) and keep track of how many draws were required to exceed 40. Use fprintf to print the number of draws to the screen.
c. repeat (a), only this time if an 8 is drawn, jump out of the loop (“breakâ€) and fprintf that the program was terminated since an 8 was drawn
d. repeat (b), only if an 8 is drawn jump back to the top of the loop (“continueâ€) without including the 8 in sum.
e. repeat (a) only make each element of a vector, z, equal to the previous element plus the new random integer, again until the sum exceed 40. So, y(1)=0; y(2)=y(1)+1<SUP>st</SUP> random integer, etc. – doing this in a "while" loop. Repeat (a) through (e) on your own using “for†loops.
I'm very new to programming and while loops are giving me trouble.
For (a) i've got:
For (b) I would need to basically count the number of (ic)s from (a) that it took to get over 40, correct? for example, is my output from (a) was ic =
1
rsum =
2
rsum =
3
ic =
8
rsum =
11
ic =
1
rsum =
12
ic =
11
rsum =
23
ic =
4
rsum =
27
ic =
3
rsum =
30
ic =
10
rsum =
40
ic =
6
rsum =
46
ic =
4
then the number of draws would be 9. I don't know how to add these ic's within the while loop.
Can someone give me a hint please?
I haven't attempted the rest of the problem yet, so I am NOT asking for any help on them. Simply posted it anyway thinking it would clear up whats going on. I will attempt those after I figure out the first two steps.
Thanks for any help on (a) and (b),
Hamlet
1. In a single m-file use "while" loops to do the following (your m-file will have 5 separate pieces of code for (a) thru (e)) – first, display your name:
a. sum random integers between 1 & 13 (playing card numbers) until the sum is greater than 40. Let the sum echo to the command window.
b. repeat (a) and keep track of how many draws were required to exceed 40. Use fprintf to print the number of draws to the screen.
c. repeat (a), only this time if an 8 is drawn, jump out of the loop (“breakâ€) and fprintf that the program was terminated since an 8 was drawn
d. repeat (b), only if an 8 is drawn jump back to the top of the loop (“continueâ€) without including the 8 in sum.
e. repeat (a) only make each element of a vector, z, equal to the previous element plus the new random integer, again until the sum exceed 40. So, y(1)=0; y(2)=y(1)+1<SUP>st</SUP> random integer, etc. – doing this in a "while" loop. Repeat (a) through (e) on your own using “for†loops.
I'm very new to programming and while loops are giving me trouble.
For (a) i've got:
ic=1 rsum=round(rand*13) while rsum<=40 rsum=rsum+ic ic=round(rand*13) endis that doing the right thing? I think it is but im not sure.
For (b) I would need to basically count the number of (ic)s from (a) that it took to get over 40, correct? for example, is my output from (a) was ic =
1
rsum =
2
rsum =
3
ic =
8
rsum =
11
ic =
1
rsum =
12
ic =
11
rsum =
23
ic =
4
rsum =
27
ic =
3
rsum =
30
ic =
10
rsum =
40
ic =
6
rsum =
46
ic =
4
then the number of draws would be 9. I don't know how to add these ic's within the while loop.
Can someone give me a hint please?
I haven't attempted the rest of the problem yet, so I am NOT asking for any help on them. Simply posted it anyway thinking it would clear up whats going on. I will attempt those after I figure out the first two steps.
Thanks for any help on (a) and (b),
Hamlet
0
Comments
As for (b)... If each while loop rotation counts as a draw, then all you need to do is count the number of loops that happen. Adding a counter to a loop is pretty common in programming.
Here's a hint: consider adding another variable - one that has a value of "1" the first time the loop is run, "2" the second time, "3" the third time... and so on, until the loop is done. Can you come up with a line that gives the variable the right value each time the loop is run?
I'm stuck on part (e).
The code I have so far for parts (a)-(d) is:
(a)
ic=1;
rsum=round(rand*13);
while rsum<=40;
rsum=rsum+ic;
ic=round(rand*13);
end
(b)
ic=1;
counter=1;
rsum=round(rand*13);
while rsum<=40;
rsum=rsum+ic;
ic=round(rand*13);
counter=counter+1;
end
fprintf('There were %f draws required to exceed 40',counter)
(c)
ic=1;
rsum=round(rand*13);
while rsum<=40;
rsum=rsum+ic;
ic=round(rand*13);
if ic==8;
fprintf('The program was terminated because %f was drawn',ic)
break;
end
end
(d)
ic=1;
counter=1;
rsum=round(rand*13);
while rsum<=40;
rsum=rsum+ic;
ic=round(rand*13);
switch ic
case{8}
continue
end
counter=counter+1;
end
fprintf('There were %f draws required to exceed 40',counter)
(e)
ic=1;
rsum=round(rand*13);
while rsum<=40;
rsum=rsum+ic;
ic=round(rand*13);
end
which is just (a) right now. I need to "make each element of a vector, z, equal to the previous element plus the new random integer, again until the sum exceed 40. So, y(1)=0; y(2)=y(1)+1<SUP>st</SUP> random integer, etc. – doing this in a "while" loop."
so Z will have the same number of elements as there are random integers it takes to hit 40? I need to initialize z inside the loop.. or do I need to initialize it outside and alter it inside? but how do i make z = to the previous element of itselt PLUS the new random integer? I'm confused about where to initialize z, and I know what it wants but my code keeps failing.
thanks for any more help,
hamlet
1. The function e<SUP>x</SUP> or exp(x) can be approximated using the Maclaurin Series.
Write Matlab code using a "while" loop to calculate a vector, exp_mac, which will hold each successive approximation to exp(x). That is, exp_mac(1) = x<SUP>0</SUP>/0!<O:p></O:p>
exp_mac(2) = exp_mac(1) + x<SUP>1</SUP>/1! exp_mac(3) = exp_mac(2) + x<SUP>2</SUP>/2! …etc. Run the calculation (the while loop) until the approximate percent relative error (apre) is less than a specified error (se). apre, is calculated as follows: 100*(((exp_mac(ii)-exp_mac(ii-1))/exp_mac(ii)). Use fprintf with %2.8f to display the value of exp(x) and the final value of exp_mac, use %1.6f to display se and the final value of apre, and use %d to display how many steps were needed for apre to get below se. Example output is shown on the next page.
Deliverables: M-file; executed code for x=8, se=0.001 & x=10, se=0.00001.
1) If you want your code to show up properly, place it inside code tags: [php] [/php]
2) Please don't copy / paste homework problems. We're not doing your HW for you. Your first few posts were great, asking questions about stuff you've already done.