function calls, help

Below are my two function calls the first is correct, I have not found any errors with it, simply posting it for reference. However I am running into issues with the second which is converting decimal to binary. I am just looking for guidance for another direction to go because I cannot seem to get it to work.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
 void find_binary(int num, char str[])                   //calculates binary format
{
int remainder;
int tab = 0;
//char table[];

while (num!=0) //same calculations 2nd time but adding every 1 or 0 (remainder) to separate element in string.

    {
        remainder = num % 2; //getting a remainder
        num = num / 2; //dividing by 2
        str[tab] = remainder + '0'; //adding 0 or 1 to an element
        tab++; //tab (element count) increases by 1 so next remainder is saved in another element
    }
                tab--;

        cout << "The decimal number " << numtemp <<  " has the binary format ";
while (tab>=0) //until we get to the 0 (1st) element of the table

    {
        cout << str[tab]; //write the value of an element (0 or 1)
        tab--; //decreasing by 1 so the 0's and 1's are shown FROM THE BACK
    }
        cout << endl;


}

int find_int(char str[]) // calculates decimal
{

int d;
char ptr[];
int decimal = 0;
int length;

length = ((strlen(ptr)) - 1);
cout << length;

for (int i = length ; i >= 0 ; i--)
  {
        d = ((pow(2,i)* ptr[i]));
        decimal =  decimal + d;
return decimal;
}
Last edited on
What appears to be wrong with your calculations? What input are you providing to this function, and what is the output of the function.

Line 33: What do you think ptr[] contains when you take the strlen of it at line 37? Hint: ptr is an uninitialized array. Did you intend str[]?
I believe my calculations are correct I am just getting an error on the output, also in regards to the ptr[]. I thought i could define it this was because the character array would self define itself. Do i just need to do ptr[50]? and if so why? just to initialize it because as I said I thought leaving it blank would allow for any input to be valid.

Also I have done this calculation before I am just being forced to do it using a char str[] and I am just confused by it because it can easily be done through alternate means.
This is the entire thing I have wrote thus far

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include <iostream>
#include <iomanip>
#include <cmath>
#include <cstring>
using namespace std;


void find_binary(int num, char str[]); //Function: calculates the binary format for the number it receives through its parameter num and stores the result in its parameter str.
int find_int(char str[]);     //Function: calculates the decimal number for its parameter str and returns the result.
int numtemp;

int main(void)
{
char str[50];
int num;        //"numtemp" is to get the origional number the user inputted back.
int number;
int decimal;  //In order to convert binary to decimal.

                cout << "Available choices:\n";
                cout << "1. Display a decimal integer in binary format\n";
                cout << "2. Convert from binary format to a decimal integer\n";
                cout << "3. Quit\n";
                cout << "Enter the number of your choice:\n";
                cin >> number;

if (number == 1) //first choice
{
                cout << "Enter an integer: ";
                cin >> num;
                numtemp = num;
                find_binary(num, str);          //function call

}

if (number == 2) //second choice
{
                cout << "Enter a binary number (as 0's and 1's): ";
                cin >> str;

                decimal = find_int(str);        //function call

                cout <<  "The binary number " << str <<  "has the decimal value " << decimal << ".";
}
if (number == 3)
return 0;
}




void find_binary(int num, char str[])                   //calculates binary format
{
int remainder;
int tab = 0;
//char table[50];

while (num!=0) //same calculations 2nd time but adding every 1 or 0 (remainder) to separate element in string.

    {
        remainder = num % 2; //getting a remainder
        num = num / 2; //dividing by 2
        str[tab] = remainder + '0'; //adding 0 or 1 to an element
        tab++; //tab (element count) increases by 1 so next remainder is saved in another element
    }
                tab--;

        cout << "The decimal number " << numtemp <<  " has the binary format ";
while (tab>=0) //until we get to the 0 (1st) element of the table

    {
        cout << str[tab]; //write the value of an element (0 or 1)
        tab--; //decreasing by 1 so the 0's and 1's are shown FROM THE BACK
    }
        cout << endl;


}

int find_int(char str[]) // calculates decimal
{

int d;
char ptr[50];
int decimal = 0;
int length;

length = ((strlen(ptr)) - 1);
cout << length;

for (int i = length ; i >= 0 ; i--)
  {
        d = ((pow(2,i)* ptr[i]));
        decimal =  decimal + d;
return decimal;
Last edited on
I believe my calculations are correct I am just getting an error on the output


So what is this error on output?

What exactly are you passing into this function?

With that input what exactly do you expect the function to output?

I repeat my question. What do you think ptr[] contains when line 87 is executed? This is an uninitialized array.

I believe what you want is:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int find_int(char str[]) // calculates decimal
{ int d;
//  char ptr[50];  // removed
  int decimal = 0;
  int length;

  length = ((strlen(str)) - 1);  // Note reference to str, not ptr
  cout << length;
  for (int i = length-1; i >= 0 ; i--)  //  Subtracted 1 from length.  Arrays are from 0 to len-1
  { d = ((pow(2,i)* str[i]-'0'));  // Changed from ptr to str
                                               // Note the subtraction of '0' to convert from ASCII to binary                                  
    decimal =  decimal + d;
  }
  return decimal;
}
ptr won't define itself. The string you're interested in is the parameter str.

Note that if str is "1001" then str[0] is NOT 1, it is the character '1'. Take a look at find_binary() and see how you converted 0 and 1 to '0' and '1'. You need to reverse this process in find_int().

The algorithm for find_int() can be simplified. You start at the left side of the string. For each digit, multiply the result by 2 and add the digit. For example, to convert 10011 from binary to decimal:
Digit Result
1 1
0 2*1+0 = 2
0 2*2+0 = 4
1 2*4+1 = 9
1 2*9+1 = 19

If you do it this way then you can avoid the expensive call to pow()
thanks for the all the input I ended up revising it and figured it out, below is my final draft

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int find_int(char str[]) // calculates decimal
{

int index; //index of the char array starting from right side of the binary number	
double d=0; //power of 2 on each iteration
int digit=0; //one digit of the whole binary number
int decimal = 0; //result
int length; // length of the binary number
length=strlen(str); // length of the binary number
index=length-1; //index of the binary number, starting from right most
for(int i=0;i<length;i++){ //iterate for number of digits in binary number
	digit=str[index]; // single digit
	if(digit=='1'){ //calculate only if the digit is 1 (0 won't make any effect)
		decimal = decimal + (pow (2, d)); //keep on adding the calculated result
	}
	d++; //increment power of 2
	index--; //decrement the index
}
Last edited on
Topic archived. No new replies allowed.