|
|
Subject:
C++ Algorithm for Insipid Numbers
Category: Computers > Algorithms Asked by: stonewallwoman-ga List Price: $5.00 |
Posted:
10 Mar 2003 00:13 PST
Expires: 09 Apr 2003 01:13 PDT Question ID: 174080 |
I need an algorithm in C++ to generate insipid numbers. I know what the "word" equation says, but I can't seem to get it written correctly in code. Been beating my head against this keyboard for 4 days trying to get it figured out on my own! | |
| |
| |
|
|
Subject:
Re: C++ Algorithm for Insipid Numbers
Answered By: maniac-ga on 10 Mar 2003 17:58 PST Rated: |
Hello Stonewallwoman, Here is a c++ program that should do what you want. I'll first provide some comments first to describe how the code works. The function is_insipid is a little bit tricky but runs pretty simply. The variable i will generate the sum. In each pass through the loop, i = i + (x%10)*(x%10) basically a running sum of the squares of the digits. Note that x%10 is the remainder of x/10 - a digit. x will be divided by 10 each time through the loop until it is less than 10. Note that integer x/10 is zero - that will cause the loop to exit. The function then prints the new sum and returns that sum to the caller. The main program is pretty simple. Ask the user for a value, copy it and pass it to is_insipid to compute the next value. Repeat the loop until the result is 1 or 58. At that point, print the appropriate answer and exit the program. I tested it with the values from the web site http://www.stonehill.edu/compsci/CS103/Assignments/assignment3D.htm (scroll down to the second assignment) and this gets the same answers. If this is not clear - don't hesitate to use a clarification request to get more information. --Maniac #include <iostream.h> int is_insipid(int x) { int i = 0; while (x>0) { i += (x % 10)*(x % 10); x = x/10; } cout << "Now have " << i << "\n"; return i; } int main(int argc, char *argv[]) { int x; int y; cout << "Enter a value:"; cin >> x; y = x; do { y = is_insipid(y); } while ((y != 1) && (y != 58)); if (y == 1) cout << x << " is insipid.\n"; else cout << x << " is NOT insipid.\n"; return 0; } --Maniac |
stonewallwoman-ga
rated this answer:
and gave an additional tip of:
$2.00
The answer I received from maniac-ga was consise, easy for my beginner brain to comprhend, and eaisily translated into terms that my instructor would accept. |
|
Subject:
Re: C++ Algorithm for Insipid Numbers
From: eadfrith-ga on 10 Mar 2003 12:10 PST |
This is an answer to the problem posed here: http://www.stonehill.edu/compsci/CS103/Assignments/assignment3D.htm It's in Java, since that's the language I know best. Shouldn't be too hard to convert it to C++. Also, you can either tackle this using recursion or iteration. I've supplied solutions using both techniques. Cheers. public static void main(String[] args) { int n = Integer.parseInt(args[0]); System.out.print("The sequence starting at " + n + " is: "); if(isInsipidIterative(n)) { System.out.println("\nInspid"); } else { System.out.println("\nNot Inspid"); } } public static boolean isInsipidIterative(int n) { while(true) { System.out.print(n + " "); if(n == 1) { return true; } else if(n == 58) { return false; } else { n = sumDigits(n); } } } public static boolean isInsipidRecursive(int n) { System.out.print(n + " "); if(n == 1) { return true; } else if(n == 58) { return false; } else { return isInsipidRecursive(sumDigits(n)); } } public static int sumDigits(int n) { int sum = 0; while(n > 0) { int digit = n % 10; sum += digit * digit; n /= 10; } return sum; } |
If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you. |
Search Google Answers for |
Google Home - Answers FAQ - Terms of Service - Privacy Policy |