Logo 
Search:

Java Answers

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds
  Question Asked By: Hayfa Khan   on Nov 06 In Java Category.

  
Question Answered By: Jezza Brown   on Nov 06

your problem has nothing to do with Delphi, Java, or any other
language. You have a fundamental task to solve. Which is called:
thinking like a programmer.

What I mean is the following: you have been asked  to convert  a
decimal number (that is, a string consisting of digits 0 through 9)
to a binary  number. This is a very basic task to be performed by
every compiler and every computer application. Or what do you think
does a compiler do when it encounters something like this?
IntVar := 2;

Correct me if I'm wrong, but I think you don't know how to accomplish
this task at all. And that's programmer's basic knowledge. It works
like this:
Suppose you have an integer number n given in base k; that means you
have a string of digits in the range 0 through k-1; for a decimal
number (base 10) it is you have a string of digits ranging from 0
through 9 (which is 10 - 1).
You have to convert this number to base m (different from k); that
means you have to generate a string of digits in range 0 through m-1.

In your example this means you have to convert a decimal  number (base
10) to a binary number which has base 2. The way you do it for
integer numbers is (I'll give the algorithm in some Pascal-like
style):

help_var := n;
Str_result := '';
while help_var != 0 do
digit := help_var mod m;
help_var := help_var / m;
Comment: This ^^ is an integer division!
Str_Result := Str_Result + Chr( 48 + digit);
Comment: this assumes that you're using ASCII codes
where '0' = Chr(48) and '9' = Chr(57).
end while;
Reverse( Str_Result);
WriteLn( Str_Result);

I hope you get what I mean regarding programmers' basics. No offence
meant.

If the algorithm is unclear, please ask again.

As to your question about such basic questions:
1) try Kernighan & Ritchie, "ANSI C"; they have loads of examples.
Even if you don't want to learn C these examples will help  you to get
some feeling for programming.
2) Read Donald Knuth, The Art Of Computer Programming. All three
volumes. I know, it's a lot of hard stuff, but if you've read this
and worked through the examples you'll have gained more experience
than most programmers I've ever met. Honestly.

Share: 

 

This Question has 5 more answer(s). View Complete Question Thread

 
Didn't find what you were looking for? Find more on help needed for logic building Or get search suggestion and latest updates.


Tagged: