Azzera filtri
Azzera filtri

using a while loop, divide the number 256 by 7 until the value remaining is less than one

23 visualizzazioni (ultimi 30 giorni)
assign the final value to output variable WhatsLeft and count the number of divisions required and assign the result to variable DivisionCount
number = 256;
whatsLeft = number<1
while number>1
number = number/7
end
divisonCount = cnt number/7
cnt
end
  2 Commenti
Walter Roberson
Walter Roberson il 31 Ago 2022
divisonCount = cnt number/7
could you explain that line? Taking into account that you would not have left the loop until number became less than 1

Accedi per commentare.

Risposta accettata

Chunru
Chunru il 1 Set 2022
  • using a while loop, divide the number 256 by 7 until the value remaining is less than one
  • assign the final value to output variable WhatsLeft and count the number of divisions required and assign the result to variable DivisionCount
Start with:
number = 256;
Divide the number by 7. The remaining value is 256/7=36.5714
Since the remaining value is not less than 1, keep going: Divide the remaining value by 7. The remaining value is 36.5714/7=5.2245
Since the remaining value is not less than 1, keep going: Divide the remaining value by 7. The remaining value is 5.2245/7=0.7464
Since the remaining value is less than 1, exit the loop
remaining = number;
cnt = 0; % count how many divisions are done
while remaining >= 1
remaining = remaining / 7;
cnt = cnt + 1;
end
remaining
remaining = 0.7464
cnt
cnt = 3
remaining*7^cnt % original number
ans = 256
  5 Commenti
Chunru
Chunru il 1 Set 2022
As a faculty member for many years, I will explain to my students the similar way when they approach to me for questions, even it is the home work questions. It can be taken as an encourage for learning. The home work is for studuent to learn. As long as they LEARN, the goal is achieved. It is also important to know that different people has different learning capabilities, teachers/professors have to make judgement (though it may be wrong) to know what is the best way to teach.
I agree that ideally giving code for a similar task is a better way. But rounds of discussion for a very beginner question like above in a platform like this forum may be too much for both questioners and answers if we go for the ideal approach. It is a trade-off to me.
Our judgement may be different but our goals look the same. :-)
Walter Roberson
Walter Roberson il 1 Set 2022
The student who is capable of generalizing from examples is fine working with similar tasks instead of needing the direct homework done for them.
The student who struggles to generalize from examples is going to struggle to generalize from seeing the direct homework done anyhow, but is going to go ahead and submit the solution someone else has done for them, having learned only that if they ask on the Internet, someone else will do their work for them.
We have been here on MATLAB Answers for 11 1/2 years. We have a lot of experience with students not bothering to study the techniques once someone has given them a complete homework solution. We find it best to set expectations that we will not do homework for people -- but that we will explain concepts and give guideance.

Accedi per commentare.

Più risposte (0)

Community Treasure Hunt

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

Start Hunting!

Translated by