/* ******************************************** Program: lab12tp.cpp Tonya M. Payne COSC 1435 :: Section 3 Version: 1.0 Date: Original 14 April 2010 Purpose: This lab is designed to provide experience working with converting decimal numbers to binary and binary numbers to decimal numbers, using functions, loops, and arrays. Input: Keyboard Output: To screen */////////////////////////////////////////////// #include //needed for compiler #include //needed for reading in files #include //for formatting using namespace std; //declare global variables const int SIZE = 8; //declare function prototypes void convDecToBin(int,bool[]); int convBinToDec(bool bArray[], int); void printArray(bool bTemp[], int, int); int iPow(int iBase, int iPower); int main() { int iDecValue = 0, //decimal variable iBinValue = 0, //binary variable iBase, //base variable for power function iPower; //power variable for power function char cAnswer; //variable for 'y' or 'Y' string test do { //these need to be reset each time the loop runs, //so they are placed inside the loop bool bArray[SIZE] = {0,0,0,0,0,0,0,0}, //the original array bTemp[SIZE] = {0,0,0,0,0,0,0,0}; //the temp array //takes in a decimal number form the user cout << "Please input an integer value from 0 - 255: "; cin >> iDecValue; while( iDecValue < 0 || iDecValue > 255 ) { cout << "That is not a valid number. Please input a single"; cout << " number from 0 - 255: "; cin >> iDecValue; } //calls the convert decimal to binary function convDecToBin(iDecValue, bArray); //make a temporary array for the numbers to be in the right order for(int i = 0, j = ( SIZE - 1); i < SIZE; i++, j--) { bTemp[j] = bArray[i]; } // calls the print function printArray(bTemp, SIZE, iDecValue); convBinToDec(bArray, SIZE); //break out of program cout << "Would you like to enter another value? Y or N: " << endl; cin >> cAnswer; }while( cAnswer == 'y' || cAnswer == 'Y' ); return 0; //end of program } //***************************************************************************** // function for converting decimal to binary // // variables in the function: iRemainder is the remainder given by mod // i is a counter // bArray[] is a boolean array // iDecValue is the number entered by the user // //***************************************************************************** void convDecToBin(int iDecValue, bool bArray[]) { int iRemainder, i = 0; while ( iDecValue > 0 ) { iRemainder = iDecValue % 2; bArray[i] = iRemainder; iDecValue /= 2; i++; } } //***************************************************************************** // function for converting binary to decimal // // variables in the function: iBinaryValue : stores the solution as the // function calculates the sum // iTempValue : a temp variable // iCount : a counter variable // m : array counter // //***************************************************************************** int convBinToDec(bool bArray[], int SIZE) { int iBinaryValue = 0, iTempValue = 0, iCount = 0; for (int m = SIZE - 1; m >= 0; m--) { iTempValue = bArray[m] * iPow(2,iCount); iBinaryValue += iTempValue; iCount++; } return iBinaryValue; } //***************************************************************************** // function for printing results // // variables in the function: bTemp[] : temporary array // SIZE : size of the array // iDecValue : user entered number // k : counter // //***************************************************************************** void printArray(bool bTemp[], int SIZE, int iDecValue) { cout << "The number you entered, " << iDecValue; cout << " in binary is: " << endl; for ( int k = 0; k < SIZE; k++ ) { cout << setw(4) << ": " << bTemp[k] << endl; } cout << "...and that number converted back into decimal is: " ; cout << convBinToDec(bTemp, SIZE) << endl; } //***************************************************************************** // Integer power function // // variables in the function: SIZE : of the array // tempVar : temporary variable // holdPow : holds the product of the value // tempVar : holds the value of the power // //***************************************************************************** int iPow(int iBase, int iPower) { int iTemp = 1; for (int i=0; i < iPower; i++) { iTemp = iTemp * iBase; } return iTemp; } //notes from the lab //for(int i = 0, j = SIZE; i < SIZE; i++, j--) //bArray[i] = bTemp[j] //bool bArray[SIZE] //write an integer power function // 0 * 2^0 // 1 * 2^1 // 0 * 2^2 // 0 * 2^3 // 1 * 2^4