1 answer

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

Question:

Part 1- HLL You can use any high-level language you wish for this part (C+t or Java is recommended), but you cannot call built-in library functions to accomplish these tasks automatically. Write a 16-bit binary conversion program that can either convert a 16-bit unsigned decimal to binary or convert a string of 16-bit binary to an unsigned decimal. Use loop and a menu to allow user to make repeated selection as needed. You can assume user will always enter valid input data. You must use a function/method for each conversion: A function/method that receives an unsigned integer (limit to 16 bits) and returns a 16-bit string containing the binary representation of that integer A function/method that receives a string containing a 16-bit unsigned integer and returns an unsigned integer value of that binary value Run the following test cases and confirm that results are correct Unsigned integers 36 and 65535 Binary strings 0000000000111011 and 1111001000000000 Sample input and output: Binary conversion program by [Your Name] This program can handle 16-bit unsigned values Select an option below to perform conversion 1unsigned decimal to binary 2binary to unsigned decimal 3 -quit Enter an option --> 1<Enter> Please enter a decimal value-36<Enter> Decimal 36 is converted to binary 0000000000100100 Enter an option> 2<Enter> Please enter a binary value0000000000111011 <Enter> Binary 000000000011101 is converted to decimal 59 Enter an option --> 3<Enter> Thanks for using my program

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)

       {

              array[i] = number % 2;

              number = number / 2;

              i++;

       }

       for (int counter = 15; counter >= 0; counter--)

       {

              cout << array[counter];

       }

}

// function for option 2

void binToDec(int number)

{

       int array[16];

       double dec = 0;

       int i = 0;

       for (int counter = 0; counter < 16; counter++)

       {

              array[counter] = 0;

       }

       while (number != 0)

       {

              array[i] = number % 10;

              number = number / 10;

              i++;

       }

       for (int counter = 0; counter < 16; counter++)

       {

              if (array[counter] != 0)

              {

                     dec += pow(2, counter);

              }

       }

       cout << "The result is " << dec << endl;

}

// negative number

void negat(int number)

{

       long array[16];

       double dec = 0.0;

       int length = 16;

       int j = 15;

       int a = 0;

       //store the number in the array check

       for (int counter = 15; counter >= 0; counter--)

       {

                     array[counter] = number % 10;

                     number = number / 10;

       }

       // flip the numbers check

       for (int counter = 15; counter >= 0; counter--)

       {

              if (array[counter] == 0)

              {

                     array[counter] = 1;

              }

              else {

                     array[counter] = 0;

              }

       }

       // add 1

       if (array[length] == 1)

       {

              while (array[length] == 1)

              {

                     array[length] = 0;

                     length++;

              }

              array[length + 1]++;

       }

       else

       {

              array[15]++;

       }

       //reverse array

       int temp, i;

       for (i = 0; i < 16 / 2; ++i) {

              temp = array[16 - i - 1];

              array[16 - i - 1] = array[i];

              array[i] = temp;

       }

       /// to dec

       for (int counter = 0; counter < 16; counter++)

       {

              if (array[counter] != 0)

              {

                     dec += pow(2, counter);

              }

       }

       dec = 0 - dec;

       cout << "The result is " << dec << endl;

}

void ShowMenu()

{

       cout << "Welcome to a 16-bit binary conversion program" << endl;

       cout << "Binary conversion program by [Qi Liu]\nPlease choice one out of the three options and input the option number :)" << endl;

       cout << "1. Convert a 16 bit unsigned decimal to binary \n2. Convert a string of 16 - bit binary to an unsigned decimal\n3. Exit" << endl;

}

/**********************************************************/

int main()

{

       int number;

       string ans;

       int choice;

       bool Quit = false;

       char move = 'z';

       while (!Quit) {

              ShowMenu();

              cout << "Enter an option: ";

              cin >> move;

              switch (move) {

              case '1':

                     int number;

                     string ans;

                     cout << "Please enter a number between 0 - 65515. \n";

                     cin >> number;

                           decToBin(number);

                     break;

              case '2':

                     int number;

                     string ans;

                                  cout << "Please enter signed binary \n";

                                  cin >> number;

                           if (number / 1000000000000000 == 1) {

                                  negat(number);

                           }

                           else {

                                  binToDec(number);

                           }

                     break;

              case '3': //user wants to quit

                     Quit = true; //execution continues after the switch

                                         //or do this to end program

                                         //return 0;

                     break;

              default:

                     cout << "Bad Input, Try again " << std::endl;

                     break;

              }

       }

      

       cout << "Thank you for using our program" << endl;

       system("pause");

       return 0;

}

Part 1- HLL You can use any high-level language you wish for this part (C+t or Java is recommended), but you cannot call built-in library functions to accomplish these tasks automatically. Write a 16-bit binary conversion program that can either convert a 16-bit unsigned decimal to binary or convert a string of 16-bit binary to an unsigned decimal. Use loop and a menu to allow user to make repeated selection as needed. You can assume user will always enter valid input data. You must use a function/method for each conversion: A function/method that receives an unsigned integer (limit to 16 bits) and returns a 16-bit string containing the binary representation of that integer A function/method that receives a string containing a 16-bit unsigned integer and returns an unsigned integer value of that binary value Run the following test cases and confirm that results are correct Unsigned integers 36 and 65535 Binary strings 0000000000111011 and 1111001000000000 Sample input and output: Binary conversion program by [Your Name] This program can handle 16-bit unsigned values Select an option below to perform conversion 1unsigned decimal to binary 2binary to unsigned decimal 3 -quit Enter an option --> 1<Enter> Please enter a decimal value-36<Enter> Decimal 36 is converted to binary 0000000000100100 Enter an option> 2<Enter> Please enter a binary value0000000000111011 <Enter> Binary 000000000011101 is converted to decimal 59 Enter an option --> 3<Enter> Thanks for using my program

Answers

No need to initialize the char move.

As you told you are facing problem in the logic of binary to decimal conversion, you can use something like this:

That will help.

    printf("Enter a binary number(1s and 0s) \n"); 
    scanf("%d", &num);  
    binary_val = num; 
    while (num > 0) 
    { 
        rem = num % 10; 
        decimal_val = decimal_val + rem * base;         num = num / 10 ;         base = base * 2; } 
.

Similar Solved Questions

1 answer
A 10-m steel rod is snugly supported by two rigid supports at the end. The coefficient...
A 10-m steel rod is snugly supported by two rigid supports at the end. The coefficient of thermal expansion and contraction of the rod is 6X10^-6 per F and the elastic modulus is 210 GPa. If the temperature of the body is increased by 50 F, the thermal stress developed at the supports is most nearly...
1 answer
A loan of ​$23372337 borrowed today is to be repaid in three equal installments due in...
A loan of ​$23372337 borrowed today is to be repaid in three equal installments due in one-and-a-half years​, three-and-a-half ​years, and six ​years, respectively. What is the size of the equal installments if money is worth 5.3 % compounded semi annually?...
1 answer
An engineering example involves inspecting a subset of 15 steel support beams on a bridge for...
An engineering example involves inspecting a subset of 15 steel support beams on a bridge for signs of enough wear that they need to be replaced. The truth is that in fact 5 beams need to be replaced. Preliminary testing involves selecting 3 of the 15 beams at random, and if any of the 3 tested beam...
1 answer
Hould you sell a bond if market rate is higher than coupon rate? Explain.
hould you sell a bond if market rate is higher than coupon rate? Explain....
1 answer
Senff Corporation uses the following activity rates from its activity-based costing to assign overhead costs to...
Senff Corporation uses the following activity rates from its activity-based costing to assign overhead costs to products: Activity Cost Pools Setting up batches Processing customer orders Assembling products Activity Rate $86.75 per batch $77.49 per customer order $12.41 per assembly hour Data conce...
1 answer
A. For each pair of compounds, tell whether the structural formulas shown represent the same compound,...
a. For each pair of compounds, tell whether the structural formulas shown represent the same compound, different compounds that are constitutional isomers or different compounds that are not constitutional isomers. [4 x 1 mark] (1) and (11) and (111) and (iv) and b. Draw a structural formula for eac...
1 answer
A company borrows $20,000 with 8% interest on October 1, 2018. This amount plus interest is...
A company borrows $20,000 with 8% interest on October 1, 2018. This amount plus interest is due on September 30, 2019. Record the adjusting entry on December 31, 2018 Journal entry worksheet for December 31, 2018 Help please...
1 answer
What is ATP synthase and how does it work to generate ATP
what is ATP synthase and how does it work to generate ATP...
1 answer
The small cylinder of a hydraulic press has an area of 2"(squared)
the small cylinder of a hydraulic press has an area of 2"(squared). If a force of 10# is applied to the small piston, how much is the pressure within the larger cylinder increased, according to pascal's principle....
1 answer
Two red blood cells each have a mass of 6.85×10−14 kg and carry a negative charge...
Two red blood cells each have a mass of 6.85×10−14 kg and carry a negative charge spread uniformly over their surfaces. The repulsion arising from the excess charge prevents the cells from clumping together. Once cell carries −1.60 pC of charge and the other −2.70 pC , and ea...
1 answer
Use the shell method to find the volume of the solid generated by revolving the region...
Use the shell method to find the volume of the solid generated by revolving the region bounded by the given curves and lines about the x-axis- 1 = exe , y=0, x=0, y=1 6 (e-1) 3пе ale-1) 67(e - 1)...
1 answer
The spectral irradiance l(v, T), defined as ? ?(V,T)dv, here v is the frequency. (a) Based...
The spectral irradiance l(v, T), defined as ? ?(V,T)dv, here v is the frequency. (a) Based on the spectral irradiance, calculate the total number of photon per unit area (m2) per unit time (s), this is the photon density of solar radiation. (b) Calculate the total number of photon per unit area (m2)...
1 answer
Determine the end behavior of graph -3(x-5)(3x-1)2(x+6)5 the 2 and 5 is exponent
Determine the end behavior of graph -3(x-5)(3x-1)2(x+6)5 the 2 and 5 is exponent...
1 answer
Suppose that all existing firms in a long-run competitive market equilibrium are identical and have the...
Suppose that all existing firms in a long-run competitive market equilibrium are identical and have the following cost function C(Q)= 1002 with MC(Q)=2Q. Suppose also that market demand is given by P(Q)=A-0.04Q, where A-40.0. What is the equilibrium market quantity? No units, no rounding. Your Answe...
1 answer
5. Which of the following is considered a buffer system: a) HC104 and NaC104 b) HCIO3...
5. Which of the following is considered a buffer system: a) HC104 and NaC104 b) HCIO3 and NaClO3 c) H2CO3 and K2CO3 d) NaHPO4 and Na3PO4 e) NH4Cl and NH3...
1 answer
W my applic X PRACTICE: CHAPTER 9 HW X CengageNOWV2 Online Cengage Learning RakeAssignment/takeAssignment Main.do?invoker=&takeAssignmentSessionLocator=&inprogress-false x...
w my applic X PRACTICE: CHAPTER 9 HW X CengageNOWV2 Online Cengage Learning RakeAssignment/takeAssignment Main.do?invoker=&takeAssignmentSessionLocator=&inprogress-false x + - eBook Calculator Dividends Per Share Lightfoot Inc., a software development firm, has stock outstanding as follows: ...
1 answer
Nuclear magnetic resonance (NMR) spectroscopy is a powerful method, in which transitions occur between different angular...
Nuclear magnetic resonance (NMR) spectroscopy is a powerful method, in which transitions occur between different angular momentum states of an atomic nucleus. Let us consider the case of 23Na, a biologically important element, for which the angular momentum quantum number is S = 3/2. (a) Calculate t...
1 answer
A total of 46% of voters in a certain city classify themselves as Independents; 30% as...
A total of 46% of voters in a certain city classify themselves as Independents; 30% as Liberals and 24% as Conservatives. In a recent election, 35% of the Independents, 62% of the Liberals, and 58% of the conservatives voted. A voter is chosen at random. Given that this person voted, what is the pro...
1 answer
Way Cool produces two different models of air conditioners. The company produces the mechanical systems in...
Way Cool produces two different models of air conditioners. The company produces the mechanical systems in its components department. The mechanical systems are combined with the housing assembly in its finishing department. The activities, costs, and drivers associated with these two manufacturing ...