In This Activity You Will Implement A Function That Will Convert A Positive Base 10 Value To Its Corresponding Base 2 V 1 (35.29 KiB) Viewed 70 times
In This Activity You Will Implement A Function That Will Convert A Positive Base 10 Value To Its Corresponding Base 2 V 2 (39.9 KiB) Viewed 70 times
In this activity, you will implement a function that will convert a positive base 10 value to its corresponding base 2 value. We will use the straight binary encoding scheme introduced in the Integer representation video; you need not worry about negative numbers. The function you will implement, Base10toBase2, will receive a base-10 (i.e., decimal) value as an unsigned integer, convert that value to a base-2 (i.e., binary) value encoded as an std::string, and return that std::string to the caller. Ensure that your string begins with "ob" directly, followed by the binary encoding of the decimal value. For example, invoking Base10toBase2 (8) would return "eb1000". NOTE: your binary encoding should not have any leading zeros, with the exception of O. For example, Base10toBase2 (3) should yield only "eb11" ("ebe11", "ebee11", and any other representations will be marked wrong).
1 #include "converter.hpp" 2 3- std::string Base10toBase2 (unsigned int base10_value) { 4 // Your code here 5 int number 0; = 6 int power of = 1; 7- while (base10_value != 0) { 8 int value remainder = base10_value % 2; 9 number = number + (value_remainder power_of); base10_value base10_value / 2; power of = 10; 10 11 12 13 14 15 } 16 } std::string str="0b" + std::to_string(number); return str: base10-to-base2 PASSED (+1) PASSED (+1) PASSED (+1) PASSED (+1) PASSED (+1) X FAILED (+0) X FAILED (+0) driver.cc converter.cc converter.hpp Less than Less than 10 #3 Less than 10 #4 Less than 10 #5 Less than 100 #1 Less than 100 #2 Greater than 100,000 #1 Your code doesn't handle the conversion for large numbers! Greater than 100,000 #2 CS 128 A+ Editor Run Grade -1 D
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!