4 bit Binary Addition in C++ -


we given assignment make program takes 2 decimal numbers input , performs binary addition in background outputs decimal sum. seem getting wrong results despite how think code logic correct.

my current sum gives me 0. correct answer should 24.

#include <iostream> #include <sstream> #include <string>   using namespace std; int add(int, int); int decbin(int); int bindec(long); int binadd(string, string);  int main() {     int = 15;     int b = 9;     int z;     z = add(a,b);     cout << z;     return 0; }   int add(int a, int b) {     int z;     ostringstream x, y;     x << decbin(a);     y << decbin(b);     z = binadd(x.str(), y.str());     return z; }  int decbin(int x) {     int d[16];     int = 0;     int j;     int ans;       while(x > 0)     {         d[i] = x % 2;         i++;         x = x / 2;     }      for(j = - 1; j >= 0; j--)     {        ans = d[j];      }       return ans; }  int bindec(int x) {     int bin, dec = 0, rem, base = 1;      bin = x;     while (x > 0)     {        rem = x % 10;        dec = dec + rem * base;        base = base * 2;        x = x / 10;     }      return dec; }  int binadd(string a, string b) {    int carry = 0;    int result[5];    int res;    int ans;     for(int = 0; < 4; i++)   {      if(a[i] == '1' && b[i] == '1' && carry == 0)     {         result[i] = 0;         carry = 1;     }     else if(a[i] == '0' && b[i] == '1' && carry == 1)     {         result[i] = 0;         carry = 1;     }     else if(a[i] == '1' && b[i] == '1' && carry == 1)     {         result[i] = 1;         carry = 1;     }     else if(a[i] == '1' && b[i] == '0' && carry == 1)     {         result[i] = 0;         carry = 1;     }     else if(a[i] == '1' && b[i] == '0' && carry == 0)     {         result[i] = 1;         carry = 0;     }     else if(a[i] == '0' && b[i] == '0' && carry == 1)     {         result[i] = 1;         carry = 0;     }     else if(a[i] == '0' && b[i] == '1' && carry == 0)     {         result[i] = 1;         carry = 0;     }     else if(a[i] == '0' && b[i] == '0' && carry == 0)     {         result[i] = 0;         carry = 0;     }  }  result[4] = carry; for(int j = 4; j >= 0; j--) {    res = result[j]; }  ans = bindec(res); return ans;  } 


Comments

Popular posts from this blog

python - mat is not a numerical tuple : openCV error -

c# - MSAA finds controls UI Automation doesn't -

wordpress - .htaccess: RewriteRule: bad flag delimiters -