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

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....
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...
Do only in C# (Not in C++ or C ) Minimum number of operations to convert...
Do only in C# (Not in C++ or C ) Minimum number of operations to convert array M to array N by adding an integer into a subarray Given two arrays M and N , the task is to find the minimum number of operations in which the array M can be converted into array N where each operation consists of adding an integer K into a subarray from L to R. M= {3, 7, 1, 4, 1, 2}, N...
Trying to make sure I'm answering these questions correctly. I'm confused on question d. I can...
Trying to make sure I'm answering these questions correctly. I'm confused on question d. I can explain why in terms of concentration gradient and the fact it's in a hypotonice solution which would cause water to go into the blood vessel, but don't know how to answer it in terms of hydrostatic pressure. 6. Imagine blood in a vessel that has an osmolarity of 300 mEq (a measure of the concentration of particles). Now, imagine that the IF around the...
Convert the following hexadecimal representations of 16-bit 2’s complement binary numbers to decimal. a.FCAD b.DEAD c.1111...
Convert the following hexadecimal representations of 16-bit 2’s complement binary numbers to decimal. a.FCAD b.DEAD c.1111 d.8000 e.FACE
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT