Question

In: Computer Science

2 Program 1 - Special Values Patrick Star wasted a lot of time in Boating School...

2 Program 1 - Special Values

Patrick Star wasted a lot of time in Boating School instead of signing up for his Spanish Class. Unfortunately for him, Spanish 101 is now full, and the only other class that will suit his schedule is Advanced Math. Patrick is determined not to let this defeat him. He will make his way up the stairs of learning one way or another. However, it’s been a while since Average Everyday Math, and he is somewhat behind. Patrick is going to put his programming class (he took that last term) to good use and write programs to do his homework. For the rest of this homework, you are Patrick Star, trying to outwit the math teacher.

For this program, we define a new term called Special Value. The Special Value of a number is the product of a random number between 15 and 25 (inclusive) and the difference between the number and its reverse.

1

For example, the Special Value of 1234 could be 55566 ( 18 * ( | 4321 - 1234 | ) ).
In this program, you are required to find the sum of the special values of a set of numbers. Make

sure you conform to the following requirements.

  1. Write a function called reverse that takes a number as a parameter and returns the reversed number. (20 points)

  2. Write a function called value that accepts a number as a parameter, calculates the special value of that number, and returns it. This function should call the reverse function. (12 points)

  3. In the main function, accept a seed for the Random Number Generator from the user, and use it to set up the RNG. (3 points)

  4. Then, accept a series of numbers from the user. Stop if the number entered is 0. Use the value function to find the special value of each of the numbers as they are entered, and calculate their sum. Finally, print the sum. (10 points)

  5. Make sure you add comments to explain your logic. (5 points)

2.1 Sample Run

Please note that the final answer depends on the random number generated at each function call, and you might get a different answer.

Enter the seed for the random number generator: 75361
Enter the numbers (0 to stop):
123
603
957
63
4567
62576
19
0
The sum of the special values is 99468

Solutions

Expert Solution

#include<bits/stdc++.h>
using namespace std;

//function to reverse a number
int reverse(int num)
{
    int rev_num = 0;
    while (num > 0)
    {
        rev_num = rev_num * 10 + num % 10;
        num = num / 10;
    }
    return rev_num;
}

int value(int n) {
    return  (15 + rand() % 11) * abs(n - reverse(n));
}
int main() {

    cout << "Enter the seed for the random number generator:";
    int seed, result = 0;
    //reading seed value
    cin >> seed;

    cout << "Enter the numbers (0 to stop):\n";
    // loop runs untill user enters 0
    while (true) {
        int x;
        cin >> x;
        if (x == 0)
            break;
        result += value(x);
    }
    // displaying the result
    cout << "The sum of the special values is " << result << endl;

    return 0;
}

OUTPUT:


Related Solutions

Question 1: Patrick Johnson is a manager for Star Company. He tells you that his company...
Question 1: Patrick Johnson is a manager for Star Company. He tells you that his company always maximizes profitability by accepting the investment opportunity with the highest internal rate of return. Explain to Mr. Johnson how his company may improve profitability by sometimes selecting investment opportunities with lower internal rates of return. Question 2: What typical cash inflow and outflow items are associated with capital investments? Explain.
Question 1: Patrick Johnson is a manager for Star Company. He tells you that his company...
Question 1: Patrick Johnson is a manager for Star Company. He tells you that his company always maximizes profitability by accepting the investment opportunity with the highest internal rate of return. Explain to Mr. Johnson how his company may improve profitability by sometimes selecting investment opportunities with lower internal rates of return. Question 2: What typical cash inflow and outflow items are associated with capital investments? Explain.
Sunland Company from time to time embarks on a research program when a special project seems...
Sunland Company from time to time embarks on a research program when a special project seems to offer possibilities. In 2019, the company expends $329,000 on a research project, but by the end of 2019 it is impossible to determine whether any benefit will be derived from it. Part 1 The project is completed in 2020, and a successful patent is obtained. The R&D costs to complete the project are $119,000. The administrative and legal expenses incurred in obtaining patent...
QUESTION 3 Izzudin Bhd from time to time embarks on a research program when a special...
QUESTION 3 Izzudin Bhd from time to time embarks on a research program when a special project seems to offer possibilities. In 2018, the company expends RM325,000 on a research project, but by the end of 2018 it is impossible to determine whether any benefit will be derived from it. REQUIRED: What account should be charged for the RM325,000, and how should it be shown in the financial statements? The project is completed in 2019, and a successful paten is...
Aaron owns two lots of real property, Lot 1 and Lot 2. Lot 1 is located...
Aaron owns two lots of real property, Lot 1 and Lot 2. Lot 1 is located next to a public right-of-way, but Lot 2 is landlocked from the public lot right-of-way by Lot 1. Aaron conveys Lot 2 to Bob with a deed, making no reference to any rights-of-way or any other kinds of easement benefiting Lot 2. Bob then sells Lot 2 to Carol. Carol claims against Aaron that she has a right-of- way easement over Lot 1 for...
An elementary school started a special reading enrichment program for seventh-graders that has been underway for...
An elementary school started a special reading enrichment program for seventh-graders that has been underway for eight months. One of the investigators wants to confirm the program is having its intended effect, and collects a sample of 34 students from the program with a standardized reading test average of 24.1. The standardized reading test average for seventh-graders in the country is 26.1 with a standard deviation of 4.9. What can the investigator conclude with α = 0.01? a) What is...
2 quintal of mango was harvested. 1.5% was wasted due to diseases during post-harvest, 1% was...
2 quintal of mango was harvested. 1.5% was wasted due to diseases during post-harvest, 1% was lost due to animal consumption. Family consumption of mango is 25 kilograms, 5 kilograms was given to labor as wages, 5 kilograms was retained for farm needs, and 3 kilograms was retained for other purposes. CALCULATE MARKETABLE SURPLUS Note: NOTE: PLEASE ANSWER ACCURATELY ONLY IF YOU KNOW. I AM GETTING LOT OF WRONG ANSWERS ON CHEGG. I AM POSTING IT FOR THE SECOND TIME....
After reading Special Topic 1 and Special Topic 2, write a 2-page paper answering and describing:...
After reading Special Topic 1 and Special Topic 2, write a 2-page paper answering and describing: Can democracy survive if a majority of the citizenry pay little or nothing in taxes while benefiting directly from a higher level of government spending? Why or why not? Discuss
1.- Explain and compare Two (2) examples of a special order (Accepting a special order and...
1.- Explain and compare Two (2) examples of a special order (Accepting a special order and Rejecting a special order) for a sample business decision for a company (100 to 200 words) 2.-: Explain Three (3) benefits when using incremental analysis for making decisions.
in C++ Compile and run the program for the following values: r = 2 Cm, h...
in C++ Compile and run the program for the following values: r = 2 Cm, h = 10 Cm. The answer should be: The cross section area of the cylinder is 3.8955634 c The side area of the cylinder is 19.474819 inch-sqr Did you get the same answer? Explain the reason for such an error and fix the problem. Modify the previous program to include a new function called total_area, that computes the total suface area of a cylinder. The...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT