How to resolve the algorithm Negative base numbers step by step in the C++ programming language
Published on 7 June 2024 03:52 AM
How to resolve the algorithm Negative base numbers step by step in the C++ programming language
Table of Contents
Problem Statement
Negative base numbers are an alternate way to encode numbers without the need for a minus sign. Various negative bases may be used including negadecimal (base -10), negabinary (-2) and negaternary (-3).[1][2]
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Negative base numbers step by step in the C++ programming language
The provided C++ code defines two functions, encodeNegativeBase
and decodeNegativeBase
, for converting integers to and from a negative base representation. Negative bases are used in certain mathematical applications, such as computer arithmetic and cryptography.
-
encodeNegativeBase
Function:- This function takes two arguments:
n
: The integer to be encoded.b
: The negative base to use for encoding. Valid values forb
are between -62 and -1.
- It encodes
n
in the specified negative baseb
and returns the encoded string. - The encoding process involves repeated division of
n
byb
, taking the remainder after each division, and then reversing the sequence of remainders. - If
b
orn
are out of range, it throws a runtime error.
- This function takes two arguments:
-
decodeNegativeBase
Function:- This function takes two arguments:
ns
: The encoded string to be decoded.b
: The negative base in which the string was encoded. Valid values forb
are between -62 and -1.
- It decodes the given string
ns
from the specified negative baseb
and returns the decoded integer. - The decoding process involves multiplying each digit in
ns
by the corresponding power ofb
and summing the results. - If
b
orns
are invalid, it throws a runtime error.
- This function takes two arguments:
-
main
Function:- The
main
function demonstrates the usage of theencodeNegativeBase
anddecodeNegativeBase
functions. - It defines a vector of pairs where each pair contains an integer and its corresponding negative base.
- For each pair in the vector, it encodes the integer in the given negative base, prints the encoded string, and then decodes the encoded string back to the original integer.
- The
In summary, this code provides functions for encoding and decoding integers using negative bases, which are useful in certain mathematical and computational scenarios.
Source code in the cpp programming language
#include <iomanip>
#include <iostream>
#include <tuple>
#include <vector>
const std::string DIGITS = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
std::string encodeNegativeBase(int64_t n, int b) {
if (b < -62 || b > -1) {
throw std::runtime_error("Argument out of range: b");
}
if (n == 0) {
return "0";
}
std::string output;
int64_t nn = n;
while (nn != 0) {
int rem = nn % b;
nn /= b;
if (rem < 0) {
nn++;
rem -= b;
}
output += DIGITS[rem];
}
std::reverse(output.begin(), output.end());
return output;
}
int64_t decodeNegativeBase(const std::string& ns, int b) {
if (b < -62 || b > -1) {
throw std::runtime_error("Argument out of range: b");
}
if (ns == "0") {
return 0;
}
int64_t total = 0;
int64_t bb = 1;
for (auto it = ns.crbegin(); it != ns.crend(); it = std::next(it)) {
auto ptr = std::find(DIGITS.cbegin(), DIGITS.cend(), *it);
if (ptr != DIGITS.cend()) {
auto idx = ptr - DIGITS.cbegin();
total += idx * bb;
}
bb *= b;
}
return total;
}
int main() {
using namespace std;
vector<pair<int64_t, int>> nbl({
make_pair(10, -2),
make_pair(146, -3),
make_pair(15, -10),
make_pair(142961, -62)
});
for (auto& p : nbl) {
string ns = encodeNegativeBase(p.first, p.second);
cout << setw(12) << p.first << " encoded in base " << setw(3) << p.second << " = " << ns.c_str() << endl;
int64_t n = decodeNegativeBase(ns, p.second);
cout << setw(12) << ns.c_str() << " decoded in base " << setw(3) << p.second << " = " << n << endl;
cout << endl;
}
return 0;
}
You may also check:How to resolve the algorithm Element-wise operations step by step in the Sidef programming language
You may also check:How to resolve the algorithm Even or odd step by step in the x86-64 Assembly programming language
You may also check:How to resolve the algorithm Sorting algorithms/Merge sort step by step in the C++ programming language
You may also check:How to resolve the algorithm Sequence of non-squares step by step in the XLISP programming language
You may also check:How to resolve the algorithm Sorting algorithms/Counting sort step by step in the Oz programming language