Question

What you need to turn in: You will need to include an electronic copy of your report (standalone) and source code (zipped) of

Objectives of this assignment: • To dust off your programming skills in the high-level language you learned in CPSC 1213 • T

write codes and give detailed explanation of how to execute.

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

Question 1

#include<iostream>

using namespace std;
int main()
{
char c;
cin>>c;
int r, m, sum = 0, b = 1;
if(c=='0'||c=='1')
{m=c-48;

while (m > 0)
{
r = m % 10;
sum = sum + r * b;
b *= 2;
m /= 10;
}
cout<<sum;}

else
cout<<"The character "<<c<<" is invalid: "<<c<<" is not a bit.";
}

F:\arraysum\main.exe The character x is invalid: x is not a bit. Process returned @ (@xe) execution time : 2.405 S Press any

explaination----

First of all a character is taken as input in c . then some initialization is done which i'll be coming through in between . if condition is executed so to check if the entered character is bit or not and if yes then the conversion of it into its decimal equivalent will be done .

now since we stored an integer value in a character variable , so it's ascii value will be stored in it not the original one. So to take the original value for conversion we subtracted 48 from it to get the digit. and then stored in a variable named m.

Now the .conversion starts . a loop is executed till m>0. we are taking modulus of m on dividing it by 10 so to take its digits at 10th place . and on everytime dividing m by 10 we will have the next digit at 10th place so we will be able to get all the digits with us. now what are we doing with those digits .?

As we know for conversion from binary to decimal we multiply the digits of binary with 2 raised to the power (0,1,2,3,4,.....)

with the power for the LSB being 0(2 raised to the power 0) then increasing the power by 1 subsequently from right to left.

Here r stored the digits , b is having the powers of 2 which is increased in the 3rd line of the loop . and in the second line we are having all the calculations stored in the sum variable which at last will be having the final value conversion into decimal. The fourth line is dividing m by 10 so each of its digit will come at 10th place eventually.

Then we printed the value converted.

This all comes in the if case , and by the way if user has input other character than 0 or 1 it will print the message invalid. which is shown in the above image.

#as per HomeworkLib guidelines i am supposed to answer only first question so plz post them separately.

Add a comment
Know the answer?
Add Answer to:
write codes and give detailed explanation of how to execute. What you need to turn in:...
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
  • write programs with detailed instructions on how to execute. code is java What you need to...

    write programs with detailed instructions on how to execute. code is java What you need to turn in: You will need to include an electronic copy of your report (standalone) and source code (zipped) of your programs. All programming files (source code) must be put in a zipped folder named "labl-name," where "name" is your last name. Submit the zipped folder on the assignment page in Canvas; submit the report separately (not inside the zipped folder) as a Microsoft Word...

  • 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...

  • 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...

  • Write a program that implements an elementary bit stream cipher. An elementary level bit stream cipher...

    Write a program that implements an elementary bit stream cipher. An elementary level bit stream cipher is an encryption algorithm that encrypts 1 byte of plain text at a time. This one uses a given 4-bit bit pattern as the key. The size of the encrypted message that we want to be able to send has a maximum length of 200 characters. You must: 1. prompt the user to input the clear text to be encrypted. You must use printf()...

  • write a c++ code be sure the user only enter A or a for packageA, B...

    write a c++ code be sure the user only enter A or a for packageA, B or b for packageB, C or c for packageC. If the user inputs some other value display an error message saying " invalid out". When you ask the user to input the number of minute and the user enters a negative value you should display the same error message "invalid input" in both cases, after you display the error message the program should stop...

  • Write a program that prompts the user to enter a person's birth date in numeric form...

    Write a program that prompts the user to enter a person's birth date in numeric form (e.g. 8-27-1980) and outputs the date of birth in the format of month day, year (e.g. August 27, 1980). Your program must work for three types of exceptions - invalid day, invalid month and invalid year (year must be between 1915 and 2015). Make these as class driven exceptions. Use one try-catch block with separate catches for the exception classes to handle errors for...

  • Case Problem 11 - 1: Modify the GreenvilleRevenue program created in the previous chapter so that...

    Case Problem 11 - 1: Modify the GreenvilleRevenue program created in the previous chapter so that it performs the following tasks: The program prompts the user for the number of contestants in this year’s competition; the number must be between 0 and 30. Use exception-handling techniques to ensure a valid value and display the error message: Number must be between 0 and 30 The program prompts the user for talent codes. Use exception-handling techniques to ensure a valid code and...

  • print fruits Python 3 1 fruit = in 8. Print Fruits 3 # Enter you The...

    print fruits Python 3 1 fruit = in 8. Print Fruits 3 # Enter you The user is asked to input a single character representing a kind of fish as indicated in the table below. It may be upper or lower case. You should print the message indicated for the entry in the table. For example, if the user enters "b" you should print the message The fruit is a banana If the user enters "p you should print the...

  • In this program, you will be using C++ programming constructs, such as overloaded functions. Write a...

    In this program, you will be using C++ programming constructs, such as overloaded functions. Write a program that requests 3 integers from the user and displays the largest one entered. Your program will then request 3 characters from the user and display the largest character entered. If the user enters a non-alphabetic character, your program will display an error message. You must fill in the body of main() to prompt the user for input, and call the overloaded function showBiggest()...

  • C Programming QUESTION 9 Write a program that prompts for and reads four integer input values,...

    C Programming QUESTION 9 Write a program that prompts for and reads four integer input values, then a single character command. Submit your program as a .c file named midterm_prog2.c. Note that, similarly to your programming assignments, some percentage of your grade will depend on proper programming style. Your program will calculate and print a new value based on the command that's entered, as follows: 'A' or 'a': Calculate the average of the four input values 'S' or 's': Calculate...

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