How to resolve the algorithm Sum digits of an integer step by step in the C++ programming language
Published on 7 June 2024 03:52 AM
How to resolve the algorithm Sum digits of an integer step by step in the C++ programming language
Table of Contents
Problem Statement
Take a Natural Number in a given base and return the sum of its digits:
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Sum digits of an integer step by step in the C++ programming language
Explanation of the C++ Code:
Function SumDigits
(Non-Template Version):
- The
SumDigits
function takes an unsigned long long integerdigits
and an optional base argumentBASE
(default 10). - It initializes an integer sum to 0 and a copy of the input
digits
calledx
. - It iterates backward from the largest digit (determined using
log
) down to the least significant digit. - For each digit position:
- It calculates the value of the digit at that position (
t
). - Adds it to the sum.
- Subtracts the digit's value multiplied by the base to the appropriate power (
z
) fromx
.
- It calculates the value of the digit at that position (
- Finally, it returns the sum of the individual digits (
x + sum
).
Function SumDigits
(Template Metaprogramming Version):
- This version uses C++ template metaprogramming to achieve the same result as the non-template version.
- It defines a recursive template function
For
that iterates through the digits ofdigits
in a similar manner to the non-template version. - The
SumDigits
template function callsFor
to perform the iteration and summation. - The template parameters allow the function to be used with different data types and bases.
Main Function:
- Both versions of
SumDigits
are invoked with various input values and bases to demonstrate their functionality. - The output is a list of integers representing the sum of the digits for each input value.
Output:
1 15 15 31 31
1 15 15 31 31
Summary:
This code provides two implementations of a function that calculates the sum of the digits in a given integer, using both a non-template and template metaprogramming approach. The template metaprogramming version demonstrates how C++ can be used to write highly optimized and efficient code through template specialization.
Source code in the cpp programming language
#include <iostream>
#include <cmath>
int SumDigits(const unsigned long long int digits, const int BASE = 10) {
int sum = 0;
unsigned long long int x = digits;
for (int i = log(digits)/log(BASE); i>0; i--){
const double z = std::pow(BASE,i);
const unsigned long long int t = x/z;
sum += t;
x -= t*z;
}
return x+sum;
}
int main() {
std::cout << SumDigits(1) << ' '
<< SumDigits(12345) << ' '
<< SumDigits(123045) << ' '
<< SumDigits(0xfe, 16) << ' '
<< SumDigits(0xf0e, 16) << std::endl;
return 0;
}
// Template Metaprogramming version by Martin Ettl
#include <iostream>
#include <cmath>
typedef unsigned long long int T;
template <typename T, T i> void For(T &sum, T &x, const T &BASE)
{
const double z(std::pow(BASE,i));
const T t = x/z;
sum += t;
x -= t*z;
For<T, i-1>(sum,x,BASE);
}
template <> void For<T,0>(T &, T &, const T &){}
template <typename T, T digits, int BASE> T SumDigits()
{
T sum(0);
T x(digits);
const T end(log(digits)/log(BASE));
For<T,end>(sum,x,BASE);
return x+sum;
}
int main()
{
std::cout << SumDigits<T, 1 , 10>() << ' '
<< SumDigits<T, 12345 , 10>() << ' '
<< SumDigits<T, 123045, 10>() << ' '
<< SumDigits<T, 0xfe , 16>() << ' '
<< SumDigits<T, 0xf0e , 16>() << std::endl;
return 0;
}
You may also check:How to resolve the algorithm Loops/Continue step by step in the AWK programming language
You may also check:How to resolve the algorithm Long year step by step in the AppleScript programming language
You may also check:How to resolve the algorithm Roots of a quadratic function step by step in the Mathematica/Wolfram Language programming language
You may also check:How to resolve the algorithm Undefined values step by step in the PicoLisp programming language
You may also check:How to resolve the algorithm Greatest subsequential sum step by step in the Python programming language