Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Arrays Exercise 1: Using Arrays for Correlation Coefficient (10pt) Let us revisit Homework 2 - Exercise 2 and use arrays to calculate the correlation coefficient.

image text in transcribedimage text in transcribed

Arrays Exercise 1: Using Arrays for Correlation Coefficient (10pt) Let us revisit Homework 2 - Exercise 2 and use arrays to calculate the correlation coefficient. Save your program as Hw6_Ex1.java. Suppose we now have more data points than previously. Consider 10 data points as follows: Prices: 23.11 10.71 26.98 28.68 23.57 25.15 24.86 17.84 23.11 13.42 Sales: 262 428 185 162 217 237 230 325 267 382 Note that each price is paired with its matching sales volume in the same order. Recall that the correlation coefficient measures the linear correlation between two variables X and Y. It has a value between +1 and -1, where 1 is total positive linear correlation, O is no linear correlation, and -1 is total negative linear correlation. We want to find out the correlation coefficient between price (x) and sales quantity (Y). We can use the following formula to calculate the correlation coefficient r: Li-[(xi )(y; - 7)] r = _(x2 )2 J2X-36)) where i and are the mean such that N 21-1 yi N Now write a simple Java program to compute the correlation coefficients r given the 10 data points listed above, using arrays to hold these data. You may follow the steps below: 1) Define two arrays, say, x and y, to hold the prices and sales, respectively; initialize the arrays by typing in the numbers; (Note: enter the data in the same order as listed above so that prices and sales will be matched properly) 2) Calculate the averages, i and y; (Hint: use for loops) 3) Calculate the correlation coefficient using the formula provided above; (Hint: use for loops to calculate the sums in the numerator and the denominator) 4) Display the value of the correlation coefficient. Only display 4 digits after the decimal point. (Hint: use printf) Hint: Your output should look like the following: C. Command Prompt C:\test>javac Hw6_Ex1.java C:\test/java Hw6_Ex1 Correlation coefficient: -0.9824 C:\test). Exercise 2: Using Arrays with Methods: Exercise 1 Revisited (10pt) Re-do Exercise 1 by creating a method with arrays as its parameter variables and return value. Save your program as Hw6_Ex2.java. Create a method that: Takes two parameters: (1) an array for X, (2) an array for Y; Returns a double value, which is the correlation coefficient r between X and Y In your method, calculate r using the formulas given above. In your main method, perform the same tasks as in the previous homework: (i) (ii) Define two arrays to hold the prices and sales, and initialize them by typing in the numbers; Call the method you just created, passing in the two arrays as parameters, to calculate the correlation coefficient. Output the value of r (display 4 decimal places only). Your program should run as follows: C.S. Command Prompt C:\test>javac Hw6_Ex2.java C:\test>java Hw6_Ex2 Correlation Coefficient: -0.9874 C:\test)

Step by Step Solution

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

More Books

Students also viewed these Databases questions