Question

In: Computer Science

I'm trying to convert between different number representations in C++ , I have the prototype but...

I'm trying to convert between different number representations in C++ , I have the prototype but im not sure what do do from here

bool convertU(unsigned & n, const string & bits);

bits should have size exactly 5, and each char of bits should be '0' or '1'; otherwise

return false.

If bits is ok, set n to the number that bits represents as an unsigned and return true.

For example, convertU(n, "0101") and convertU(n, "10210") should return false;

convertU(n, "10011") should set n to 19 and return true.

Solutions

Expert Solution

If you have any doubts, please give me comment...

#include<iostream>

#include<cmath>

#include<string>

using namespace std;

bool convertU(unsigned & n, const string & bits);

int main(){

    unsigned n;

    if(convertU(n, "0101"))

        cout<<"n = "<<n<<endl;

    if(convertU(n, "10210"))

        cout<<"n = "<<n<<endl;

    if(convertU(n, "10011"))

        cout<<"n = "<<n<<endl;

    return 0;

}

bool convertU(unsigned & n, const string & bits){

    n = 0;

    int len = bits.size();

    for(int i=0; i<bits.size(); i++){

        if(bits[i]!='0' && bits[i]!='1')

            return false;

        if(bits[i]=='1')

            n += pow(2, len-i-1);

    }

    return true;

}


Related Solutions

I'm trying to convert between different number representations in C++ , I have the prototype but...
I'm trying to convert between different number representations in C++ , I have the prototype but im not sure what do do from here bool convert2(int & n, const string & bits); bits should have size exactly 5, and each char of bits should be '0' or '1'; otherwise return false. If bits is ok, set n to the number that bits represents as a 2's complement number and return true. For example, convertU(n, "10011") should set n to -13...
I'm trying to convert between different number representations in C++ , I have the prototype but...
I'm trying to convert between different number representations in C++ , I have the prototype but im not sure what do do from here bool convertF(double & x, const string & bits); bits is supposed to be an 8-bit (not 5-bit) value, each char being '0' or '1'; if not, return false. The first bit of bits represents the sign bit, the next 3 bits represent the exponent, and the next 4 bits represent the significand. convertF may assume without...
The source code I have is what i'm trying to fix for the assignment at the...
The source code I have is what i'm trying to fix for the assignment at the bottom. Source Code: #include <iostream> #include <cstdlib> #include <ctime> #include <iomanip> using namespace std; const int NUM_ROWS = 10; const int NUM_COLS = 10; // Setting values in a 10 by 10 array of random integers (1 - 100) // Pre: twoDArray has been declared with row and column size of NUM_COLS // Must have constant integer NUM_COLS declared // rowSize must be less...
I'm trying to use Jupyter (python) to convert the contents of a pkl file into a...
I'm trying to use Jupyter (python) to convert the contents of a pkl file into a dictionary, WITHOUT using pandas. I'm able to import pickle and can open my file...but I'm not sure how to create the dictionary.
I'm trying to make this C++ loan calculator but I can't get the program to output...
I'm trying to make this C++ loan calculator but I can't get the program to output what I need. For instance I know the formula is right and when I enter 100000 1.5 20 as my cin variables I should get 482.55 but I get 1543.31. What am I not seeing as the issue? Also this should cout only 2 decimal places right? I'm not allowed to alter the #include and add any fixed setprecision. This is what I have....
I'm trying to solve a problem where I have an object resting on an inclined plane,...
I'm trying to solve a problem where I have an object resting on an inclined plane, with the angle of the plan being alpha, and the weight being w. I'm having trouble figuring out how I can calculate the component of the weight parallel to the plane. I also want to find out the weight component perpendicular to the plane. I don't want an outright answer, more of an explanation to help me understand. Thanks!
Java I'm trying to create a program that replicates a theater ticket reservation system. I have...
Java I'm trying to create a program that replicates a theater ticket reservation system. I have a Seat class with members row(integer), seat(character) and ticketType(character). Where a ticket type can be 'A' adult, 'C' child, 'S' senior, or '.' recorded as empty. I have a Node generic class that points to other nodes(members): up, Down. Left, Right. It also has a generic payload. Lastly, I have an Auditorium class which is also generic. its member is a First Node<T>. As...
I have a decimal fraction: 76.234567x10^-14 I need to convert this number to binary. I know...
I have a decimal fraction: 76.234567x10^-14 I need to convert this number to binary. I know you can multiply the number by 2 constantly to get the binary number, but that will take forever to do, even converting it to hex with x16 takes a long time, is there any easier way to convert this? Show all steps and work please.
C++ program please, can you show me where I did wrong. I'm trying to print the...
C++ program please, can you show me where I did wrong. I'm trying to print the counter-clockwise spiral form using int *p, but my output turned out weird, thank you void makeSpiral(int *p, int rows, int cols) { int left = 0, value = 1, top = 0; while(left < cols && top < rows) { for(int i = top;i < rows;++i) { *(p+i*cols+left) = value++; } left++; for(int i = left;i < cols;++i) { *(p+(rows-1)*cols+i) = value++; } rows--;...
I am trying to solve a c++ problem over c strings where I have to input...
I am trying to solve a c++ problem over c strings where I have to input an email address and check if it is valid. After completing my code I keep getting errors that my program will not run, specifically the lines with my for loops. Can you please look at my code and tell me what is wrong and how I can fix the code? I included below the assignment instructions and my current code. Assignment Instructions Write a...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT