Note that there are some explanatory texts on larger screens.

plurals
  1. POFor loop without the second condition, i.e. the boolean check?
    primarykey
    data
    text
    <p>I have to write a function that calculates the floor of log base 16 of an unsigned int passed in. There are restrictions as to what operators and what constants we are allowed to use, and we can only use specifically <code>for</code> loops. </p> <p>For clarity, we cannot use any conditional statements(if, else, switch ... ). The function prototype is: </p> <pre><code>int floor_log16(unsigned int x); </code></pre> <p>Allowed operators: <code>++</code> <code>--</code> <code>=</code> <code>&amp;</code> <code>|</code> <code>~</code> <code>^</code> <code>&lt;&lt;</code> <code>!</code> <code>&gt;&gt;</code></p> <p>Allowed constants: <code>1</code> <code>2</code> <code>3</code> <code>4</code> <code>8</code> <code>16</code></p> <p>I wrote a version of the program as follows:</p> <pre><code>int floor_log16(unsigned int x) { int index=1; int count=(1!=1); count--; for(; index&lt;=x; index&lt;&lt;=4) { count++; } return count; } </code></pre> <p>which seems to work as desired. However, I realized that based on the later functions and description of the needed functionality we have to write, I noticed that under "allowed operators" sometimes <code>&gt;</code> and <code>&lt;</code> were listed. </p> <p>I deduce this implies that since for the <code>floor_log16</code> function listed above, we weren't explicitly told to use <code>&gt;</code> or <code>&lt;</code>, I can only assume that the solution posted above will not be accepted. </p> <p>This leaves me rather confused because I don't understand how you can possibly have a for loop without a boolean check? </p> <p>Isn't the whole idea of a loop to iterate while a condition is met? </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload