Write a program that prompts the user for a positive integer N Then compute the value...
Fantastic news! We've Found the answer you've been seeking!
Question:
Transcribed Image Text:
Write a program that prompts the user for a positive integer N Then compute the value of triangle numbers Triangle (N) using a recursive subroutine. The definition of a triangle number is: o Triangle(N 1 ) = N + Triangle(N-1) O For example: I I Triangle(1) = 1 Triangle( 2 ) = 3 Triangle( 3 ) = 6 Triangle( 4 ) = 10 For this, each function invocation has its own parameter N in its stack frame. Then it gets the return value from the next invocation and adds it into its N (stored in the stack frame) before retrieving that N (holding the sum) and putting the result into $v0 to return to the caller Your program will prompt the user for N and calculate and print Triangle(N). Test with several values, including a large value like 100. The square of an integer N is equal to Triangle(N) + Triangle( N-1 ). Write a recursive function using the Triangle function from part 1 to calculate Square (N). Your program has to prompt the user for a positive integer N and then print the result of Square (N). The definition of a square number is: Square(N 1 ) = Triangle(N) + Triangle(N-1) Call Square using a full stack frame, and have Square call Triangle twice, passing its argument on the stack. For the recursion, create automatic local variables on the stack and save both recursive results in the variables, then retrieve the values to add together to return to the caller. Write a program that prompts the user for a positive integer N Then compute the value of triangle numbers Triangle (N) using a recursive subroutine. The definition of a triangle number is: o Triangle(N 1 ) = N + Triangle(N-1) O For example: I I Triangle(1) = 1 Triangle( 2 ) = 3 Triangle( 3 ) = 6 Triangle( 4 ) = 10 For this, each function invocation has its own parameter N in its stack frame. Then it gets the return value from the next invocation and adds it into its N (stored in the stack frame) before retrieving that N (holding the sum) and putting the result into $v0 to return to the caller Your program will prompt the user for N and calculate and print Triangle(N). Test with several values, including a large value like 100. The square of an integer N is equal to Triangle(N) + Triangle( N-1 ). Write a recursive function using the Triangle function from part 1 to calculate Square (N). Your program has to prompt the user for a positive integer N and then print the result of Square (N). The definition of a square number is: Square(N 1 ) = Triangle(N) + Triangle(N-1) Call Square using a full stack frame, and have Square call Triangle twice, passing its argument on the stack. For the recursion, create automatic local variables on the stack and save both recursive results in the variables, then retrieve the values to add together to return to the caller.
Expert Answer:
Answer rating: 100% (QA)
Heres a Python program that accomplishes the tasks you described using recursive functions def t... View the full answer
Related Book For
Introduction to Java Programming, Comprehensive Version
ISBN: 978-0133761313
10th Edition
Authors: Y. Daniel Liang
Posted Date:
Students also viewed these programming questions
-
Johnsons Electronics Store, owned by Ms. Johnson, recently conducted a sale event to clear out old inventory and generate revenue. The following transactions occurred during and after the sale: 1....
-
Convert the decimal number (123.456)10 into the following indicated bases: (a) Binary number (b) Octal number (c) Hexadecimal number (1 Mark) Q2: Convert the decimal number 456 to binary in two ways:...
-
31% of American adults surveyed stated that giving up their phone for a day would be more difficult than giving up their significant other. In 2012, the United States population over the age of 18...
-
5.8 Calculate the U value for the following double- glazed windows assuming the temperatures and the heat transfer coefficients as given in Example 5.1: (a) Ordinary glass with vacuum between the...
-
A car drives for half an hour at constant speed and uses 30 MJ over a distance of 40 km. What was the traction force to the road and its speed?
-
The motion picture industry is a competitive business. More than 50 studios produce a total of 300 to 400 new motion pictures each year, and the financial success of each motion picture varies...
-
A hydraulic press has a ram diameter of 250 mm and a plunger diameter of 30 mm. For a force of 40 N applied on the plunger, find the force exerted on the object by the hydraulic press.
-
Selected account balances and transactions of Alpine Manufacturing Co. follow: May Transactions: a. Purchased raw materials and factory supplies on account at costs of $45,000 and $10,000,...
-
You are presented with the following trial balance of Golden City Bhd as at 31 December 2020: Debit RM'000 Credit RM'000 Intangible assets 50 Plant at cost 100 Plant, accumulated depreciation, at 1...
-
The focus of this project is to create a master budget for the Williams Company based on the companys industry outlook, recent company outcomes, and the companys business rules. You will create a...
-
Sameer corporate is an equity based firm that does not have any debt in the market. The company issued 20,00,000 common shares with a face value of TK 50 per shares. Due to its excellent financial...
-
1. What year are you preparing your students for? 1973? 1995? Provide evidence that your school's curriculum and the program you use are preparing your students for 2030? Are you even preparing them...
-
Portsmouth Company makes upholstered furniture. Its only variable cost is direct materials. The demand for the company's products far exceeds its manufacturing capacity. The bottleneck (or...
-
1. Compute the lower of cost or market for the Inventory applied separately to each item. 2. If the market amount is less than the recorded cost of the inventory, then record the LCM adjustment to...
-
In this problem we assume the market portfolio is the S&P 500 index. The top 5 stocks in the S&P 500 index, when ranked by market capitalization, make up 22% of the total market capitalization of the...
-
Were the public relations tactics of the food industry ethical? Why or why not? The public relations practices of the food industry were not ethical in this video. The industry used deceptive...
-
Assume that Mexico (a small open economy in a recession seeking to promote economic growth through trade) is now adopting a fixed exchange rate system (e.g. peg the peso to the US dollar). [25...
-
Define the essential properties of the following types of operating systems: a. Batch b. Interactive c. Time sharing d. Real time e. Network f. Parallel g. Distributed h. Clustered i. Handheld
-
Write a program that prompts the user to enter the year and first day of the year, and displays the first day of each month in the year. For example, if the user entered the year 2013, and 2 for...
-
The two-dimensional arrays m1 and m2 are identical if they have the same contents. Write a method that returns true if m1 and m2 are identical, using the following header:public static...
-
In business applications, you are often asked to compute the mean and standard deviation of data. The mean is simply the average of the numbers. The standard deviation is a statistic that tells you...
-
What percentage of the 9,449 survey respondents live in the Eastern part of the county?
-
(a) What percentage of those not completing high school were females? (b) What percentage of those not completing high school were males? (c) What percentage of those completing high school were...
-
Which county region has the largest percentage of respondents?
Study smarter with the SolutionInn App