Question

In: Computer Science

For this week’s lab assignment, you will write a program called lab9.c. You will write a...

For this week’s lab assignment, you will write a program called lab9.c. You will write a program so that it contains two functions, one for each conversion. The program will work the same way and will produce the same exact output. The two prototypes should be the following: int btod(int size, char inputBin[size]); int dtob(int inputDec); The algorithm for the main() function should be the following: 1. Declare needed variables

2. Prompt user to enter a binary number

3. Use scanf() to get that value

4. If getting it as a string, use strlen() to find the length of the string

5. Call btod() function sending to it the size and the value that was entered by the user and save the return value so the result can be printed out

6. Prompt user to enter a decimal number

7. Use scanf() to get that value

8. Call dtob() function saving the return value in a variable so the result can be printed out

Don’t forget to break the program into smaller steps and compile and run AFTER EACH STEP.

Solutions

Expert Solution

Hi

I have written the program for binary to decimal and decimal to binary and its working fine, please compiler and test at your end, if you have any questions please let me know.

// readFileCalavg.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <string.h>
#include <stdlib.h>
#include <iostream>

int btod(int size, char inputBin[64]) {
   int decimalOutput = 0, i = 0, powerMul;
   powerMul = size - 1;
   while (i<size) {
       decimalOutput = decimalOutput + (pow(2, powerMul) * (inputBin[i] - 48));
       i++;
       powerMul--;
   }
   return decimalOutput;
}

long dtob(int inputDec) {

   long binarynum = 0, rem, place = 1;

   while (inputDec > 0)
   {
       rem = inputDec % 2;
       binarynum += rem * place;
       place *= 10;
       inputDec /= 2;
   }

   return binarynum;
}

int main()
{

   char binaryInput[64];
   int decimalInput = 0;
   printf("Please enter the binary number\n");
   scanf("%s", binaryInput);
   printf( " The decimal number is : %d \n",btod(strlen(binaryInput),binaryInput));
   //-----------------------------
   printf("Please enter the decimal number\n");
   scanf("%d", &decimalInput);
   printf(" The binary number is : %ld \n", dtob(decimalInput));
   getchar();
   getchar();
   return 0;
}

Thanks


Related Solutions

For this week’s assignment, you will write a program class that has two subroutines and a...
For this week’s assignment, you will write a program class that has two subroutines and a main routine. The program should be a part of the ‘Firstsubroutines’ class and you should name your project Firstsubroutines if you are using Netbeans. Your program must prompt the user to enter a string. The program must then test the string entered by the user to determine whether it is a palindrome. A palindrome is a string that reads the same backwards and forwards,...
For this lab, you will write a C++ program that will calculate the matrix inverse of...
For this lab, you will write a C++ program that will calculate the matrix inverse of a matrix no bigger than 10x10. I will guarantee that the matrix will be invertible and that you will not have a divide by 0 problem. For this program, you are required to use the modified Gaussian elimination algorithm. Your program should ask for the size (number of rows only) of a matrix. It will then read the matrix, calculate the inverse, and print...
For this lab, you will write a C++ program that will calculate the matrix inverse of...
For this lab, you will write a C++ program that will calculate the matrix inverse of a matrix no bigger than 10x10. I will guarantee that the matrix will be invertible and that you will not have a divide by 0 problem. For this program, you are required to use the modified Gaussian elimination algorithm. Your program should ask for the size (number of rows only) of a matrix. It will then read the matrix, calculate the inverse, and print...
For this lab, you will write a C++ program that will calculate the matrix inverse of...
For this lab, you will write a C++ program that will calculate the matrix inverse of a matrix no bigger than 10x10. I will guarantee that the matrix will be invertible and that you will not have a divide by 0 problem. For this program, you are required to use the modified Gaussian elimination algorithm. Your program should ask for the size (number of rows only) of a matrix. It will then read the matrix, calculate the inverse, and print...
C++ Assignment 1) Write a C++ program specified below: a) Include a class called Movie. Include...
C++ Assignment 1) Write a C++ program specified below: a) Include a class called Movie. Include the following attributes with appropriate types: i. Title ii. Director iii. Release Year iv. Rating (“G”, “PG”, “PG-13”, etc) - Write code that instantiates a movie object using value semantics as text: - Write code that instantiates a movie object using reference semantics: - Write the print_movie method code: - Write Constructor code: - Write Entire Movie class declaration
In C++ For this assignment, you will write a program to count the number of times...
In C++ For this assignment, you will write a program to count the number of times the words in an input text file occur. The WordCount Structure Define a C++ struct called WordCount that contains the following data members: An array of 31 characters named word An integer named count Functions Write the following functions: int main(int argc, char* argv[]) This function should declare an array of 200 WordCount objects and an integer numWords to track the number of array...
For this computer assignment, you are to write a C++ program to implement a class for...
For this computer assignment, you are to write a C++ program to implement a class for binary trees. To deal with variety of data types, implement this class as a template. Most of the public member functions of the BinaryTree class call private member functions of the class (with the same name). These private member functions can be implemented as either recursive or non-recursive, but clearly, recursive versions of these functions are preferable because of their short and simple implementations...
For this computer assignment, you are to write a C++ program to implement a class for...
For this computer assignment, you are to write a C++ program to implement a class for binary trees. To deal with variety of data types, implement this class as a template. Most of the public member functions of the BinaryTree class call private member functions of the class (with the same name). These private member functions can be implemented as either recursive or non-recursive, but clearly, recursive versions of these functions are preferable because of their short and simple implementations...
C++ Funcion For this lab you need to write a program that will read in two...
C++ Funcion For this lab you need to write a program that will read in two values from a user and output the greatest common divisor (using Euclidean algorithm) to a file. You will also need to demonstrate using ostream and ostringstream by creating 2 functions to output your print heading: one that uses ostream and the other uses ostringstream. Using ostream and ostringstream Write two PrintHeader functions that will allow you to output to the screen and to an...
C PROGRAMMING – Steganography In this assignment, you will write an C program that includes processing...
C PROGRAMMING – Steganography In this assignment, you will write an C program that includes processing input, using control structures, and bitwise operations. The input for your program will be a text file containing a large amount of English. Your program must extract the “secret message” from the input file. The message is hidden inside the file using the following scheme. The message is hidden in binary notation, as a sequence of 0’s and 1’s. Each block of 8-bits is...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT