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 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 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 convert2(int & n, const string & bits);

int main(){

    int n;

    cout<<"returns: "<<convert2(n, "10011")<<endl;

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

    return 0;

}

bool convert2(int & n, const string &bits){

    int n_bits = 5;

    int i=0;

    int result = 0;

    for(int i=1; i<n_bits; i++){

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

            return false;

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

            result += pow(2, n_bits-i-1);

    }

    result += 1;

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

        result = -(result);

    n = result;

    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 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,...
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...
I'm trying to work with this code that I have. i want to kick the user...
I'm trying to work with this code that I have. i want to kick the user back if they don't enter a number in the specified range but I haven't been able to get a while loop working right. Write a program name Blackjack_Jr that allows a human user to play a single hand of "blackjack" against a dealer. Pick two values from 1-10 for the player. These are the player's "cards". These two values must be user inputs Generate...
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--;...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT