Programming - Week 6 problem 2

profileProTutor11
 (Not rated)
 (Not rated)
Chat

 

 

NOTE: Remember that you are NOT allowed to use the break, return, or exit statements to exit from a loop. All loops must exit ONLY via their test condition. 

 

Program #2 

Mathematically, given a number , we can define a sequence , , ... where is the sum of the squares of the digits of . n is happy if and only if there exists i such that . 

Here is an example: 

First, take each digit of any positive integer, square them and add them together. We will use the number nineteen as an example: 

19 12 + 92 = 82 

Then take each digit of this number, square them and add them together: 

82 82 + 22 = 68 

And repeat until the result is 1: 

68 62 + 82 = 100 

100 12 + 02 + 02 = 1 

So when the sum of the squares of the digits in the last member in the sequence starting with n eventually reduces to 1, n is happy. 

Additionally: 

If a number is happy, then all members of its sequence are happy. If a number is unhappy, all members of its sequence are unhappy. 

The above example sequence: 19, 82, 68, 100 is happy, and so all of the terms within the sequence are also happy. 

Write a program that determines which numbers produce a happy sequence, within a series of test numbers. 

The user will provide a first number to test and a last number to test. The program will test the numbers starting with the first number, incrementing by one, and repeating until the last number. 

Program Implementation Requirements: 

1. Tell the user what the program does before prompting for input. 

 

 

2. main will call a user-defined function to read the first and last numbers for the range to test from the user. 

• The user-defined function will: 

o Separately read and validate each number. Both the first and last numbers must be at least 1 and can be up to 9999 (remember to define constants for these values!) 

If the first number is not in the correct range, issue an error message and then re-prompt until the number entered is valid. 

o The last number cannot be smaller than the first number. 

If the last number is not in the correct range or is smaller than the first number, issue an error message and then re-prompt until the number entered is valid. 

o Pass back both values to main using reference parameters. 

3. main will start with the first number as the test number, and loop until all numbers including the last number have been tested. Each time the loop runs, main will: 

o Call a user-defined function to test if the number is happy. 

o Display the results (whether the sequence is happy or unhappy), and pause so the results can be read by the user. 

o Increment the test number. 

• The user-defined function to test whether a number is happy must implement nested loops, as follows: 

o A test number will be passed into the function. 

o The function’s outer loop will test the number, as follows: 

o Use an inner loop to compute the sum of the squares of the digits in the current term. NOTE: You must use a loop, so this program will still work for larger numbers, if the maximum value of the last number constant is modified. 

o Count and display each term. 

o Each term displayed should be formatted to a width of 8, and 10 terms should be displayed per line. 

o Repeat with the sum of the squares of the digits in the current term becoming the next term to test, until it is determined: 

The test number is happy (when the sum of the square of the digits is 1) 

The test number is unhappy (when 50 terms have been computed, without reaching 1). 

o After the outer loop exits, the function shall display the number of terms in the sequence. 

o A boolean value will be returned to main, indicating whether the displayed sequence is happy or not. 

 

Program to test for Happy numbers 

Enter the first number to test: 79 

Enter the last number to test: 1 

Invalid -- must be between 79 and 9999. Try again. 

Enter the last number to test: 82 

Test sequence for 79 is: 

79 130 10 1 

4 sequence terms computed 

The above sequence of numbers is happy 

Press any key to continue . . . 

Test sequence for 80 is: 

80 64 52 29 85 89 145 42 20 4 

16 37 58 89 145 42 20 4 16 37 

58 89 145 42 20 4 16 37 58 89 

145 42 20 4 16 37 58 89 145 42 

20 4 16 37 58 89 145 42 20 4 

50 sequence terms computed 

The above sequence of numbers is unhappy 

Press any key to continue . . . 

Test sequence for 81 is: 

81 65 61 37 58 89 145 42 20 4 

16 37 58 89 145 42 20 4 16 37 

58 89 145 42 20 4 16 37 58 89 

145 42 20 4 16 37 58 89 145 42 

20 4 16 37 58 89 145 42 20 4 

50 sequence terms computed 

The above sequence of numbers is unhappy 

Press any key to continue . . . 

Test sequence for 82 is: 

82 68 100 1 

4 sequence terms computed 

The above sequence of numbers is happy 

Press any key to continue . . . 

Testing of numbers 79 to 82 complete. 

 

NOTES: 

o If the first and last numbers are the same, and only the first number will be tested. 

o For testing purposes, it may help to know that the happy numbers up to 100 are: 

 

1, 7, 10, 13, 19, 23, 28, 31, 32, 44, 49, 68, 70, 79, 82, 86, 91, 94, 97, 100 

Program and Function Documentation (both programs) 

1) Include top of program comments as specified in course Content section 1.8. 

2) Comment any constants/variables whose names are not completely descriptive. 

3) Include prototypes for all of your user-defined functions. The prototypes should be placed above main, and the function definitions should be placed below main. 

4) Provide a comment above each user-defined function definition that includes: 

o Dividing lines 

o A description of what the function does 

o The name and a description of each input parameter, used to pass values into the function 

o The name and a description of each output parameter, used to pass values back from the function 

o The name (if there is one), and a description of what is returned from the function via the return statement (if anything). 

 

Examples: 

// ---------------------------------------------------------- 

// Task: Reads a positive and negative number from the user 

// and error checks them 

// Input parameters: prompt – prompt to display 

// Output parameters: posValue, negValue – valid numbers read 

// ---------------------------------------------------------- 

void getData (string prompt, int& posValue, int& negValue) 

 

    • 10 years ago
    A+ Answers - Most Economical
    NOT RATED

    Purchase the answer to view it

    blurred-text
    • attachment
      happynumbers.cpp