While loop to generate between 3 and 5 loops of user input numbers then multiply those user input numbers for a running product

3 ビュー (過去 30 日間)
hello ive started to use for and while loops in class to get a random number of loops asking for my input in a number then we need to add those numbers up to get a product ive done it with a for loop to get a sum of all numbers but i am stuck on the while loop and help would be apperciated her is the question; any help would be apperciated!
  2 件のコメント
dpb
dpb 2020 年 3 月 2 日
We don't do HW w/o seeing the student's work and asking specific ML questions where get stuck...
Walter Roberson
Walter Roberson 2020 年 3 月 2 日
The documentation for rand() shows how to generate a random number within a particular range.

サインインしてコメントする。

回答 (1 件)

NVSL
NVSL 2025 年 1 月 29 日
編集済み: Walter Roberson 2025 年 1 月 29 日
I see you want help in executing a while loop random number of times. Below is an example of how a random number can be generated in a specified range.
r = randi([3 5])
The above code generates a random integer in the range of 3 and 5 inclusive. You can use this “r” to control the number of iterations of while loop by using a counter variable. The below code snippet demonstrates how to do the same:
cnt = 0
while cnt < r
%perform operations - like taking input and computing running product
cnt=cnt+1
end
For more details, please refer to the following MathWorks documentations:
Hope this helps!

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by