Function output into int array
I am working on a program that reads in two decimal numbers from the command line, converts them to binary, adds them, then outputs the sum of the binary and decimal.
I made a function that converts the decimal input into binary, but now I can't figure out how to get those values into an int array.
For Example: Input: ./a.out 3 2
my function converts 3 into 11 and 2 into 10
now I need to put those values at the end of a int array so it looks like this: 0000000000000000000000000000011 and 0000000000000000000000000000010
That way my logic for adding the binary numbers can work properly.
Here is my attempt but it is saying it can assign int from void:
#include <iostream>
#include <cstdlib>
#include <string.h>
using namespace std;
void binary(int);
int main(int argc, char* argv[])
{
if(argc != 3)
{
cerr << "Invalid number of operands" << endl;
return 1;
}
int i;
int arg1 = atoi(argv[1]);
int arg2 = atoi(argv[2]);
int sum = arg1 + arg2;
int a[32];
int b[32];
int c[32];
int carry = 0;
bool print = false;
for(i = 0; i < 32; i++)
{
a[i] = 0;
b[i] = 0;
c[i] = 0;
}
for(i = 31; i >= 0; i--)
{
a[i] = binary(arg1); //PROBLEM AREA
b[i] = binary(arg2); //PROBLEM AREA
}
for(i = 31; i >= 0; i--)
{
if (a[i] == 1 && b[i] == 1 && carry == 0)
{
c[i] = 0;
carry = 1;
}
else if (a[i] == 1 && b[i] == 0 && carry == 0)
{
c[i] = 1;
carry = 0;
}
else if (a[i] == 0 && b[i] == 0 && carry == 0)
{
c[i] = 0;
carry = 0;
}
else if (a[i] == 0 && b[i] == 1 && carry == 0)
{
c[i] = 1;
carry = 0;
}
else if (a[i] == 1 && b[i] == 1 && carry == 1)
{
c[i] = 1;
carry = 1;
}
else if (a[i] == 1 && b[i] == 0 && carry == 1)
{
c[i] = 0;
carry = 1;
}
else if (a[i] == 0 && b[i] == 0 && carry == 1)
{
c[i] = 1;
carry = 0;
}
else if (a[i] == 0 && b[i] == 1 && carry == 1)
{
c[i] = 0;
carry = 1;
}
}
if(carry == '1')
cout << carry;
for (i = 0; i < 32; i++)
{
if (c[i] == 1)
print = true;
if (print)
cout << c[i];
}
cout << " = " << sum;
co开发者_如何转开发ut << endl;
return 0;
}
void binary(int number)
{
int remainder;
if(number <= 1)
{
cout << number;
return;
}
remainder = number % 2;
binary(number >> 1);
cout << remainder;
}
Any ideas or suggestions would be appreciated!
a[i] = binary(arg1); //PROBLEM AREA
The method binary()
is a void method. You should return an int
value from it to be able to assign it to a[i].
Since the input is decimal, why do you add two binary represented decimals? It would be simpler to just show each number as a binary number but do the addition using decimal values then just show the result in binary representation.
EDIT: ok didnt see your homework tag at first.
your binary() function could return a vector of int, create the vector in your binary function and return it. e.g.
vector<int> binary(int n);
alternatively create an array in your main program, pass the array to the binary function and then print the contents of the binary after it returns. e.g.
void binary( int n, int maxSizeArray, int* binArray );
Here:
cout << remainder;
you are printing the remainder, instead of returning it:
return remainder;
And don't forget to change void binary(int number)
to int binary(int number)
.
Just use bit operations
#include <iostream>
#include <climits>
template <typename T>
char *to_binary_string(const T &n, char *out) {
char *p = out;
if (out) {
for (int i = sizeof(T) * CHAR_BIT - 1; i >= 0; --i)
*p++ = ((n & (1 << i)) >> i) + '0';
*p = 0;
}
return out;
}
int main() {
char buf[33] = "";
int test[] = { 0xF, 0x2, 0x3, 0xFF, 0x15 };
for (int i = 0; i < sizeof(test)/sizeof(*test); ++i) {
std::cout << to_binary_string<int>(test[i], buf) << std::endl;
}
return 0;
}
... or instead of that mumbo-jumbo with limited functionality of bit operators you can use inline-assembly function (which is more efficient, I believe; no optimization, but it's 100% clear this way)
void binary(long int var_in, char* var_out)
{
__asm
{
MOV ESI, var_in
MOV EDI, var_out
ADD EDI, 0x1F
XOR EBX, EBX
DEC EBX
NOT EBX
@loop1_start:
NOT EBX
INC EBX
CMP EBX, 0x1F
JG @loop1_end
BT ESI, EBX
JC @loop1_set1
MOV AL, 0x30
JMP @loop1_set0
@loop1_set1:
MOV AL, 0x31
@loop1_set0:
NOT EBX
MOV BYTE PTR DS:[EDI+EBX], AL
JMP @loop1_start
@loop1_end:
MOV BYTE PTR DS:[EDI+1], 0x00
}
}
Where var_in is a 32 bit integer and var_out is a char array of at least 33 elements (32 bytes + '\0').
P.S.: Your 'binary' is recursive. As everybody knows using recursive functions instead of the ones with iteration (if it is possible) produces much slower results.
The reason is that you are writing the binary to output, rather than in an array. What you should do is, send the array to your function and have it fill it.
For example, let's say the function looks like this:
void to_binary(int number, int *array, int position_to_fill);
Then what you can do is, exactly like you wrote, but instead of cout
, you write to the array in the proper location:
void to_binary(int number, int *array, int position_to_fill)
{
int remainder;
if (position_to_fill < 0) // shouldn't really happen
return;
if (number <= 1)
{
array[position_to_fill] = number;
return;
}
remainder = number % 2;
binary(number >> 1, array, position_to_fill-1);
array[position_to_fill] = number;
}
This way, you fill the array from last position to the first with the last position containing the least significant bit.
Now, when you call the functions, instead of
for(i = 31; i >= 0; i--)
{
a[i] = binary(arg1); //PROBLEM AREA
b[i] = binary(arg2); //PROBLEM AREA
}
you write
to_binary(arg1, a, 31);
to_binary(arg2, b, 31);
精彩评论