please help me with my C programming lab question

Gage8

Senior member
Feb 11, 2003
632
0
0
Our professor threw this lab assignment at us:

Input an integer containing only 0s and 1s (i.e. as a "binary" integer) and print its decimal equivalent. (hint: Use the modulus and division operators to pick off the "binary" number's digits one at a time from right to left). Just as in the decimal number system where the rightmost digit has a positional value of 1, and the next digit left has a positional value of 10, then 100, then 1000, etc., in the binary number system the rightmost digit has a positional value of 1, the next digit left has a positional value of 2, then 4, then 8, etc. Thus the decimal number 234 can be interprete4d as 4 * 1 + 3 * 10 + 2 * 100. The decimal equivalent of binary 1101 is 1 * 1 + 2 + 1 * 4 + 1 * 8 or 1 + 0 + 4 + 8 or 13.


We are on chapter three learning about control structures so we haven't gotten that far into the course. The main thing I don't understand is the part about "using the modulus and division operators to pick of the "binary" number's digits one at a time." It is pretty clear how you determine the decimal equivalent but what is the logic behind finding each digit's value? I have no clue how to write the code to find the value of each digit in the binary number 1101.

Does anyone have any ideas? I would appreciate any help.

Thanks,
Gage
 

EagleKeeper

Discussion Club Moderator<br>Elite Member
Staff member
Oct 30, 2000
42,589
5
0
Once you understand the intent behind modulus, then everything may fall into place.
 

Gage8

Senior member
Feb 11, 2003
632
0
0
Animal: that is a very good question. I'm not sure.


sooo...what is the intent behind modulus? I thought it gave you only the remainder.
 

EagleKeeper

Discussion Club Moderator<br>Elite Member
Staff member
Oct 30, 2000
42,589
5
0
Originally posted by: Gage8
Animal: that is a very good question. I'm not sure.


sooo...what is the intent behind modulus? I thought it gave you only the remainder.
Exactly.
Take the modulus and reformat into what you are looking for (decimal equivalent)

 

Gage8

Senior member
Feb 11, 2003
632
0
0
but the big question is how. if i have a random binary number such as 1101 what do i divide by to find each digit? That is the big question.
 

Ynog

Golden Member
Oct 9, 2002
1,782
1
0
Since this is homework, I want to try to help you solve the problem yourself.

But here is a different question that might cause you to figure out the answer.

Say you have the integer 6389.

How would you go about finding out the, ones digit, tens digit, hundreds digit and
thousands digit.
How would you solve the problem if you had find out A,B,C,D if you were required
to print out, (A) * 10^3 + (B) * 10^2 + (C) * 10^1 + (D) * 10^0 = 6389
Obviously the answer would be A=6, B=3, C=8 and D=9.

Now if you can figure that out. Say instead of the decimal integer 6389, you had
the decimal integer 1101. Could you find out the A,B,C,D?
Once you do. It shouldn't be that hard to take the newly learned A,B,C,D and
solve the rest of the problem.


If you really cannot solve it, I can give a more obvious hint or the actual answer.
 

Gage8

Senior member
Feb 11, 2003
632
0
0
Ynog thanks a bunch! That helped me to understand how to do it. I need to divide by the place of the digit. With 1101 I would first look at the 1 in the thousandths place and divide by 1000. Therefore, 1101/1000 is 1 which shows me the first digit. Then its 1101-1000 = 101. then 101/100 = 1. And so on.

Cool. my only problem is testing it. Is there a free version of C i can download to test this?

You're the best Ynog.
 

Ynog

Golden Member
Oct 9, 2002
1,782
1
0
Gage, if your in windows, I really don't have a wide knowledge base on free C compilers, but I know you can get
cygwin and it has a free gcc compiler. I'm sure others here might know better than I, if there is a better free
windows compiler. Just as a question, if this is for a computer lab, don't they have computers to use, or provide
information on where to compile.


Gage, just as another hint, You have the write idea.

However you need to do it alittle differently.
Remember, you with 1101, you know there are 4 digits, what if you don't know how many digits are entered.
Which you won't. Because you enter it into an integer, you could have something bigger like 100000101010
or something smaller like 11.

You need to make sure you get all the digits entered. This is were modulus plays a much bigger role.
You have to start from the right and work left.
 

Gage8

Senior member
Feb 11, 2003
632
0
0
Well, i was going to try to avoid using the modulus. Our professor put a limit of 5 digits so i will test for this initially and give an error message if there is more than five. I was planning to start on the left and work right, subtracting as i go.

Is it easier to do it with modulus? If so could you explain it to me as I don't see how the remainder comes into play in this.

Thanks,
Gage
 

Ynog

Golden Member
Oct 9, 2002
1,782
1
0
Its just as easy to do with modulus.


If you have 6389 mod 10, the answer is 9 right.
If you then divide it by 10, you get 638. Because when dividing with integers there is no remainder.
So 638 mod 10 gives 8.
Divided by 10 gives 63. And so on. you now get the numbers in reverse.

Plus there is a good chance you'll have a problem down the road that asks to reverse the numbers of
a value anyway. And modules is the best approach.

You can solve the problem either way, however I guess that your professor would prefer the modulus approach.
 

Gage8

Senior member
Feb 11, 2003
632
0
0
ok...the light is turning on. I see what you mean. The thing that wasn't connecting was that dividing by 10 with mod gives you the last number you are looking for. Using the calculator was probably limiting my understanding this concept because there is no mod feature on it.

I don't think my professor would mind you helping me out because this is just the logic part of the problem. Now I have to actually code it.

As to your question before about testing it on campus, we are using a unix server (where i'm just learning my way around) and every time I try to test it with ./filename it tells me that I don't have permission to do that. Something tells me that my permissions are messed up on my account because another student was able to do it just fine. That is the only way I am aware of that we can test our programs.

Thanks again for your help Ynog. I really appreaciate it.
Gage
 

Ynog

Golden Member
Oct 9, 2002
1,782
1
0
As for the ./filename, you sound like you need to compile the program.

C isn't like a scripting language you cannot just run the file. You need to compile it first.
I would write the program and save it as something.c

So you then need to compile it by doing either cc or gcc, depending on the compiler you have. cc will probably work.
the command would look like

cc something.c - Now this generates a a.out file. then run a.out ./a.out
or you can do. cc something.c -o program_name - where program name is the name of for the program.

 

Gage8

Senior member
Feb 11, 2003
632
0
0
Oh, hmmmm. We haven't learned any of that so maybe our professor doesn't want us to test it yet. I guess he just wants us to write it out and hope it is right. I found a compiler for windows called Pelles C for windows but so far i haven't had much luck.

I guess I'll just have to store up all my questions for my professor. I'm learning that this isn't as straitforward as visual basic. Oh well, if at first you don't succeed, try, try, try, try, try...and try again.

Thanks again for your help. I do understand using mudular now.
Gage