I have this code completed. But it isn't showing me my first number that I know exist within these parameters. I should have 4 numbers displayed but I only receive the last 3.
? I'm getting the numbers that you wrote as output.
The Armstrong Numbers up to 450
153
370
371
407
Btw, digits.close() is not needed. That is done automatically by the ofstream's destructor. (Personally I would just print to cout and then redirect it to a file if needed).
You can also change your constructor to just
ofstream digits("SumOfDigits.out");
Last, in general it's cleaner to declare a variable only in the scope that it's needed.
I would declare number, sum, remainder all inside the while loop, because that's where they're only used.
#include <iostream>
#include <cmath>
bool is_armstrong_number(int n)
{
intconst original_n = n;
int sum = 0;
do sum += std::pow((n % 10), 3); while (n /= 10);
return original_n == sum;
}
int main()
{
for (int i = 100; i <= 450; ++i)
if (is_armstrong_number(i))
std::cout << i << '\n';
}
I'm unsure if I am doing something wrong, or if my computer is wrong. But you all are saying you've printed the results just fine and you've shown it. But I am still only getting the last 3 numbers the 153 is not on my list.
Try writing to cout instead of digits and see if you get the expected output. If you see 153 when using cout there are a number of possible issues. If you don't, there's a different set of possible issues. Report back here and we'll help you troubleshoot.
In addition to what mbozzi said, try doing std::round(pow(remainder,3.0)) instead of just the pow to see if that changes the result. I don't think this should make a difference though.
I have suspicions about the same line as @Ganado. Another alternative is to avoid floating-point round-off altogether and write sum += remainder * remainder * remainder;
You seem to be obsessed with reading/writing everything through files, but you can do a fudge whilst debugging: comment out the three lines concerned with declaring, opening, closing digits as a filestream and temporarily replace with ostream &digits = cout;
Just for fun ...
1 2 3 4 5 6 7 8 9 10 11 12 13
#include <iostream>
usingnamespace std;
int sumCubes( int N )
{
int remainder = N % 10;
return N ? sumCubes( N / 10 ) + remainder * remainder * remainder : 0;
}
int main()
{
for ( int i = 100; i <= 450; i++ ) if ( sumCubes( i ) == i ) cout << i << '\n';
}
@mbozzi, I changed the output to cout and my results were the same.
@lastchance, I am obsessed with writing to a file because that is what the professor has asked us to do. Granted I am very green to the C++ realm, there's a lot I don't know or don't understand. Even the feedback from you kind coders throws me a curveball. I wish I had studied this stuff years ago.
I don't know specifically what instructions are being called, but basically that implementation of pow is returning a number, very, very close to remainder^3, but in this case, it's returning a number just below remainder^3.
For example (not actual numbers): if remainder^3 is 5*5*5 = 125, it's returning 124.9999999.
When 124.9999999 is assigned to an integer, it is truncated to 124.
Try printing the result of (pow(remainder, 3.0) < remainder * remainder * remainder)
Is it 1 (true)?
Bottom Line: Be very careful whenever you're converting something from a floating-point number back to an integer. When in doubt, don't use floating-point calculations for integers, and if you do, then round before assigning to an integer.