Need to create an array that consists of 5 numbers found through a while loop

26 views (last 30 days)
I am creating a game where you loop through a while loop and get a random number between set values. If your number is within a range you get points and that is added to your total. I am trying to also display the values (distance) you get in the command window with fprintf but I am getting a struct value for the array. I have tried multiple struct commands and was eventually able to get it to a cell, but was unable to print. I am also unable to get my append to work. So how do I save these values as I loop through my function and print it out.
userInput = input("Which car variation are you testing?\n1. Gatorade wheels, gatorade bottle\n2. Gatorade wheel, water bottle\n" + ...
"3. Water bottle wheels, gatorade bottle\n4. Water bottle wheels, water bottle\n");
score = 0;
if userInput == 1
count = 0;
while count < 5
a = 10;
b = 20;
distance = (b-a).*rand(1,1) + a;
array = [];
array.append = distance;
distance1 = distance(count);
count = count + 1;
if distance >= 15 && distance <= 18
score = score + 3;
elseif distance < 18
score = score + 1;
elseif distance < 15
score = score + 1;
end
end
elseif userInput == 2
elseif userInput == 3
elseif userInput == 4
else
printf("Sorry, we could not perform your test.");
end
fprintf("You travelled %d and your total score is: %3.0f", array, score);
  3 Comments
Jacob
Jacob ungefär 18 timmar ago
I did the distance1 thing as a lets see what this does and proceed I just forgot to erase it before i asked this question. I fixed the array = []; and the array(end+1) but im getting an array size of 1x10 instead of 5 do you know why this could be.
Walter Roberson
Walter Roberson ungefär 18 timmar ago
if distance >= 15 && distance <= 18
score = score + 3;
elseif distance < 18
score = score + 1;
elseif distance < 15
score = score + 1;
end
Consider a distance value < 15. It would fail the >= 15 & <= 18 test so it would move to the elseif. Since it is < 15 it is certainly < 18 so it would pass the < 18 test. Having succeeded on that test, it would not use the "elseif" comparison against 15.
I suspect the desired test is elseif distance > 18
Note that if the requirement is for the score to be incremented by 1 whenver the distance is outside the range 15 to 18, then you could do that with a simple "else" (unless somehow the distance can end up being nan in which case specific tests for > 18 and < 15 would exclude the nan case.)

Sign in to comment.

Accepted Answer

Stephen23
Stephen23 ungefär 19 timmar ago
Take a look at this line:
array.append = distance;
You looks as if you are trying to write Python code. However, MATLAB does not have lists and it certainly does not have methods (e.g. APPEND) that operate on lists. The MATLAB approach is to use indexing:
userInput = 1;
score = 0;
array = [];
if userInput == 1
count = 0;
while count < 5
a = 10;
b = 20;
distance = (b-a).*rand(1,1) + a;
array(end+1) = distance;
count = count + 1;
if distance >= 15 && distance <= 18
score = score + 3;
elseif distance < 18
score = score + 1;
elseif distance < 15
score = score + 1;
end
end
elseif userInput == 2
elseif userInput == 3
elseif userInput == 4
else
printf("Sorry, we could not perform your test.");
end
fprintf("You travelled %g and your total score is: %u", array(end), score);
You travelled 17.062 and your total score is: 10

More Answers (0)

Categories

Find more on Parallel Computing Fundamentals in Help Center and File Exchange

Products


Release

R2024b

Community Treasure Hunt

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

Start Hunting!