homework4 - 4.31 z = x + y; cout <<...

Info iconThis preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 4.31 z = x + y; cout << ++z; 4.27 // *********************************************************************** // File: bintodec.cpp // // Binary to Decimal - User inputs an integer as a binary number, // and the program outputs the decimal equivalent. // // Author: Benjamin Jones // Created: 28 January 2008 // Updated: Original // // Assignment: 4.27 // Compiler: MinGW32 G++ 3.4.2 (GNU) // // Inputs: One integer that is in binary // Outputs: Display message, "Binary number: <binary> Decimal number: <decimal> // ********************************************************************* // // Modules: // 1. Get input from user // // 2. Convert binary to decimal // // 3. Display result to user // //*********************************************************************** // // Design: // // 1.a Read input as a binary integer // // 2.a Use binary integer as a counter // 2.a.1 While binary integer is greater than zero // 2.a.2 Check whether right-most digit is a one // 2.a.2.1 If yes, add to decimal number two to the power of // the current iteration (place value) // 2.a.3 Set binary number to itself divided by ten (shift right) // 2.a.4 Increment counter to next place value // 2.b Return value of decimal number // // 3.a Display message, "Binary number: <binary> Decimal number: <decimal> // // ********************************************************************** #include <iostream> #include <math.h> using namespace std; const int OK = 0; // 2. Convert binary to decimal int convertBinToDec (int binaryNumber) { int endDigitTest; int decimalNumber = 0; int counter = 0; // 2.a Use binary integer as a counter while (binaryNumber > 0 ) { // 2.a.2 Check whether right-most digit is a one endDigitTest = binaryNumber % 2; if (endDigitTest == 1) { // 2.a.2.1 If yes, add to decimal number two to the power of // the current iteration (place value) decimalNumber += pow(2,counter); } // 2.a.3 Set binary number to itself divided by ten (shift right) binaryNumber /= 10; // 2.a.4 Increment counter to next place value counter++; } // 2.b Return value of decimal number return decimalNumber; } int main() { int binaryNumber = 0; // 1. Get input from user cout << "Please enter a binary number: "; // 1.a Read input as a binary integer cin >> binaryNumber; cout << endl; // 3. Display result to user // 3.a Display message, "Binary number: <binary> Decimal number: <decimal> cout << "Binary number: " << binaryNumber << endl << "Decimal number: " << convertBinToDec(binaryNumber) << endl << endl; system("PAUSE"); return OK; } 4.35.1 // ***********************************************************************...
View Full Document

Page1 / 15

homework4 - 4.31 z = x + y; cout <<...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online