Why is my code not working for base 2 numbers that are greater than 1,000,000? Please answer in C++ programming language

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899603
Joined: Mon Aug 02, 2021 8:13 am

Why is my code not working for base 2 numbers that are greater than 1,000,000? Please answer in C++ programming language

Post by answerhappygod »

Why is my code not working for base 2 numbers that are greaterthan 1,000,000? Please answer in C++ programming language...
Why Is My Code Not Working For Base 2 Numbers That Are Greater Than 1 000 000 Please Answer In C Programming Language 1
Why Is My Code Not Working For Base 2 Numbers That Are Greater Than 1 000 000 Please Answer In C Programming Language 1 (75.18 KiB) Viewed 37 times
5- int main() { 6 7 8 9 10 11 12 13 14 15 } 16 std::cout << Base10toBase2 (100000) << std::endl; // should be 11000011010100000 // output: 0b11000011010100000 // ERROR HERE std::cout << Base10toBase2 (4358929) << std::endl; // should be 10000101000001100010001 //output: 0b1965712050523034129 return 0; base 10-to-base2 PASSED (+1) PASSED (+1) ✔ PASSED (+1) ✓ PASSED (+1) ✔ PASSED (+1) ✔ PASSED (+1) ✔ PASSED (+1) X FAILED (+0) PASSED (+1) driver.cc converter.cc converter.hpp Less than 10 #1 Less than 10 #2 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 3
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. Do consider as a positive input, though! 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 "øb1000". NOTE: your binary encoding should not have any leading zeros, with the exception of O. For example, Base10toBase2 (3) should yield only "ob11" ("0b011", "0b0011", 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 unsigned long long number = 0; 6 unsigned long long power of = 1; 7- while (base10_value != 0) { 8 unsigned long long value_remainder = base10_value % 2; number number + (value_remainder * power_of); 9 10 11 12 13 14 15 } 16 } base10_value = base10_value / 2; power of *= 10; std::string str = "0b" + std::to_string(number); return str; base10-to-base2 driver.cc converter.cc converter.hpp CS 128 A+ Editor
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply