Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

 

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 = 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)

Step by Step Solution

3.38 Rating (164 Votes )

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Algebra and Trigonometry

Authors: Ron Larson

10th edition

9781337514255, 1337271179, 133751425X, 978-1337271172

More Books

Students also viewed these Programming questions