for(; m > 0; m >>= 1);
$ cat test.c
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char ** argv)
{
int m = 20;
#ifdef DEBUG_OUT
printf("Start value of m : %d\n", m);
for(; m > 0; m >>= 1)
printf("Value of m now : %d\n", m);
#endif
#ifndef DEBUG_OUT
for(; m > 0; m >>= 1);
#endif
return EXIT_SUCCESS;
}
$ gcc -DDEBUG_OUT -pedantic -o test.debug test.c
$ gcc -pedantic -o test test.c
$ ./test.debug
Start value of m : 20
Value of m now : 20
Value of m now : 10
Value of m now : 5
Value of m now : 2
Value of m now : 1
$ ./test
$ echo $?
0
Why should we do your homework again ? 🙄
Code:for(; m > 0; m >>= 1);
for(n = 0; (m /= 2) > 0; n++)
printf("Value of m,n now : %d %d\n", m,n);
Gotta love how high-horsed people get on the Internet. From the OP's prior posts, they are just trying to learn on their own.
And unless the OP fat fingered the initial example, I think your solution misses the point
while((m >>= 1) > 0);
Rewrite the following loop so that it's body is empty.
Code:for ( n = 0; m > 0; n++ ) m /= 2;
for ( n = 0; m > 0; )
{
m /= 2;
n++;
}
for ( n = 0; m > 0; m /= 2, n++)
;
And BTW, I doubt the OP fat fingered the example since there's no point in n in a loop without a body. The point was removing the body which was the m /= 2; part. Your solution is as good as mine. Actually, an even better one considering n is just pointless in a bodyless loop (at least for these 1 line examples) :
Code:while((m >>= 1) > 0);
If you're going to use n later in the program to know how many divisions occured, then sure, keep the counting there. (And btw, for a division by 2 on ints, the bit shift operator is much more efficient than a division, though compilers probably just optimize that away).
which is itself equivalant to -
Code:for ( n = 0; m > 0; m /= 2, n++) ;
$ gcc -pedantic -o test test.c
$ ./test
We divided 5 times
We divided 4 times
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char ** argv)
{
int m;
int n;
for(m = 20, n = 0; m > 0; m >>= 1, n++);
printf("We divided %d times\n", n);
for(m = 20, n = 0; (m >>= 1) > 0; n++);
printf("We divided %d times\n", n);
return EXIT_SUCCESS;
}
The code snippet given is completely pointless. What is the initial value for m? Whilst this is not necessary to answer the question, it does seem a bit stupid for an example which presumably is for beginners to c or programming.
Being right depends on what result we want n to have. Since this is unspecified in the problem, we can't know the right answer.
I'd wager that being right is having understood how a for loop works and demonstrating it by incorporating the division in it...
But I'm curious, since n is in the original loop and obviously gets a value there, why do you claim it is unspecified? In my opinion, it either isn't important at all or it should be the same as in the code given.
Being right depends on what result we want n to have. Since this is unspecified in the problem, we can't know the right answer.
It's unspecified in the sense we don't know what n will be used for.
Given that n can have different values depending on the solution and the fact we don't know what n will even be used for, on my part, I think the n is simply there because a newbie to C doesn't realize a for loop can be written as for(;😉 for all the compiler cares and the book/instructor doesn't want to go into explaining it at that point.
Ignoring the fact that it is a question intended for beginners and it is being way overanalyzed, rewriting the code and giving another value of n is worse than removing it completely in my opinion. Much harder to find bugs introduced after refactoring if you do something like that... Off-by-ones... Grr!
I doubt Ulbador will appreciate you calling his answer the worst one in the thread but oh well. 😀
Rewrite the following loop so that it's body is empty.
Code:for (n = 0; m > 0; n++) m /= 2;
KnightWRX said:Actually, an even better one considering n is just pointless in a bodyless loop (at least for these 1 line examples) :
Code:while((m >>= 1) > 0);
Code:for(; m > 0; m >>= 1);
m = 0;
Obviously the point of the snippet is to determine n. Why else you would keep dividing by two repeatedly?
The purpose of any such code outside of a textbook example, yes. Here, n cannot be determined since m is unknown. But for any m, n should be the same for the given example and a correct answer. 🙂
if(m > 0)
m = 0;
KnightWRX said:Being right depends on what result we want n to have. Since this is unspecified in the problem, we can't know the right answer. Is the fact we are counting n to know how many divisions it took for m to be == 0 or how many it took to give us the lowest possible value of m while still > 0 ? Depending on what was sought of n, we may have all missed the point (including you Ulbador...).
Exactly!
But since m will always be 0 for all (positive) values of m, the only relevant part of the snippet is n.
Obviously, this is not what the textbook wants.
No, the textbook wants a solution that is equivalent to the example given.
That is, the answer should produce the same n for any given m. This means that m can only be updated after the conditional check. I believe we are on the same page here. 🙂
But we know what exactly the book wants n to be. In fact, they've give us the whole snippet showing what n needs to be. The problem is fully qualified. The book wants a solution that is equivalent to the snippet.
Ultimately, if we want to absolutely reproduce the same value of n that the original snippet did, lloydean is the one with the most correct answer.
Yes we are on the same page. I was just disputing KnightWRX's solutions.
My solution missed the point if the OP fat fingered the example, not unless.
I've been hanging on programming forums for the last 10 years, I've had my share of answering homework for others. People "just learning on their own" starting at a date right around the start of a semester and then disappearing right at the date that's quite conveniently the end of a semester.
$ cat test.c
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char ** argv)
{
int m = 20;
#ifdef DEBUG_OUT
printf("Start value of m : %d\n", m);
for(; m > 0; m >>= 1)
printf("Value of m now : %d\n", m);
#endif
#ifndef DEBUG_OUT
for(; m > 0; m >>= 1);
#endif
return EXIT_SUCCESS;
}
for ( n = 0; m > 0; m /= 2, n++)
;