Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

BE 1500 Homework #7 Submit this assignment as a PDF through the Homework 7 ink o

ID: 3820962 • Letter: B

Question

BE 1500 Homework #7 Submit this assignment as a PDF through the Homework 7 ink on Blackboard. Copy and paste your MATLAB commands and final answers into a Word document for each problem. For Excel portions, copy and paste the contents of any cells that contain an equation into the document, and label it with the cell reference. Adetailed explanation of how to do this is available on Blackboard as a video in the Homework folder. Once completed Save As through the File menu to save the document file type as a PDF. Submit the PDF and a zip of your matlab and excel files. 1. create a function called my sum that computes the sum of the numbers in any vector using a while loop. The function should get the vector as an input argument and return the sum as an output argument. Do not use the built in function sum. Test your function on the following 7 21 63 189 567 2. Use the input command inside a while loop to get numbers from the user. The loop should continue until the user enters -1. 3. In the game of blackjack the player can draw cards from a deck until the sum of their card values reaches 21. Create a while loop. In each iteration generate a random number between 2 and 11. Add all these random numbers together until their sum becomes greater than or equal to 21. Then print out the sum and the number of cards dealt. Use this line of code to generate the number: new card floor(rand(1,1) 10 2); Sample output: blackjack Hand Score 21, Number of Cards 3 blackjack Hand Score 26, Number of Cards 4 blackjack Hand score 24, Number of Cards 4 4. Create a while loop. In each iteration generate a random number using rand(1,1) Keep doing that until we get a number between 0.5 and 0.6. Print outthe generated number and number of iterations Sample output: random number n is 4, number is 0.515766 random number n is 7, number is 0.509839 random number n is 14, number is 0.593828

Explanation / Answer

1.

function (sum) = add(x)
length = size(x)(2);
i = 1;
sum = 0;
while (i <= length)
    sum = sum + x(i);
    i = i + 1;
end
end

2.

condition = true;

while (condition)
i = input("enter a number: ");
condition = i~=-1;
end

3.


cardcount = 0;
score = 0;
while score < 21
newCard = floor(rand(1,1)*10 + 2);
score = score + newCard;
cardcount = cardcount + 1;
end
printf("Hand score = %d, Number of cards used = %d ",score,cardcount);

4.

condition = true;
iteration = 0;
while condition
number = rand(1,1);
condition = number < 0.5 || number > 0.6;
iteration = iteration + 1;
end
printf("number = %d, Number of iterations = %d ",number,iteration);