/* Due date for this lab: Saturday 11/5 */ /* Please email me your code using the following command */ /* mail -s "CISC2100 lab2" xzhang@fordham.edu < hash.cpp */ #include #include #include #include using namespace std; /* Calculate the digits of the given num in the given base. * precondition: num>=0 * postcondition: return an array of digits, digit[0] is the zero-th digit (rightmost), * digit[1] is the second to the right... * digitsLen stores the length of the array */ /* Todo by you: Modify this function so that it can convert/calculate * the digits of num in other base */ int * ConvertToBase2 (unsigned int num, int & digitsLen) { //Step 1: Decide how many binary digits are needed... (Using logarithm). // use command "man" to find out how to use math functions (log, log2...) // e.g., man log2 digitsLen = (int)(log2(num))+1; // Step 2: allocate int array dynamically of the given length int * digits = new int[digitsLen]; // Step 3: for (int i=0;i> input; //Test your functions above digits = ConvertToBase2 (input, len); cout <<"The binary representation is: "; for (int i=len-1;i>=0;i--) cout <> sel; } while (sel=='y' || sel=='Y'); cout <<"now convert other base to base 10 \n"; // base 2 or 8 to base 10 do { cout <<"Enter a number, and then its base"; cin >> inputString >> base; //Call ConvertStringToInt function here... cout <<"\nCont(y/n)?"; cin >> sel; } while (sel=='y' || sel=='Y'); }