Question

In C++ please:

Lab question 1: Provide a high-level algorithm in pseudocode to convert binary to signed decimal below. Apply your algorithm

0 0
Add a comment Improve this question Transcribed image text
Answer #1

CODE

#include<iostream>

using namespace std;
  
// This function adds two binary strings and return
// result as a third string
string addBinary(string a, string b)
{
string result = ""; // Initialize result
int s = 0; // Initialize digit sum
  
// Traverse both strings starting from last
// characters
int i = a.size() - 1, j = b.size() - 1;
while (i >= 0 || j >= 0 || s == 1)
{
// Comput sum of last digits and carry
s += ((i >= 0)? a[i] - '0': 0);
s += ((j >= 0)? b[j] - '0': 0);
  
// If current digit sum is 1 or 3, add 1 to result
result = char(s % 2 + '0') + result;
  
// Compute carry
s /= 2;
  
// Move to next digits
i--; j--;
}
return result;
}


// Function to convert binary to decimal
int binaryToDecimal(string n)
{
string num = n;
int dec_value = 0;
  
// Initializing base value to 1, i.e 2^0
int base = 1;
  
int len = num.length();
for (int i = len - 1; i >= 0; i--) {
if (num[i] == '1')
dec_value += base;
base = base * 2;
}
  
return dec_value;
}

// Function to flip the binary number bits
string flipBinary(string a) {
   string res = "";
   for(int i=0;i<a.length();i++)
       if(a[i] == '1')
           res += '0';
       else
           res += '1';
  
   return res;
}

int main() {
   string binary;
   int decimal;
  
   cout << "Please enter a binary value --> ";
   cin >> binary;
  
   //if the leftmost number is 1, then
   if(binary[0] == '1') {
       //flip the binary bits
       string flipedBinary = flipBinary(binary);
       //add 1 to the result
       string result = addBinary(flipedBinary, "1");
       //convert to decimal
       decimal = binaryToDecimal(result);
       //print the negation of decimal
       cout << "Binary " << binary << " is converted to decimal -" << decimal << endl;
   }
   else {
       //if the leftmost number is 1, then just convert it to decimal
       cout << "Binary " << binary << " is converted to decimal " << binaryToDecimal(binary) << endl;
   }
}

OUTPUT

Please enter a binary value --> 11110011 Binary 11110011 is converted to decimal -13

Please enter a binary value --> 00011101 Binary 00011101 is converted to decimal 29

Add a comment
Know the answer?
Add Answer to:
In C++ please: Lab question 1: Provide a high-level algorithm in pseudocode to convert binary to...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 1. Implement an algorithm to convert binary number into an integer. Binary number is represented as...

    1. Implement an algorithm to convert binary number into an integer. Binary number is represented as a string. Ex. int n = to_int("01010"); int to_int(const std::string& b) { } 2. Implement an algorithm to convert a decimal number to binary. The return type is string which holds the binary number as string. std::string to_binary(int n) { } 3. Implement a function to check if the number is positive or negative. The function should return true if number is positive. bool...

  • Please answer it by easy JAVA pseudocode. Thanks Write a pseudocode algorithm that calculates and prints...

    Please answer it by easy JAVA pseudocode. Thanks Write a pseudocode algorithm that calculates and prints the overall MPG (miles per gallon) for a series of miles and gallons user inputs. Use a loop to repeatedly ask the user to enter a miles value and a gallons value. Within the loop, accumulate miles and gallons separately. Use a counter loop to ensure that the loop repeats three times. The algorithm should generate a display of prompts, inputs, and final output...

  • 5.14 LAB: Convert to binary Write a program that takes in a positive integer as input,...

    5.14 LAB: Convert to binary Write a program that takes in a positive integer as input, and outputs a string of 1's and 0's representing the integer in binary. For an integer x, the algorithm is: As long as x is greater than 0 Output x modulo 2 (remainder is either 0 or 1) Assign x with x divided by 2 Note: The above algorithm outputs the 0's and 1's in reverse order. Ex: If the input is: 6 the...

  • Question 9 (1 point) in The number - 9 10 is to be stored in an...

    Question 9 (1 point) in The number - 9 10 is to be stored in an 8-bit register as a signed binary number in 2's- complement form. Which of the following represents the register contents? 11111010 00110101 00011101 11110111 Question 10 (1 point) Saved Convert 3725 (decimal) to a hexadecimal number 1E2 O EFI

  • Problem 1: regarding Binary numbers, create an algorithm (flowchart ) that reads a 4-bit binary number...

    Problem 1: regarding Binary numbers, create an algorithm (flowchart ) that reads a 4-bit binary number from the keyboard as a string and then converts it into a decimal number. For example, if the input is 1100, the output should be 12. (Hint: Break the string into substrings and then convert each substring to a value for a single bit. If the bits are b0, b1, b2, and b3, the decimal equivalent is 8b0+ 4b1+ 2b2+ b3.) Problem 2: Suppose...

  • Here is the code I made, but the test case is not working, it goes wrong...

    Here is the code I made, but the test case is not working, it goes wrong when the binary string convert to decimal. please help. #include "stdafx.h" #include <iostream> #include <string> #include <math.h> #include <locale> using namespace std; // function for option 1 void decToBin(int number) {        int array[16];        int i = 0;        for (int counter = 0; counter < 16; counter++)        {               array[counter] = 0;        }        while (number > 0)        {...

  • C++ preffered please, thank you. Write a code in a high-level language (C++, C# or Java...

    C++ preffered please, thank you. Write a code in a high-level language (C++, C# or Java or ??) for a Deterministic Pushdown automaton which will accept the string a^nb^n where sigma = a, b. Create an input file with the following strings aaabbb, ababab, aababb, abbaab and give your output showing what is accepted and what is not accepted.

  • QUESTION 1 Convert 1 0110 1010 from binary to decimal, assuming nine-bit two's complement binary representation....

    QUESTION 1 Convert 1 0110 1010 from binary to decimal, assuming nine-bit two's complement binary representation. Provide only the number. For a negative number provide the negative sign. Example 79 NOT +79 or 78 Decimal Example -79 NOT negative 79 or -79 Decimal QUESTION 2 Count the next 10 numbers in base 5 starting after 3434. (Don't include 3434) Provide you answer with only numbers and spaces. No extra spaces or words Example  410 411 412   NOT 410, 411, 412

  • Please help solving these following questions: Convert 18210 into binary (or into hex). Convert 3276810 into...

    Please help solving these following questions: Convert 18210 into binary (or into hex). Convert 3276810 into binary (or into hex). Convert E7A216 into binary or decimal. Convert 11100101102 into decimal (or into hex). • Understand or code a function that uses default parameters. • Describe or use a group of overloaded functions.• Write a function to find the average of a 1-dim array of floats. • Write a function to multiply two 1-dim arrays together to produce a third array...

  • ********** PLEASE PROVIDE IN JAVA & NEW SET OF ALGORITHM For this lab use the program...

    ********** PLEASE PROVIDE IN JAVA & NEW SET OF ALGORITHM For this lab use the program at the bottom to sort an array using selection sort. Write a selection sort to report out the sorted low values: lowest to highest. Write another to report out the sorted high values – highest to lowest. Last but not least, the program should output all the values in the array AND then output the 2 requested sorted outputs. -----> MY OLD PROGRAM BELOW...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT