167k views
2 votes
Code has already been provided to define a function named divideBySeven that accepts a single input variable number that is any positive number. Add commands to use a while loop to repeatedly divide this number by 7 until the value remaining is less than 1. Your function should assign values to the two output variables as follows: Assign the final value of the number to the output variable whatsLeft. Count the number of divisions required and assign the result to the output variable divisionCount with unsigned 8-bit integer datatype. Note the value of the variable number is a function input. Be sure not to overwrite this value in your code. Be sure to assign values to each of the output variables.

Starter code:

function [whatsLeft, divisionCount] = divideBySeven(number)

%Enter the code for your function here.

User Pie
by
8.4k points

1 Answer

2 votes

Final answer:

To solve this problem, you can use a while loop in the function.

First, initialize the 'whatsLeft' variable with the input 'number'.

Then, use a while loop to continuously divide 'whatsLeft' by 7 until it becomes less than 1.

Keep track of the number of divisions using the 'divisionCount' variable.

Step-by-step explanation:

The steps are given below:

  • First, initialize the 'whatsLeft' variable with the input 'number'.
  • Then, use a while loop to continuously divide 'whatsLeft' by 7 until it becomes less than 1.
  • Keep track of the number of divisions using the 'divisionCount' variable.
  • Finally, assign the final value of 'whatsLeft' to the 'whatsLeft' output variable, and assign the value of 'divisionCount' to the 'divisionCount' output variable.

Here's an example of how the code should look:

function [whatsLeft, divisionCount] = divideBySeven(number)
whatsLeft = number;
divisionCount = 0;
while whatsLeft >= 1
whatsLeft = whatsLeft / 7;
divisionCount = divisionCount + 1;
end
end
User Enrico Marchesin
by
8.4k points