[vox-tech] C-Newbie needs help with a source-code
Nicole TWN
vox-tech@lists.lugod.org
Thu, 30 Oct 2003 12:16:51 -0800
At 06:06 AM 10/30/2003, you wrote:
>what the program is about: you may input a decimal number and the program
>will convert it into a dual number... after this it will determine the
>length of the dual number (bestimme_laenge() ) then it should declare an
>array Dec[] with last position --> lange-1 then the positions Dec[i] ...
>i++ were filled in while while() after this it shut put out the positions
>of the array from Dec[lange] to Dec[0] so that the dual number is in the
>right order... but there are more numbers in the output the array shoul be
>long... o.O thats what i don't understand... i hope you can help me with
>this prob... greetz DaRkI sorry, my english is bad.. i know... but I hope
>you'll understand what i mean ;)
OK, but you're not giving us a whole lot to work with here. Let's break
this into smaller problems.
So: you need to convert a decimal number into a binary number, then fill a
variable-length array with the binary digits? And you don't know how big
it's going to be?
Now, the problem here is that plain C will not allow you to declare new
variables in the middle of a program (though C++ will). Your question
seems to be: how do I know how big to make it? I can think of two
different approaches:
1. Dynamic memory allocation. At the top of your program, declare a
pointer (int *i) or an empty array (int i[]). Once you know how big the
binary number is going to be, use malloc() to assign some memory to that
pointer.
2. Use your brain. What's the biggest number a single integer variable can
store? Why? How is this related to the largest size your array will ever
be? Hint: look up sizeof(int) on your computer.
Hope this helps.
--nicole twn