Please help

R

raghu

// Prog. to convert integer to binary

#include<stdio.h>
int main(void)
{
int i=4;
int j;
for(j=0;j<3;j++)
{
i=i%2;
printf("%d",i);
}
return 0;
}

Output should be 001 but output is 000
if i=3 ouput is 111.
Whats the problem in code?
I have compiled in Turbo C.

Please help.
 
R

Richard

raghu said:
// Prog. to convert integer to binary

#include<stdio.h>
int main(void)
{
int i=4;
int j;
for(j=0;j<3;j++)
{
i=i%2;
printf("%d",i);
}
return 0;
}

Output should be 001 but output is 000
if i=3 ouput is 111.
Whats the problem in code?

Do you know what the code is doing?

Do you know what i=i%2 means?

Have you stepped through with a debugger to see what happens to your i
value?

Why do you think that the binary output for "4" should be "001"? it
shouldnt. It should be "100".

Answer these questions and you will be able to fix the code yourself!

Best of luck!
I have compiled in Turbo C.

Please help.

--
 
R

Richard Heathfield

raghu said:
// Prog. to convert integer to binary

#include<stdio.h>
int main(void)
{
int i=4;
int j;
for(j=0;j<3;j++)
{
i=i%2;

You just changed the value of i - which was your only record of the value
you wanted to represent in binary notation - to hold a value of either 0 or
1. All subsequent iterations of the loop will give that same value.

You need to do something like this:

#include <stdio.h>

void printbinary(unsigned int n)
{
if(n / 2 > 0)
{
printbinary(n / 2);
}
printf("%d", n % 2);
}

int main(void)
{
printbinary(4);
putchar('\n');
return 0;
}
 
M

mark_bluemel

raghu said:
// Prog. to convert integer to binary

#include<stdio.h>
int main(void)
{
int i=4;
int j;
for(j=0;j<3;j++)
{
i=i%2;
printf("%d",i);
}
return 0;
}

Output should be 001 but output is 000
if i=3 ouput is 111.
Whats the problem in code?
I have compiled in Turbo C.

Please help.

Richard has answered you in detail. I'll simply repeat what I said in
reply to the same question in the "Please Check" thread...

Here's how I learnt to debug this sort of problem (many many years ago
in COBOL...) :-

Sit down with a piece of paper and a pen, and pretend you're the
computer executing this program. What will the values of the variables
be at each iteration of the loop?

You'd learn a whole lot more about programming in the real world (which
is largely about finding out what went wrong, in my experience) if you
did some of this rather than expecting us to debug your code.
 
M

mark_bluemel

Richard said:
[snip]

Do you know what the code is doing?

Apparently not.
Do you know what i=i%2 means?

He appears not to, but he should be able to read the manual...
Have you stepped through with a debugger to see what happens to your i
value?

Why does he need a debugger? Do you think he's going to be ready to
cope with one if he can't cope with code at this level?

If he follows my suggestion and pretends to be the computer, he'll see
what's going on more clearly than he could with a debugger.
 
N

Norm Mann

raghu said:
// Prog. to convert integer to binary

#include<stdio.h>
int main(void)
{
int i=4;
int j;
for(j=0;j<3;j++)
{
i=i%2;

The above is the problem. You set i to the remainder of the modulus
operation, but you didn't store the "whole" portion. It's the whole portion
from each iteration that needs to be acted on by the modulus operator, not
the remainder.

HTH,

-NM
 
R

Richard

Richard said:
[snip]

Do you know what the code is doing?

Apparently not.
Do you know what i=i%2 means?

He appears not to, but he should be able to read the manual...
Have you stepped through with a debugger to see what happens to your i
value?

Why does he need a debugger? Do you think he's going to be ready to
cope with one if he can't cope with code at this level?

Because a debugger lets you see the data "live". Easy really. Had he
used one he would see his i value changing incorrectly and hopefully he
would have deduced that he had the wrong understanding of that %
does. All done in less time than it takes to stick in a printf or ask
the group.
If he follows my suggestion and pretends to be the computer, he'll see
what's going on more clearly than he could with a debugger.

He can hardly pretend to be a computer if he doesn't know what the
operators do.
 
R

Richard

Richard Heathfield said:
raghu said:


You just changed the value of i - which was your only record of the value
you wanted to represent in binary notation - to hold a value of either 0 or
1. All subsequent iterations of the loop will give that same value.

You need to do something like this:

#include <stdio.h>

void printbinary(unsigned int n)
{
if(n / 2 > 0)

To the OP - also consider using ">>" - it *might* be of interest to you
when shifting bit patterns as you are doing.

Have a look at this:

http://gcc.gnu.org/ml/gcc-help/2001-06/msg00155.html
{
printbinary(n / 2);
}
printf("%d", n % 2);
}

int main(void)
{
printbinary(4);
putchar('\n');
return 0;
}

--
 
A

Andrew Poelstra

// Prog. to convert integer to binary

#include<stdio.h>
int main(void)
{
int i=4;
int j;
for(j=0;j<3;j++)
{
i=i%2;
printf("%d",i);
}
return 0;
}

Output should be 001 but output is 000
if i=3 ouput is 111.
Whats the problem in code?
I have compiled in Turbo C.

I'll fix formatting again:
/* Prog. to convert integer to binary */
#include <stdio.h>

int main(void)
{
int i = 4;
int j;
for(j = 0; j < 3; j++)
{
i %= 2;
printf("%d",i);
}
return 0;
}

Step through that. On the first iteration, i will be 4 % 2 = 0. On every
iteration after that, it will be 0 % 2 = 0.

There's your logic problem. It's up to you to fix it and come back with
any other issues. HTH.
 
K

Kenneth Brody

Richard said:
Richard said:
[...]
If he follows my suggestion and pretends to be the computer, he'll see
what's going on more clearly than he could with a debugger.

He can hardly pretend to be a computer if he doesn't know what the
operators do.

And he won't understand the output of the debugger if he doesn't
know what the operators do, either.

This may be a case of "he should do both". Run it in a debugger,
and at each step, pretend to be the computer and determine what
you think the result should be. Then, if the debugger gives a
different result at a given step, stop and ask yourself "why?"

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:[email protected]>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,280
Latest member
BGBBrock56

Latest Threads

Top