I am trying to write recursive function which converts a binary numbers into decimal system. I couldn't find the bug. Any help will be appreciated deeply.

Here is my first attempt:

```
#include <iostream>
int sum = 0;
using namespace std;
int powerofO(int taban, int us)
{
int result = 1;
for (int i = 1; i <= us; i++)
result *= taban;
}
int func(int array[], int length, int weight);
int main()
{
int *size;
int digits;
int binary_array[*size];
cout << "how many digits ? \n";
cin >> digits;
*size = digits;
cout << "Enter your " << digits <<
" digits binary number using with a space between"
" each number \n";
for (int i = 0; i < digits; i++)
cin >> binary_array[i];
cout << func(binary_array, digits, -1) << endl;
return 0;
}
int func(int array[], int length, int weight)
{
if (length == 0) {
sum = sum + array[0];
return sum;
}
func(array, length - 1, weight + 1);
sum = sum + array[length] * powerofO(2, weight);
return sum;
}
```

And I tried to rearrange my function to the following, but it is still not working.

```
int func( int array[],int length ,int weight){
length--;
weight++;
if(length == 0){
sum = sum + array[0] * powerofO(2,weight);
return sum;}
sum = sum + array[length] * powerofO(2,weight);
func(array,length,weight);}
```

The cause of the immediate segmentation fault is very near the top of the `main()`

function:

```
int main()
{
int *size;
int digits;
int binary_array[*size];
```

`int *size`

only declares a *pointer to an int*, it does not actually allocate memory for that int. `*size`

is used a little later when trying to allocate the array, and the whole thing blows up.

So, a little re-arrangement and the program *runs*. I don't think the output is correct yet :) but I don't want to take away all your learning opportunities.

```
#include <iostream>
int sum = 0;
using namespace std;
int powerofO(int taban, int us)
{
int result = 1;
for (int i = 1; i <= us; i++)
result *= taban;
}
int func(int array[], int length, int weight);
int main()
{
int digits;
cout << "how many digits ? \n";
cin >> digits;
int binary_array[digits];
cout << "Enter your " << digits <<
" digits binary number using with a space between"
" each number \n";
for (int i = 0; i < digits; i++)
cin >> binary_array[i];
cout << func(binary_array, digits, -1) << endl;
return 0;
}
int func(int array[], int length, int weight)
{
if (length == 0) {
sum = sum + array[0];
return sum;
}
func(array, length - 1, weight + 1);
sum = sum + array[length] * powerofO(2, weight);
return sum;
}
```

In func() you are calling func() again before doing the sum. So then when length finally gets to 0, sum only contains the final digit in array[].

Similar Questions

I want to have a regexp to validate DECIMAL numbers: The valid DECIMAL numbers are : +123 123 -123 +123.0000 +123,123.999 .0234 +1123,233,34 invalid DECIMAL numbers are : +1233+ -1233_ +12.233.33 I

I am trying to create a method that converts Decimal to Binary. Here is what I wrote so far (which is not working properly): public static String D2B(int decimal){ String binaryValue=; for (int temp

It seems every time I go to write a recursive function I end up making it return void and using a ref parameter. I'd much rather be able to write a function that just returns a result list. Apologies

I need to subtract numbers in my table and i'm using php to get the result e.g 17.45 - 7.15. The result im getting is a whole number. How can i get the difference of the two numbers with its decimal p

This question already has an answer here: C++ stream output with 3 digits after the decimal point. How? 3 answers In C language we can write like this; printf(%.2f, number); How can I do tha

I am trying to write a code to get the set of points (x,y) that are accessible to a monkey starting from (0,0) such that each point satisfies |x| + |y| < _limitSum. I have written the below code an

I am trying to understand how the recursive method of deletion of a binary search tree works. The code that I came across in many places looks as follows: void destroy_tree(struct node *leaf) { if( le

Using JSONObject to read a json response from a server. The server returns some decimal number. Normal numbers are not a problem to but the problem occurs when there is decimal number of form 0.00068.

I need a function that takes two parameters as input: 0<=a<=1 and 0<=b<=1. The output is another number c which falls in the interval [0,1] too. c must be greater than or equal to both a a

I bought a Programming book at a yard sale for $2 because I've always wanted to learn how to code but don't have the money and resources for school. I've gotten through the first few chapters just fin

I've been searching a while and haven't gotten anything too useful yet, I'm working on a subnet calculator, and well I have used the decimal to binary which I found here, though, I haven't found a goo

I tried Binary multiplication technique on decimal numbers. Algorithm: To multiply two decimal numbers x and y, write them next to each other, as in the example below. Then repeat the following: divid

Possible Duplicate: Recursive lambda functions in c++0x Here is a plain old recursive function: int fak(int n) { return (n <= 1) ? 1 : n * fak(n - 1); } How would I write such a recursive functi

Hello I have a situation like this: I need to submit a binary value to a php function. Php automatically converts this number to a decimal number. for example if I pass function(1000000000000000100000

When writing a function operating on Stream(s), there are different notions of recursion. The first simple sense is not recursive on the compiler level, since the tail if not evaluated instantly so th

In python, a recursive function for a Fibonacci sequence that returns the nth fibonacci number can be written as: def fib(n): if n == 1: return 0 if n == 2: return 1 return fib(n-2) + fib(n-1) I unde

I have to write two functions converting decimal numers into a (-2)adian number system (similar to binary only with -2) and vice versa. I already have managed to get the decimal -> (-2)adian runnin

How do I get only the numbers after the decimal? Example: 2.938 = 938

I want to avoid from decimal numbers in my Axis, how can I do that ? XAML: <Charting:Chart VerticalAlignment=Stretch HorizontalContentAlignment=Stretch> <Charting:Chart.Axes> <Cha

Hello I was writing a decimal to binary function and I found this code that works perfectly: while n > 0: b = str(n % 2) + b n >>= 1 However I do not know what >>= does could you enlig

This question already has an answer here: Convert hex string to int in Python 9 answers I need to write a function that converts hex to decimal and I've got an idea of how to do it but I'm stuc

I am using StringTemplate in Java. I would like to render decimal number with a certain precision (e.g. 3 digits after the decimal point). Is it possible to for the ST object to do it? And how? Edi

I need to convert a string into binary, and then convert this binary number into decimal. What I was doing was ask the user for a word, transform each character into a list index, and then convert eac

I am not sure if this is good programming practice, but I would like to know if one can define a recursive function using the lambda expression. This is an artificial example I made up: So one can de

I am trying to create the implementation of a recursive version of my binary search. This is what I have so far. Can anyone help I am not sure how to finish. def binarySearch(searchList, numberSought,

I'm trying to output some data from a python script to a binary file. If I have a list of floats I've been using this out_file = open(filename, wb) float_array.array(f, []) float_array.fromlist

Im very new html, javascript, and css so please forgive if my question sounds idiotic to you. My question is how can I prevent the function toFixed() from rounding of the decimal number. Here's my lin

This is my recursive function : public bool controllaSelezioneSottopagina(KPage k_oPaginaAttuale, KPage k_oPaginaSuperiore) { foreach (KPage k_oSottoPagina in k_oPaginaSuperiore.SottoPagine) { if (k_o

This question already has an answer here: Converting Decimal to Binary Java 11 answers I am trying to figure out how I would take all the odd numbers in a file and convert them to binary 1 and

I was wondering if there an established convention to specifying fixed point binary numbers in decimal format (with the use of a macro). I am not sure if this possible in C/C++, but perhaps this is im

I'm going to write System.Data.Linq.Binary value to MemoryStream and perform some manipulations, then re-write new values from MemoryStream to Binary! how to do?

I have a question on data conversion from binary to decimal. Suppose I have a binary pattern like this: pattern<-do.call(expand.grid, replicate(5, 0:1, simplify=FALSE)) pattern Var1 Var2 Var3 Var4

There is a clear way to convert binary recursion to tail recursion for sets closed under a function, i.e. integers with addition for the Fibonacci sequence: (Using Haskell) fib :: Int -> Int fib n

I want to convert decimal numbers to binary numbers. I want to store them in an array. First I need to create an array that has a certain length so that I can store the binary numbers. After that I pe

How can i print the numbers of a float/double variable after the decimal point? For example: 435.5644 the output will be 5644 Thanks,

I have an assignment to write a program that converts decimal numbers to binary numbers, in the C programming language. This is the code I wrote: #include <stdio.h> #define MAX_LEN 1000 void tra

What's the simple way to remove more than 1 decimal number from source number . for example source numbers are : 1st source number is : 56.48216585224 2nd source number is: 93 Output must be : 1st

Hey, I have been asked to write a recursive binary search for my data structure class in university, but i'm having a slight problem. When i search for a number that is out of bounds (over 10 in this

I have a 12bit binary that I need to convert to decimal. E.g. A = [0,1,1,0,0,0,0,0,1,1,0,0]; Bit 1 is the MSB, Bit 12 is the LSB . How do I do it?

I have recursive function which walk nested dict and return needed key's value: def recurs(l): for key in l.keys(): if key == '_events': return l[key] else: recurs(l[key]) c=recurs(d) print c And how

I am using the python shell to figure out how the print command works in python. When I type in print 01 1 print 010 8 print 0100 64 print 030 24 What's going on here? Is it just base 2? Why d

I am developing a function in Javascript using FileSystemObject, where I just have to write in a file the binary data we are provided. This is my function. function exportFile(data) { var fso, f2; fso

How can I convert a binary string, such as 1001101 to Decimal? (77)

Function fun(n) is defined as such: fun(n) = 1 (if n <=1) fun(n) = fun(n/2) (if n is even) fun(n) = 2*fun((n-1)/3) (if n> and n is odd) I'm trying to write a recursive function to compute and r

i'm in the process of trying to write a function that converts an amount into a specified number of coins. For this I'm calling a function on itself (I think this is recursive programming?). I'm havin

I have written some peace of code to implement a function, which uses binary search to check, if a given value is inside an array or not. I used a recursive function which calls itself with new inputs

How can I make a function that calculates the factorial (or the gamma function) of decimal numbers in JavaScript? For example, how could I calculate 2.33!?

How do you write a function that has this form: A(key, B(key, C(key, ValFactory(key)))) Where A, B, and C have this signature: TResult GetOrAdd(string key, Func<string, TResult> generator); An

I'm trying to implement the Huffman's encoding algorithm in c++. my question is : after i got the equivalent binary string for each character , how can i write those zeros and ones as binary on a file

I have to do this exercise and am completely stumped as to how. I've constructed some FA's before, but using binary numbers. How can I do this but for even decimal numbers?