demolaynyc 0 Report post Posted December 4, 2006 Ok I need someone to help me tackle this program. Here's the question:Exercise 9a) Write a program that displays the sum of the cubes of the digits of any non-negative integer. Two program runs are shown below: Enter an integer: 145 Sum of digits is 10 Enter an integer: 8888 Sum of the cubes of the digits is 2048 Modify the program to determine what integers of two, three, or four digits are equal to the sum of the cubes of their digits.----source file for part A----- /* Chapter 4 Exercise 9 by Albert Villaroman 11-20-06 */#include <iostream.h>int main() { long numValue; //user input cout <<"Enter an integer: "; cin >>numValue; long MaxDigits = 100000000; while (MaxDigits > numValue) { //while loop to get how many digits numValue has MaxDigits /= 10; } long sum = 0; //sum of all digits while (MaxDigits >=1) { long digit = numValue / MaxDigits; //get digit sum += digit*digit*digit; numValue = numValue % MaxDigits; //enter new value for numValue MaxDigits /= 10; //ex: 100 => 10 } int counter = 10; while (counter < 90000) { if (sum == numValue) { cout <<numValue; } counter++; } cout <<"Sum of the cubes of the digits is " <<sum <<endl; return(0);} What I need help with is part B.Can someone paraphrase the part B question for me because it just doesn't make sense to me. I'd appreciate a full source file of the second part if you can.Thank you. Share this post Link to post Share on other sites
pyost 0 Report post Posted December 4, 2006 Modify the program to determine what integers of two, three, or four digits are equal to the sum of the cubes of their digits. First of all, you need to go through all numbers from 10 to 9999. Then, by using this code, you will calculate the sum of the cubes of the digits for every number. After that, you will check whether the number itself is equal to the previously calculated sum. If it is, that is one of the numbers you are looking for. Share this post Link to post Share on other sites
Arbitrary 0 Report post Posted December 5, 2006 cool.gif Modify the program to determine what integers of two, three, or four digits are equal to the sum of the cubes of their digits.Hmm...following whatever Pyost said should get you somewhere, but I just wanted to add my two cents. Since you're going to be reusing the whole "get-the-cube-of-the-sum-of-the-digits" code, you might as well put that into a method and then call that method every time you want to use the code. It'll probably save a bit of space (as well as some headaches) :] Share this post Link to post Share on other sites
demolaynyc 0 Report post Posted December 5, 2006 (edited) Yea, I agree but since I'm still learning C++, I wouldn't really know how to do it. Even if, my instructor would get a little suspicious. Hey pyost, can you put what you just said into the C++ coding because I understand better if i see the code. ThanksPS: I think this exercise is stupid I mean will it even be useful in real programming? When would be the time to put this into application, does anyone know? I'm not talking about C++ in general, it's just this exercise specifically.---Hold up---So is part B a separate program? I should just use the formula from part A? This exercise is really confusing.---Alright here's the code that I came up with: /* Chapter 4 Exercise 9 by Albert Villaroman 11-20-06 */#include <iostream.h>int main() { long MaxDigits = 100000000, sum = 0, num = 10; //sum of all digitswhile (num < 9999) { while (num < 50) { int numBeforeCalc = num; cout <<"this is num: " <<num <<endl; while (MaxDigits > num) { //while loop to get how many digits num has MaxDigits /= 10; } while (MaxDigits >= 1) { long digit = num / MaxDigits; //get digit cout <<"digit: " <<digit <<endl; sum += digit*digit*digit; num = num % MaxDigits; //enter new value for numValue MaxDigits /= 10; //ex: 100 => 10 } cout <<sum <<" " <<numBeforeCalc <<endl; if (sum == numBeforeCalc) cout <<"Number that equals original number. " <<numBeforeCalc <<endl; num = numBeforeCalc; num += 1; } //end big while int q; cin >>q; return(0);} Edited December 5, 2006 by demolaynyc (see edit history) Share this post Link to post Share on other sites
yeh 0 Report post Posted December 8, 2006 Yeah... I can understand your predicament. Sometimes the question posed to you by your teacher/lecturer is just plain vague. Heck, sometimes what they want and what the question want is entirely different. So, I suggest you go ask your teacher what it is that he/she wants. Ask him/her for a sample of input and output so you can better know the question. Now, what pyost and arbitrary said is actually correct. But since you asked for some code, here is some help. Hehe.... I'm going to embarass myself here if it turns out to be wrong... Anyway, feel free to correct me if you think what I'm suggesting is wrong. The code below loops through 10 to 9999, computing their sum of the cubes of their digits and then compare the sum to the original number to see whether they are the same. I try to use as much code from your original post as possible. #include <iostream.h>int main() { counter=10; while(counter<=9999) { numvalue = counter; //From original code long MaxDigits = 100000000; while (MaxDigits > numValue) { //while loop to get how many digits numValue has MaxDigits /= 10; } long sum = 0; //sum of all digits while (MaxDigits >=1) { long digit = numValue / MaxDigits; //get digit sum += digit*digit*digit; numValue = numValue % MaxDigits; //enter new value for numValue MaxDigits /= 10; //ex: 100 => 10 } //End of original code if(counter==sum) cout<<counter<<endl; counter++; } return(0);} Share this post Link to post Share on other sites
pyost 0 Report post Posted December 8, 2006 I believe this code is exactly what you need. I think this exercise is stupid I mean will it even be useful in real programming? When would be the time to put this into application, does anyone know? I'm not talking about C++ in general, it's just this exercise specifically.I shared the same opinion a year ago, when I started programming, but it turned out to be quite different. Sure, some examples aren't really useful when it comes to real programming, but they show you how to deal with some basics (like this one). And it will all be necessary at some point. I am currently developing a PHP application, and whereas I have learned Pascal in school, not PHP, it has helped me a lot. Not because of the code itself, but because of the problem-solving techniques.So just don't complain, it's worth it Share this post Link to post Share on other sites