![]() |
|
#1
|
|||
|
|||
base conversions c++ (please help)my professor hasn't taught us how to use strings yet and we're supposed to create a prog that converts between bases without them.
I'm having a hard time figuring out how to convert from base 10 to base 2. Please help. I've been trying different things for the past 2 hours and none work right. (He wrote part of the code for us and we receive user input once then use a function to convert.) |
|||
|
#2
|
|||
|
|||
|
Quote:
So what do you have so far? Regards, Dave |
|
#3
|
|||
|
|||
|
Here's the function. I commented out a lot of stuff (in case I may need them later) to try others. Sorry if it looks confusing.
CPP / C++ / C Code:
Last edited by dsmith : 01-Feb-2005 at 21:19.
Reason: Please use [c] & [/c] when posting c code
|
|
#4
|
|||
|
|||
|
Quote:
First of all, before you sit down at the computer and open the text editor to start typing C language statements: Ya gotta have a plan. Here's a way to approach this. First: state the problem: 1. Describe the function in a few words. 2. Define the input(s) to the function. 3. Define the output(s) from the function: return value, if any; side effects, if any. Now: decide how your program is going to implement the functionality. (Choose an algorithm.) Decide how you will test the function. (A main() function that will invoke your function with various input values to test proper operation.) I usually implement the main() function first, since it typically involves user input (console or file input), and I want to make sure I can feed the new function with what I want to. Then implement the new function. Lots of times there are extra printf() statements that show intermediate results of the various operatons to make sure things are going OK. These extra print statements will be commented out or deleted before releasing the final product. Test the function. Use inputs that you know the answer to. Use inputs that visit the "dark corners" of functionality where things may go wrong. (Can it handle negative inputs? Can it handle zero? Can it handle the largest integer that you can represent on the machine. Etc.) I'll show you what I mean for the first few steps. 1. Describe the function: You have to tell me what your assignment is. Saying "convert decimal to binary" is not very descriptive, but maybe that's all you can think of for now. 2. Describe function input(s): An integer in a variable passed as an argument. (You tell me: is this for non-negative integers only, or what. Design and test of the program depend on knowing what you have to deal with.) 3. Describe the function output(s). Now, here's where we first get an inkling of the importance of knowing what we are supposed to do before actually trying to do it. Can the function return an integer? Well it could, but what's the point. The input is an integer,which is represented internally as a binary number in any machine that you or I are likely to have access to. How can we convert a decimal integer to a binary integer? Inside the computer, they are all binary numbers. So, I can think of two possibilities (actually I can think of about a dozen, but I will pick the first two that came to mind). Your assignment should have completely specified inputs, outputs and functionality, but I am making up my own assignment. Here's a couple of possibilities: (a) Print out the binary digits (bits) in human-readable form a bit at a time. So, if the number is 123(decimal) the routine prints out "1111011" or something like that. (b) Put the characters representing the '0' and '1' bits into an array. Maybe the array (as well as the integer to be converted) is an argument to the function. Then the calling function (main()) can print out the values of the array as a string, for example. Let's suppose we choose (a). Then the function has to do two things: 1. extract the bits (value 0 or 1) from the given number 2. print the character '0' or '1' for each bit. Implementation time: For first test, we just pick a number to feed to the function; if your instructor has showed you how to get a user input number, you can use this instead: CPP / C++ / C Code:
Your turn: Regards, Dave |
|
#5
|
|||
|
|||
|
Quote:
Oops, Dave |
|
#6
|
|||
|
|||
base conversionAs a part of a larger program, I need a function whose input is an integer and it returns the corresponding binary number as a STRING of fixed length.
for eg: suppose the length of the string is 8(this is fixed outside the function) and the input is 32 then the function should return 000100000 plz reply ASAP cuz it's quite urgent. thanks in advance Arikeri |
|
#7
|
||||
|
||||
|
Quote:
Be specific in what you need, and don't ask us to write code for you -- it probably won't happen, but we'll be glad to help if you have specific questions. Also, Dave has graciously given greensand a pattern on how to figure it out. Even you are allowed to use what he suggested. You can also look at the Tutorial sub-board above -- I know the answer is there, too. __________________
During the election they said Obama could only be elected when pigs fly. Well, we currently have an epidemic of Swine Flu. Coincidence? |
|
#8
|
|||
|
|||
|
Quote:
Iam sorry for not being specifc. Hereis a function I have written: ------------------------------------------------------------------------ CPP / C++ / C Code:
I get the following error when I compile the program: parse error at end of input ------------------------------------------------------------------------- BIN is a global variable .Its an array of type int Last edited by LuciWiz : 07-Mar-2005 at 08:54.
Reason: Please insert your C code between [c] & [/c] tags
|
|
#9
|
|||
|
|||
|
what Iam doing is the possibility '(b)' in DAVEs reply.
|
|
#10
|
||||
|
||||
|
Quote:
Sticky: GIDForums enables New [C ] / [ C++] bbcode. so your code can be read. Usually what that message usually means is you have too many {'s and not enough }'s __________________
During the election they said Obama could only be elected when pigs fly. Well, we currently have an epidemic of Swine Flu. Coincidence? |
Recent GIDBlog
Once again, no time for hobbies by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Re: Conversion: Binary, Decimal, Hexadecimal | WaltP | C Programming Language | 1 | 10-May-2006 07:49 |
| calling abstract base class method calls draw instead | achoo | FLTK Forum | 1 | 19-Dec-2004 10:38 |
| Help with Decimal to Hex Conversions | jediboy | C Programming Language | 1 | 13-Dec-2004 01:11 |
| Rebase - Must Get! | BobbyDouglas | Miscellaneous Programming Forum | 0 | 18-Aug-2003 05:57 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The