My code isn't working for negative numbers, computation errors that I can't visibly understand from my code... please an

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

My code isn't working for negative numbers, computation errors that I can't visibly understand from my code... please an

Post by answerhappygod »

My code isn't working for negative numbers, computation errors that I can't visibly understand from my code... please answer in C++ programming language. (COPY AND PASTED CODE UNDER PICTURES!)
Question: In this activity, you will continue implementing the Base10toBase2 function. This function will receive negative, positive, and zero integer values as input, convert that value to a 8-bit base-2 value encoded as an std::string, and return the string to the caller. If your output exceeds the bounds for an 8-bit integer, you should throw an invalid argument error (i.e., std::invalid_argument). When converting a negative base-10 value to binary, encode the binary representation using two's complement.Make sure that your string begins with "0b" directly, followed by the binary encoding of the decimal value (with as many leading zeros as needed to reach 8-bits). For example, invoking Base10toBase2(-8) would return "0b11111000".Hint: What is the range of values for our representation?
My Code Isn T Working For Negative Numbers Computation Errors That I Can T Visibly Understand From My Code Please An 1
My Code Isn T Working For Negative Numbers Computation Errors That I Can T Visibly Understand From My Code Please An 1 (71.19 KiB) Viewed 25 times
CODE FOR converter.hpp (only copy/paste, can't insert image):
#ifndef CONVERTER_HPP#define CONVERTER_HPP
#include <string>#include <stdexcept>
std::string Base10toBase2(int base10_value);
#endif
ENTIRE CODE COPY/PASTE:
#include "converter.hpp"
std::string Base10toBase2(int base10_value) { // if base10 value is out of range for 8-bit conversion, throw an invalid argument error if (base10_value < -255 || base10_value > 255) { throw std::invalid_argument("Value cannot be in an 8-bit integer!"); } // string starting with 0b as required std::string str = "0b"; // if base10 value is equal to 0, set str value if (base10_value == 0) { str += "00000000"; // 8-bit characters for 0 // else if value is greater than 0 } else if (base10_value > 0) { // set string value to hold 2 bit values std::string base2_value = ""; // while value is a base10value while (base10_value) { // convert base10 to base2 base2_value += (base10_value % 2) + '0'; base10_value /= 2; } // swapping base2 values and incrementing/decrementing int i = 0; int j = base2_value.length() - 1; while (i < j) { std::swap(base2_value, base2_value[j]); i++; j--; } // ensuring there are no off leading 0's by subracting 8 by length int no0s = 8 - base2_value.length(); std::string leading0s = ""; for (int i = 0; i < no0s; i++) { leading0s += '0'; } // populate str with leading 0's and base2 values to ensure 8-bit conversion str += leading0s; str += base2_value; // if no conditions above are met } else { // set absolute value of base10 to check neg values base10_value = abs(base10_value); // string holding base2 conversions std::string base2_value = ""; // while value is a base10 value while (base10_value) { // populate base2 base2_value += (base10_value % 2) + '0'; base10_value /= 2; } // swapping base2 values and incrementing/decrementing int i = 0; int j = base2_value.length() - 1; while (i < j) { std::swap(base2_value, base2_value[j]); i++; j--; } // ensuring there are no off leading 0's by subracting 8 by length int no0s = 8 - base2_value.length(); std::string leading0s = ""; for (int i = 0; i < no0s; i++) { leading0s += '0'; } // update base2_value to also include leading 0s base2_value = leading0s + base2_value; // loop backwards through base2 for (int i = base2_value.length() - 1; i >= 0; i--) { if (base2_value == '1') { break; } if (base2_value == -1) { // change - # to return + 8-bit form along with base2 value (with leading0s) return "0b1" + base2_value; } for (int j = i - 1; j >= 0; j--) { if (base2_value[j] == '0') { base2_value[j] = '1'; } else { base2_value[j] = '1'; } } } // populate str with base2 values that are 8-bit str += base2_value; } return str;}
1 #include "converter.hpp" 2 3- std::string Base10toBase2 (int base10_value) { 4 // if base10 value is out of range for 8-bit conversion, throw an invalid argument error if (base10_value < -255 || base10_value > 255) { 5- 6 throw std::invalid_argument ("Value cannot be in an 8-bit integer!"); 7 8 9 10 11 12 13 14- 15 16 17 18- 19 20 } // string starting with ob as required std::string str = "0b"; // if base10 value is equal to 0, set str value if (base10_value == 0) { str += "00000000"; // 8-bit characters for 0 // else if value is greater than 0 } else if (base10_value > 0) { // set string value to hold 2 bit values std::string base2_value = ""; // while value is a base10value while (base10_value) { // convert base10 to base2 hase? value += (hase10 value % 2) + 'A'• base 10tobase2-pt2 driver.cc converter.cc converter.hpp CS 128 A+ Editor
20 21 22 23 24 25 26- 27 28 29 30 31 32 33 34- 35 36 37 38 २१ base2_value += (base10_value % 2) + '0'; base10_value /= 2; } // swapping base2 values and incrementing/decrementing int i = 0; int j = base2_value.length() - 1; while (i<j) { std::swap (base2_value , base2_value [j]); i++; j--; } // ensuring there are no off leading 0's by subracting 8 by length int no0s= 8 base2_value.length(); std::string leading0s = ""; for (int i = 0; i < no0s; i++) { leading0s += '0'; } // populate str with leading 0's and base2 values to ensure 8-bit conversion str += leading0s; ctr + hace? value. base10tobase2-pt2 driver.cc converter.cc converter.hpp CS 128 A+ Editor
39 40 41 42 43 44 45 46 47- 48 49 50 51 52 53 54 55 - 56 57 58 str + base2_value; // if no conditions above are met } else { // set absolute value of base10 to check neg values base10_value = abs(base10_value); // string holding base2 conversions std::string base2_value = ""; // while value is a base10 value while (base10_value) { // populate base2. base2_value += (base10_value % 2) + '0'; base10_value /= 2; } // swapping base2 values and incrementing/decrementing int i = 0; int j = base2_value.length() - 1; while (i < j) { std::swap (base2_value , base2_value [j]); i++; i--- base10tobase2-pt2 driver.cc converter.cc converter.hpp CS 128 A+ Editor
58 59 60 61 62 63- 64 65 66 67 68 0 69 70 71 72 73 71- 74 75 76- 77 j--; } // ensuring there are no off leading 0's by subracting 8 by length int no0s= 8 base2_value.length(); std::string leading0s = ""; for (int i = 0; i < no0s; i++) { leading0s += '0'; } // update base2_value to also include leading Os base2_value = leading0s + base2_value; // loop backwards through base2 for (int i = base2_value.length() - 1; i >= 0; i--) { // if base2 at i = 1 at the very end of converting (instead of 0) if (base2_value == '1') { // break break; } // if base2 at i == -1 if (base2_value 11 chanso -1) { + return 10 hit form along with hara? valus. (with loading@ == base10tobase2-pt2 driver.cc converter.cc converter.hpp CS 128 A+ Editor
76- 77 78 79 80 81- 82 83- 84 85 86- 87 88 89 90 91 92 93 94 95 } if (base2_value == -1) { // change #to return + 8-bit form along with base2 value (with leading0s) return "0b1" + base2_value; } // loop backwards inside loop for (int j = i - 1; j >= 0; j--) { // if base2 at j == 1, set to 0 if (base2_value [j] == '0') { } base2_value [j] = '1'; // else if above conditions are not met, set j to 1 } else { } base2_value [j] = '1'; } // populate str with base2 values that are 8-bit str + base2_value; } return str; base10tobase2-pt2 driver.cc converter.cc converter.hpp CS 128 A+ Editor
X FAILED (+0) X FAILED (+0) X FAILED (+0) X FAILED (+0) Out-of-Bounds Tests Your code doesn't work for numbers of large magnitude! Negative #1 Your code doesn't work for negative numbers. Negative #2 Your code doesn't work for negative numbers. Negative #3 Your code doesn't work for negative numbers. Zero PASSED (+1) PASSED (+1) PASSED (+1) PASSED (+1) Positive #3 Positive #1 Positive #2
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply