Basic While Loop (divide random scalar by 7 until less than 1) Question

7 ビュー (過去 30 日間)
Batuhan Yildiz
Batuhan Yildiz 2022 年 12 月 7 日
編集済み: Bhanu Prakash 2023 年 2 月 23 日
Requirements (3/4 finished except last one):
  • Is first output whatsLeft correct for input number of 256?
  • Is second output divisionCount correct for input number of 256?
  • Are both outputs correct for an input that is a random number in the thousands
  • Does solution use a while loop?
Here's is my code so far:
function [whatsLeft, divisionCount] = divideBySeven(number)
%Enter the code for your function here.
if (number > 0)
temp = number;
counter = 0;
while temp >= 1
temp = temp/7;
counter = counter + 1;
end
whatsLeft = temp;
divisionCount = uint8(counter);
end
end
  2 件のコメント
Sai
Sai 2022 年 12 月 28 日
The code seems to be correct. Any other issues you are facing?

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

回答 (1 件)

Bhanu Prakash
Bhanu Prakash 2023 年 2 月 17 日
編集済み: Bhanu Prakash 2023 年 2 月 23 日
Hi Batuhan,
As per my understanding, you are trying to perform “divide by seven” operation using “while” loop. You have a code containing both “if” and “while” loops in it and I assume that the code needs “while” loop only.
I have updated the MATLAB code, for your reference.
function [whatsLeft, divisionCount] = divideBySeven(number)
counter=0;
%Enter the code for your function here.
while(number>=1)
number=number/7;
counter=counter+1;
end
whatsLeft=number;
divisionCount=counter;
end
Hope this answer helps you.
Thanks,
Bhanu Prakash.

カテゴリ

Help Center および File ExchangeStartup and Shutdown についてさらに検索

タグ

製品


リリース

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by