1 answer

Write codes and give detailed explanation of how to execute. What you need to turn in:...

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.

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 or PDF file. O You should adhere to the following guidelines: . State whether your code works. . Clearly explain how to compile and execute your code. . If needed/applicable, report/analyze (as appropriate) the results. The quality of analysis and writing is critical to your grade. Good writing and presentation are expected.
Objectives of this assignment: • To "dust off your programming skills in the high-level language you learned in CPSC 1213 • To stress the fact that the variables ultimately are Os and is • To practice conversion from a base (decimal, hexadecimal, or binary) to another base • To distinguish between "numbers" and characters What you need to do: Programming Exercise ! (12 points): Write a program that prompts the user to enter a character that represents a binary digit a bit!). (Recall that c can be only "0" or "1.") Your program must use the character type for the input. If the user enters a character "x" that is not a bit, you must print out the following error message: "The character x is invalid: x is not a bit. If the character is a bit, your main program must print out its value in decimal Example 1: If the user enters the character "0." your program must print out the value 0. Example 2: If the user enters the character "I." your program must print out the value I. Example 3: If the user enters the character "B." your program must print out the following error message: "The character B is invalid: B is not a bit." Programming Exercise 2 (12 points): Write a program that prompts the user to enter a character c that represents a hexadecimal digit (recall that can be "0."",""2,"..... "8,""9," "A" "B." "C." "D" "E" or "F"). Your program must use the character type for the input. If the user enters a character "X" that is not a hexadecimal digit, you must print out the following error message: "The character x is invalid: x is not a hexadecimal digit." If the character is a hexadecimal digit, your main program must print out its value in decimal. Example 1: If the user enters the character 4, your program must print out the value 4. Example 2: If the user enters the character "B." your program must print out the value II. Example 3: If the user enters the character "E." your program must print out the value 14. Example 4: If the user enters the character "1" your program must print out the following error message: "The character I is invalid: I is not a hexadecimal digic." Programming Exercise 3 (16 points): Write a program that prompts the user to enter a positive integer n ( up to 2-1). You must write a function that takes as input n and returns a string s representing the number nin binary. For this assignment, you must use the method of successive division by 2 to convert the number to binary. Your main program must print out s. Example: If the user enters the number 66, your program must print out 1000010

Answers

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.

.

Similar Solved Questions

1 answer
Trundle Inc. makes two styles of trophies, basic and deluxe, and operates at capacity. Trundle does...
Trundle Inc. makes two styles of trophies, basic and deluxe, and operates at capacity. Trundle does large custom orders. Trundle budgets to produce 10,000 basic trophies and 5,000 deluxe trophies. Manufacturing takes place in two production departments: forming and assembly. In the forming departmen...
1 answer
18. A student performed the same experiment you did in the lab to find the chemical...
18. A student performed the same experiment you did in the lab to find the chemical formula of a copper chloride hydrate with the general formula Cu,Cl,.2H20. Water molecules evaporated when the solid was heated until it turned brown then the sample was dissolved in water to react with a coil of alu...
1 answer
A firm has a current ratio of 0.9. Given this you know for certain that the...
A firm has a current ratio of 0.9. Given this you know for certain that the firm: A. Has negative net working capital B. Has more cash than inventory C. Has more current liabilities than it does current assets D. Has positive net working capital E. Pays cash for its inventory...
1 answer
In order for an electron to be emitted, the energy of the incoming photon must be...
In order for an electron to be emitted, the energy of the incoming photon must be greater than the work function of the metal. A work function tells us the minimum energy an electron needs to gain in order to escape the material in which it is embedded. A gold cathode plate has a work function of 5....
1 answer
3. In the figure below the force F = 15 N. Take m1 = 2.0 kg,...
3. In the figure below the force F = 15 N. Take m1 = 2.0 kg, m2 = 5.0 kg, and 0 = 30°. Assume ideal rope and frictionless surface. Find the tension in the rope and the acceleration of the blocks. F m2 mi 0...
1 answer
=E- 3 1 Q1: Consider the complex vectors: ū = 21, ý = 1 - 2...
=E- 3 1 Q1: Consider the complex vectors: ū = 21, ý = 1 - 2 -5 a) Evaluate <ü, lv > where 1 = 2 - i. b) Find the distance between ū and . c) Decide whether vectors ū and v are orthonormal. d) Describe the span of the vectors ū and v....
1 answer
(4) (9 pts) Evaluate S SSF. ds, where F = ri+yj + 2zk and S is...
(4) (9 pts) Evaluate S SSF. ds, where F = ri+yj + 2zk and S is the portion of the surface z=1-22 – y above the ry-plane, with upward orientation....
1 answer
QUESTION 49 Your bank card has an APR of 21% and there is a 3% fee...
QUESTION 49 Your bank card has an APR of 21% and there is a 3% fee for cash advances. The bank starts charging interest on cash advances immediately. You get a cash advance of $2,000 on the first day of the month. You get your credit card bill at the end of the month. What is the approximate total f...
1 answer
An experiment has 6 different outcomes. The probabilities of the first five outcomes are 3/20, 7/20,...
An experiment has 6 different outcomes. The probabilities of the first five outcomes are 3/20, 7/20, 1/10, 1/25 and 4/25. What is the probability of the last outcome?...
1 answer
How can I draw the structures of all the geometric isomers of #[Ru(H_2O)_2(NH_3)_2Cl_2]^+# . What are the mirror images of any of these chiral molecules?
How can I draw the structures of all the geometric isomers of #[Ru(H_2O)_2(NH_3)_2Cl_2]^+# . What are the mirror images of any of these chiral molecules?...
1 answer
Since monetary policy changes through the fed funds rate occur with a lag, policymakers are usually...
Since monetary policy changes through the fed funds rate occur with a lag, policymakers are usually more concerned with adjusting policy according to changes in the forecasted or expected inflation rate, rather than the current inflation rate. In light of this, suppose that monetary policymakers emp...
1 answer
Following previous question, suppose that instead of funding the $200 million investment in 10 pe...
Following previous question, suppose that instead of funding the $200 million investment in 10 percent German loans with U.S. CDs, the FI manager funds the German loans with $200 million equivalent one-year euro CDs at a rate of 7 percent Now the balance sheet of the FI would be as follows Assets U....
1 answer
Your investment advisor believes that recent stock returns should be given more consideration when calculating future...
Your investment advisor believes that recent stock returns should be given more consideration when calculating future returns and risk than older returns. The historical returns for Lately Lighthouse, Inc. are given in the table below, along with the weights that your advisor believes are appropriat...
1 answer
Haynes, Inc., obtained 100 percent of Turner Company’s common stock on January 1, 2017, by issuing...
Haynes, Inc., obtained 100 percent of Turner Company’s common stock on January 1, 2017, by issuing 8,300 shares of $10 par value common stock. Haynes’s shares had a $15 per share fair value. On that date, Turner reported a net book value of $83,850. However, its equipment (with a five-ye...
1 answer
Maths & Mechanics l. Resolving Vectors Find the force acting along the femur and the force...
Maths & Mechanics l. Resolving Vectors Find the force acting along the femur and the force pushing the femur into the hip joint (acetabulum) Hip abductors and adductors o a 800 N 800 Ne 800 N 1500 N 1500 N 1500 N 40 degre 40 degre...