Given a (decimal – e.g. 3.72) number that is passed in as a string, print the binary representation.If the number can not be represented accurately in binary, print “ERROR”.
string print_binary(string val) { int pos = val.find('.',0); int intpart = atoi(val.substr(0,pos).c_str()); double doublepart = atof(val.substr(pos+1).c_str()); string intstr = ""; string doublestr = ""; while(inpart>0){ if(intpart&1) intstr = "1"+intstr; else intstr = "0"+intstr; intpart>>=1; } while(double>0){ if(doublestr.lenght()>32) return "ERROR"; doublepart* = 2; if(doublepart>=1){ doublestr+="1"; doublepart-=1; } else{ doublestr +="0"; } } return intstr+"."+doublestr; }