Rewrite the following nested if-else statement as a switch statement that accomplishes exactly the same thing....
Fantastic news! We've Found the answer you've been seeking!
Question:
Transcribed Image Text:
Rewrite the following nested if-else statement as a switch statement that accomplishes exactly the same thing. Assume that num is an integer variable that has been initialized, and that there are functions fl, f2, f3, and f4. Do not use any if or if-else statements in the actions in the switch statement, only calls to the four functions. if num < -2 || num > 4 f1 (num) else if num <= 2 if num >= 0 f2 (num) else f3 (num) end else f4 (num) end end Test your switch statement with num: a random integer from -5 to 5, and creating anonymous functions for fl-f4: fl=sin (num), f2=enum cos(num), f3= num?+2*num, f4= arctan(num). QUESTION 2: Write a script that will prompt the user for a quiz grade and error-check until the user enters a valid quiz grade. The script will then echo print the grade. For this case, valid grades are in the range from 0 to 10 in steps of 0.5. Do this by creating a vector of valid grades and then use any or all in the condition in the while loop. QUESTION 3: Write a function that will receive a variable number of input arguments: the length and width of a rectangle, and possibly also the height of a box that has this rectangle as its base. The function should return the rectangle area if just the length and width are passed, or also the volume if the height is also passed. QUESTION 4: The Fibonacci numbers is a sequence of numbers Fi: 0 1 1 2 35 8 13 21 34 ... where Fo is 0, Fı is 1, F2 is 1, F3 is 2, and so on. A recursive definition is: Fo = 0 F1 = 1 Fn = Fn-2 +Fn-1 if n > 1 Write a recursive function to implement this definition. This local function will receive one integer argument n, and it will return one integer value that is the nth Fibonacci number. Note that in this definition, there is one general case but two base cases. Then, test the function by printing the first 20 Fibonacci numbers. QUESTION 5: Sometimes a value that is much larger or smaller than the rest of the data (called an outlier) can throw off the mean. In order to handle this, sometimes the minimum and maximum values from a data set are discarded before the mean is computed. Write a function that will return the mean of the values in a vector, not including the outliers. Assume that the values in the vector are unique. It is okay to use the built-in mean function. To test this, create a vector of 10 random integers, each in the range from 0 to 50, and pass this vector to the function. QUESTION 6: Write a local function in your script that will receive data points in the form of x and y vectors. If the lengths of the vectors are not the same, then they can't represent data points, so an error message should be printed. Otherwise, the function will fit a polynomial of a random degree through the points and will plot the points and the resulting curve with a title specifying the degree of the polynomial. The degree of the polynomial must be less than the number of data points, n, so the function must generate a random integer in the range from 1 to n-1 for the polynomial degree. QUESTION 7: Write a local function called cosLineWidths that will plot cos (x) for x values ranging from - pi to pi in steps of 0.1, using black *'s. It will do this three times across in one Figure Window, with varying line widths (Note: even if individual points are plotted rather than a solid line, the line width property will change the size of these points.). If no arguments are passed to the function, the line widths will be 1, 2, and 3. If, on the other hand, an argument is passed to the function, it is a multiplier for these values (e.g., if 3 is passed, the line widths will be 3, 6, and 9). The line widths will be printed in the titles on the plots. Test your function with: cosLineWidths Line Width 1.00 Line Width 2.00 Line Width 3.00 1 1 0.8 0.8 0.8 0.6 0.6 0.6 0.4 0.4 0.4 0.2 0.2 0.2 -0.2 -0.2 -0.2 -0.4 -0.4 -0.4 -0.6 -0.6 -0.6 -0.8 -0.8 -0.8 -1 -4 -1 -4 -2 2 -4 -2 0. -2 2 4 cosLineWidths (0.5) Line Width 0.50 Line Width 1.00 Line Width 1.50 1 1 0.8 0.8 0.8 0.6 0.6 0.6 0.4 0.4 0.4 0.2 0.2 0.2 -0.2 -0.2 -0.2 -0.4 -0.4 -0.4 -0.6 -0.6 -0.6 -0.8 -0.8 -0.8 -1 -4 -1 -4 -2 4 -2 -4 -2 X (x)soɔ (x)soɔ (x)soɔ (x)soɔ (x)soɔ cos(x) Rewrite the following nested if-else statement as a switch statement that accomplishes exactly the same thing. Assume that num is an integer variable that has been initialized, and that there are functions fl, f2, f3, and f4. Do not use any if or if-else statements in the actions in the switch statement, only calls to the four functions. if num < -2 || num > 4 f1 (num) else if num <= 2 if num >= 0 f2 (num) else f3 (num) end else f4 (num) end end Test your switch statement with num: a random integer from -5 to 5, and creating anonymous functions for fl-f4: fl=sin (num), f2=enum cos(num), f3= num?+2*num, f4= arctan(num). QUESTION 2: Write a script that will prompt the user for a quiz grade and error-check until the user enters a valid quiz grade. The script will then echo print the grade. For this case, valid grades are in the range from 0 to 10 in steps of 0.5. Do this by creating a vector of valid grades and then use any or all in the condition in the while loop. QUESTION 3: Write a function that will receive a variable number of input arguments: the length and width of a rectangle, and possibly also the height of a box that has this rectangle as its base. The function should return the rectangle area if just the length and width are passed, or also the volume if the height is also passed. QUESTION 4: The Fibonacci numbers is a sequence of numbers Fi: 0 1 1 2 35 8 13 21 34 ... where Fo is 0, Fı is 1, F2 is 1, F3 is 2, and so on. A recursive definition is: Fo = 0 F1 = 1 Fn = Fn-2 +Fn-1 if n > 1 Write a recursive function to implement this definition. This local function will receive one integer argument n, and it will return one integer value that is the nth Fibonacci number. Note that in this definition, there is one general case but two base cases. Then, test the function by printing the first 20 Fibonacci numbers. QUESTION 5: Sometimes a value that is much larger or smaller than the rest of the data (called an outlier) can throw off the mean. In order to handle this, sometimes the minimum and maximum values from a data set are discarded before the mean is computed. Write a function that will return the mean of the values in a vector, not including the outliers. Assume that the values in the vector are unique. It is okay to use the built-in mean function. To test this, create a vector of 10 random integers, each in the range from 0 to 50, and pass this vector to the function. QUESTION 6: Write a local function in your script that will receive data points in the form of x and y vectors. If the lengths of the vectors are not the same, then they can't represent data points, so an error message should be printed. Otherwise, the function will fit a polynomial of a random degree through the points and will plot the points and the resulting curve with a title specifying the degree of the polynomial. The degree of the polynomial must be less than the number of data points, n, so the function must generate a random integer in the range from 1 to n-1 for the polynomial degree. QUESTION 7: Write a local function called cosLineWidths that will plot cos (x) for x values ranging from - pi to pi in steps of 0.1, using black *'s. It will do this three times across in one Figure Window, with varying line widths (Note: even if individual points are plotted rather than a solid line, the line width property will change the size of these points.). If no arguments are passed to the function, the line widths will be 1, 2, and 3. If, on the other hand, an argument is passed to the function, it is a multiplier for these values (e.g., if 3 is passed, the line widths will be 3, 6, and 9). The line widths will be printed in the titles on the plots. Test your function with: cosLineWidths Line Width 1.00 Line Width 2.00 Line Width 3.00 1 1 0.8 0.8 0.8 0.6 0.6 0.6 0.4 0.4 0.4 0.2 0.2 0.2 -0.2 -0.2 -0.2 -0.4 -0.4 -0.4 -0.6 -0.6 -0.6 -0.8 -0.8 -0.8 -1 -4 -1 -4 -2 2 -4 -2 0. -2 2 4 cosLineWidths (0.5) Line Width 0.50 Line Width 1.00 Line Width 1.50 1 1 0.8 0.8 0.8 0.6 0.6 0.6 0.4 0.4 0.4 0.2 0.2 0.2 -0.2 -0.2 -0.2 -0.4 -0.4 -0.4 -0.6 -0.6 -0.6 -0.8 -0.8 -0.8 -1 -4 -1 -4 -2 4 -2 -4 -2 X (x)soɔ (x)soɔ (x)soɔ (x)soɔ (x)soɔ cos(x)
Expert Answer:
Related Book For
Posted Date:
Students also viewed these programming questions
-
Assume that children is an integer variable containing the number of children in a community, and that families is an integer variable containing the number of families in the same community. Write a...
-
If Number is an integer variable and MyString = "One potato" then the following statement is valid: Number = Length_Of(MyString) True or False
-
Four different kinds of fertilizers f1, f2, f3 and f4 are used to study the yield of beans. The soil is divided into three blocks each containing four homogeneous plots. The yields in pounds per acre...
-
The following data pertain to the securities of Linford Company during 2012, the companys first year of operations: a. Purchased 400 shares of Persimmon Corporation stock at $40 per share plus a...
-
a. What proportion of a normal distribution is greater than the mean? b. What proportion is within 1 standard deviation of the mean? c. What proportion is greater than a value that is 1 standard...
-
Replace the force and couple system by an equivalent force and couple moment at point O. Units Used: kN = 103 N Given: M = 8kNm = 60 deg a = 3 m f = 12 b = 3 m g = 5 c = 4 m F1 = 6kN d = 4m F2 = 4kN...
-
After operating for a month, Shawn Andrews dental practice completed the following transactions during July: Using the steps outlined in the five-step transaction analysis, record the transactions in...
-
Calculating the materials usage variance Laura Pierre is the manager of the Roebuck Bagel Shop. The corporate office had budgeted her store to sell 3,000 ham sandwiches during the week beginning July...
-
II.On the basis of the following data for Cole Co. for the 2013 and 2012 prepare the statement of cash flows.Use the indirect method of reporting cash flows from operating activities. 2013 2012 Cash...
-
1. For the network a. Write the nodal equations for the networks in Figure given. b. Solve for the nodal voltages. c. Find the magnitude and polarity of the voltage across each resistor. R4 2 3 R3 |...
-
Find the effective (equivalent) annual rate (EFF%EAR) for semi-annually, quarterly, monthly, and daily compounded rates. Assume Inom=6% EARannual, EARsemiannually, EARquarterlly, EARmonthly, EARdailly
-
Requirement 1. What is the expected net cash inflow per year from purchasing a second shrink-wrap machine (i.e., how much cost could be saved each year by ell The expected net cash inflow (cost...
-
The exchange rate between euros and dollars is currently 0.83 euros per dollar. Inflation is expected to be 1% in Europe and 2% in the US. If relative purchasing power parity holds, what is the...
-
Given f(x)=g(x)h(x), g(2)=-1 g ' (x)=3 h(2)=-2 h ' (2)=4 Find f'(2)
-
A small electrical motor inside a toy truck moves 4 wheels each 0.20 kg at a speed of 3.3 m/s for 2.1. What is the power put out by the motor? 0.032W 0.11 W 0.017 W 2.1 W
-
1. 2. The lessee makes a lease payment of $67,200 to the lessor for equipment in an operating lease transaction. Pharoah Company leases equipment from Noble Construction Inc. The present value of the...
-
1. You have the following cash flow stream expected over the next ten years: Year CashFlow 1,2 250,000 3,4,5 300,000 6,7 325,000 8 250,000 9 200,000 10 150,000 Given the cash flow stream would you...
-
Which of the followingcarbocations is the least stable? CH3CH2 . CH3CHCH3 CH3 I . CH3C0 T CH3 IV. V. CH3 CH3CCH2 CH3
-
The table shows the average sales S (in millions of dollars) of an outerwear manufacturer for each month t, where t = 1 corresponds to January. (a) Create a scatter plot of the data. (b) Find a...
-
Use the functions f(x) = x + 4 and g(x) = 2x - 5 to find the function. 1. g-1 f-1 2. f-1 g-1
-
find the equation of the parabola y = ax2 + bx + c that passes through the points. To verify your result, use a graphing utility to plot the points and graph the parabolas. 1. 2. (2, 5) -4 4 (1, -2)...
-
Why is it important to create a final project report, presentation, and lessons-learned report?
-
Compare the lessons-learned template on the companion website and the one available from Microsoft as shown in Figure 8-6. Search for at least one other example of lessons-learned report or template....
-
Answering questions such as, What will you do differently on the next project based on your experience working on this project? is part of a __________. A. lessons-learned report B. customer...
Study smarter with the SolutionInn App