Question

Description Develop a program (in C++) that first asks the user to input a signed number, for example 48, then converts it to binary number by using Twos Complement regulations. Do the same for the second inputted number, eg. 17. Then compute the summation of these two numbers, output the results in binary format first, then if there is an overflow, display error message; otherwise, convert the result to decimal value and show it on the screen, e.g. 48+(-17)-3. Note that there is no limit to the number of bits your program can handle. For convenience, your program can assume it is for eight bit binary numbers. All invalid inputs should be detected by your program. Your program should be user-friendly, i.e., the user can choose when to end your program Please show that it works and show the output.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Answer for your question is:

#include<iostream>

using namespace std;

void display(int arr[]){

for(int x=31;x>=0;x--){

cout<<arr[x];

}

cout<<"\n";

}

void binary(int n , int arr[]){

if(n<0)n*=-1;

int x=0;

while(n){

arr[x++]=n%2;

n/=2;

}

}

void twocomp(int arr[]){

int x=0;

while(arr[x]!=1){

x++;

}

x++;

for(;x<32;x++){

if(arr[x]==0)arr[x]=1;

else if(arr[x]==1)arr[x]=0;

}

}

void Add(int num1[],int num2[],int sum[]){

int carry=0;

int s;

for(int x=0;x<32;x++){

s=num1[x]+num2[x]+carry;

sum[x]=s%2;

carry = s/2;

}

}

int main(){

int num1,num2;

cout<<"Enter First Number : ";

cin>>num1;

cout<<"Enter Second number : ";

cin>>num2;

int n1[32],n2[32],sum[32];

for(int x=0;x<32;x++){

n1[x]=0;

n2[x]=0;

sum[x]=0;

}

binary(num1,n1);

binary(num2,n2);

if(num1<0)twocomp(n1);

if(num2<0)twocomp(n2);

Add(n1,n2,sum);

if(n1[31]==0 && n2[31]==0 &&sum[31]==1)cout<<"\nOverflow\n";

else if(n1[31]==1 && n2[31]==1 &&sum[31]==0)cout<<"\nOverflow\n";

else {

cout<<"\nn1 : ";

display(n1);

cout<<"\n\n";

cout<<"\nn2 : ";

display(n2);

cout<<"\n\n";

cout<<"\nSum : ";

display(sum);

cout<<"\n";

cout<<"\n\n"<<num1<<" + "<<num2<<" = "<<num1+num2<<"\n";

}

return 0;

}

sample output of C++ program is:

Add a comment
Know the answer?
Add Answer to:
Please show that it works and show the output. Description Develop a program (in C++) that...
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
  • Make a program in C that takes in four parameters, 1) string of +,- or *...

    Make a program in C that takes in four parameters, 1) string of +,- or * for addition,subtraction, or multiplication 2) 32 bit two's complement integers where a 'b' in front of it means binary, 'o' means octal 'd' means decimal, and 'x' means hexadecimal 3) another 32 bit two's complement number with the same base as the first number, so if first number was a binary the second would also be a binary 4) the type of base you...

  • Please use python to write the simple program. Exercise 2 - Summation Write a program to...

    Please use python to write the simple program. Exercise 2 - Summation Write a program to accept integer inputs until the user inputs a non-integer. Then, the program prints the summation of the inputted numbers. Hint: Use the function input() to get the user input. Use a variable total for calculating the summation. Need to use while loop for the repeated inputs. Use if statement with isdigit() function to check whether the user input is an integer or not. if...

  • (3 pts) Consider an unsigned fixed point decimal (Base10) representation with 8 digits, 5 to the...

    (3 pts) Consider an unsigned fixed point decimal (Base10) representation with 8 digits, 5 to the left of the decimal point and 3 to the right. a.      What is the range of the expressible numbers?    b.      What is the precision?    c.       What is the error?    ______________________________________________________________________________   (3 pts) Convert this unsigned base 2 number, 1001 10112, to each base given below   (Note: the space in the binary string is purely for visual convenience) Show your work. Using...

  • NOTE: Write the Program in python as mentioned below and use while loop and flags as...

    NOTE: Write the Program in python as mentioned below and use while loop and flags as necessary. And please write the code following the program description given below. Directions: Read the program description below carefully. All requirements must be met by your program to receive full credit. Thus, pay particular attention to input and output requirements, structural requirements, and so on. Furthermore, code that contains syntax errors will not receive any credit, so be sure that your code interprets correctly...

  • Assignment Develop a program to analyze one or more numbers entered by a user. The user...

    Assignment Develop a program to analyze one or more numbers entered by a user. The user may enter one or more numbers for analysis. Input should be limited to numbers from 1 through 1000. Determine if a number is a prime number or not. A prime number is one whose only exact divisors are 1 and the number itself (such as 2, 3, 5, 7, etc.). For non-prime numbers, output a list of all divisors of the number. Format your...

  • NOTE: Write the Program in python as mentioned below and use while loop and flags as...

    NOTE: Write the Program in python as mentioned below and use while loop and flags as necessary. And please write the code following the program description given below. DON'T use Function concept or any other concept except while loop,if-else. Directions: Read the program description below carefully. All requirements must be met by your program to receive full credit. Thus, pay particular attention to input and output requirements, structural requirements, and so on. Furthermore, code that contains syntax errors will not...

  • PRG255 3.2 (2 marks) Write a C program that uses the bitwise shift operators to shut...

    PRG255 3.2 (2 marks) Write a C program that uses the bitwise shift operators to shut the The program will ask the user to enter an unsigned also how many bits for the shift operation. Display the entered operation in both decimal and binary formats, vise shirt operators to shift the bits to the right >> or the left << user to enter an unsigned integer number, choose right shift or left shift, and orauon. Display the entered number and...

  • and then print their average. Write a C program that accepts a string of text from...

    and then print their average. Write a C program that accepts a string of text from the user and prints back the string without any of the vowels. For example, if the user entered "Hello, world", the program should print "Hll, wrld" Write a C program that accepts an integer in decimal from the user and prints the number of 1' bits in the integers binary representation. For example, if the user entered 14, the program should print 3 because...

  • For each part, do or answer the following: - Show how you would add the two...

    For each part, do or answer the following: - Show how you would add the two 8-bit binary numbers by filling in each of the blank boxes with a 0 or 1. NOTE1: Addition of 2's complement numbers is done just like the addition of unsigned whole numbers, so you only need to show each addition once. NOTE2: There are 9 blank boxes for the Carry bits and 8 blank boxes for the Sum bits. CAUTION: Every blank box should...

  • You must write a C program that prompts the user for two numbers (no command line...

    You must write a C program that prompts the user for two numbers (no command line input) and multiplies them together using “a la russe” multiplication. The program must display your banner logo as part of a prompt to the user. The valid range of values is 0 to 6000. You may assume that the user will always enter numerical decimal format values. Your program should check this numerical range (including checking for negative numbers) and reprompt the user for...

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