More Related Content Similar to 02_variance.pdf Similar to 02_variance.pdf (20) More from Emertxe Information Technologies Pvt Ltd More from Emertxe Information Technologies Pvt Ltd (20) 02_variance.pdf5. Assignment 2
WAP to perform variance calculation with dynamic arrays.
Input: Read array size ‘N’ and read array elements of size ‘N’
6. Assignment 2
WAP to perform variance calculation with dynamic arrays.
Input: Read array size ‘N’ and read array elements of size ‘N’
Output: Print the variance
13. Assignment 2
Example :-
Step 1 : Read size ‘N’
N = 7
Step 2 : Dynamically allocate the memory for array ‘arr’ of size ‘N’
Step 3 : Read ‘N’ number of array elements
14. Assignment 2
Example :-
Step 1 : Read size ‘N’
N = 7
Step 2 : Dynamically allocate the memory for array ‘arr’ of size ‘N’
Step 3 : Read ‘N’ number of array elements
4 2 7 1 5 3 6
arr
28. Assignment 2
Example :-
Step 5 : Calculate Deviation (D)
D[i] = arr[i] – Mean
D[1] = 2 – 4 = -2
4 2 7 1 5 3 6
0 1 2 3 4 5 6
i
0 -2
D
arr
31. Assignment 2
Example :-
Step 5 : Calculate Deviation (D)
D[i] = arr[i] – Mean
D[2] = 7 – 4 = 3
4 2 7 1 5 3 6
0 1 2 3 4 5 6
i
0 -2 3
D
arr
33. Assignment 2
Example :-
Step 5 : Calculate Deviation (D)
D[i] = arr[i] – Mean
D[3] = 1 – 4 = -3
4 2 7 1 5 3 6
0 1 2 3 4 5 6
i
0 -2 3
D
arr
34. Assignment 2
Example :-
Step 5 : Calculate Deviation (D)
D[i] = arr[i] – Mean
D[3] = 1 – 4 = -3
4 2 7 1 5 3 6
0 1 2 3 4 5 6
i
0 -2 3 -3
D
arr
36. Assignment 2
Example :-
Step 5 : Calculate Deviation (D)
D[i] = arr[i] – Mean
D[4] = 5 – 4 = 1
4 2 7 1 5 3 6
0 1 2 3 4 5 6
i
0 -2 3 -3
D
arr
37. Assignment 2
Example :-
Step 5 : Calculate Deviation (D)
D[i] = arr[i] – Mean
D[4] = 5 – 4 = 1
4 2 7 1 5 3 6
0 1 2 3 4 5 6
i
0 -2 3 -3 1
D
arr
39. Assignment 2
Example :-
Step 5 : Calculate Deviation (D)
D[i] = arr[i] – Mean
D[5] = 3 – 4 = -1
4 2 7 1 5 3 6
0 1 2 3 4 5 6
i
0 -2 3 -3 1
D
arr
40. Assignment 2
Example :-
Step 5 : Calculate Deviation (D)
D[i] = arr[i] – Mean
D[5] = 3 – 4 = -1
4 2 7 1 5 3 6
0 1 2 3 4 5 6
i
0 -2 3 -3 1 -1
D
arr
42. Assignment 2
Example :-
Step 5 : Calculate Deviation (D)
D[i] = arr[i] – Mean
D[6] = 6 – 4 = 2
4 2 7 1 5 3 6
0 1 2 3 4 5 6
i
0 -2 3 -3 1 -1
D
arr
43. Assignment 2
Example :-
Step 5 : Calculate Deviation (D)
D[i] = arr[i] – Mean
D[6] = 6 – 4 = 2
4 2 7 1 5 3 6
0 1 2 3 4 5 6
i
0 -2 3 -3 1 -1 2
D
arr
47. Assignment 2
Example :-
Step 6 : Calculate square of Deviation and store in ‘D_sqr’ array
D_sqr[0] = (0) * (0)
0 -2 3 -3 1 -1 2
0 1 2 3 4 5 6
D
i
48. Assignment 2
Example :-
Step 6 : Calculate square of Deviation and store in ‘D_sqr’ array
D_sqr[0] = (0) * (0)
D_sqr[0] = 0
0 -2 3 -3 1 -1 2
0 1 2 3 4 5 6
D
i
49. Assignment 2
Example :-
Step 6 : Calculate square of Deviation and store in ‘D_sqr’ array
D_sqr[0] = (0) * (0)
D_sqr[0] = 0
0 -2 3 -3 1 -1 2
D_sqr
0 1 2 3 4 5 6
0
D
i
51. Assignment 2
Example :-
Step 6 : Calculate square of Deviation and store in ‘D_sqr’ array
D_sqr[1] = (-2) * (-2)
D_sqr[1] = 4
0 -2 3 -3 1 -1 2
0 1 2 3 4 5 6
0
D
i
D_sqr
52. Assignment 2
Example :-
Step 6 : Calculate square of Deviation and store in ‘D_sqr’ array
D_sqr[1] = (-2) * (-2)
D_sqr[1] = 4
0 -2 3 -3 1 -1 2
0 1 2 3 4 5 6
0 4
D
i
D_sqr
54. Assignment 2
Example :-
Step 6 : Calculate square of Deviation and store in ‘D_sqr’ array
D_sqr[2] = (3) * (3)
D_sqr[2] = 9
0 -2 3 -3 1 -1 2
0 1 2 3 4 5 6
0 4
D
i
D_sqr
55. Assignment 2
Example :-
Step 6 : Calculate square of Deviation and store in ‘D_sqr’ array
D_sqr[2] = (3) * (3)
D_sqr[2] = 9
0 -2 3 -3 1 -1 2
0 1 2 3 4 5 6
0 4 9
D
i
D_sqr
56. Assignment 2
Example :-
Step 6 : Calculate square of Deviation and store in ‘D_sqr’ array
0 -2 3 -3 1 -1 2
0 1 2 3 4 5 6
0 4 9
D
i
D_sqr
57. Assignment 2
Example :-
Step 6 : Calculate square of Deviation and store in ‘D_sqr’ array
D_sqr[3] = (-3) * (-3)
D_sqr[3] = 9
0 -2 3 -3 1 -1 2
0 1 2 3 4 5 6
0 4 9
D
i
D_sqr
58. Assignment 2
Example :-
Step 6 : Calculate square of Deviation and store in ‘D_sqr’ array
D_sqr[3] = (-3) * (-3)
D_sqr[3] = 9
0 -2 3 -3 1 -1 2
0 1 2 3 4 5 6
0 4 9 9
D
i
D_sqr
59. Assignment 2
Example :-
Step 6 : Calculate square of Deviation and store in ‘D_sqr’ array
0 -2 3 -3 1 -1 2
0 1 2 3 4 5 6
0 4 9 9
D
i
D_sqr
60. Assignment 2
Example :-
Step 6 : Calculate square of Deviation and store in ‘D_sqr’ array
D_sqr[4] = (1) * (1)
D_sqr[4] = 1
0 -2 3 -3 1 -1 2
0 1 2 3 4 5 6
0 4 9 9
D
i
D_sqr
61. Assignment 2
Example :-
Step 6 : Calculate square of Deviation and store in ‘D_sqr’ array
D_sqr[4] = (1) * (1)
D_sqr[4] = 1
0 -2 3 -3 1 -1 2
0 1 2 3 4 5 6
0 4 9 9 1
D
i
D_sqr
62. Assignment 2
Example :-
Step 6 : Calculate square of Deviation and store in ‘D_sqr’ array
0 -2 3 -3 1 -1 2
0 1 2 3 4 5 6
0 4 9 9 1
D
i
D_sqr
63. Assignment 2
Example :-
Step 6 : Calculate square of Deviation and store in ‘D_sqr’ array
D_sqr[5] = (-1) * (-1)
D_sqr[5] = 1
0 -2 3 -3 1 -1 2
0 1 2 3 4 5 6
0 4 9 9 1
D
i
D_sqr
64. Assignment 2
Example :-
Step 6 : Calculate square of Deviation and store in ‘D_sqr’ array
D_sqr[5] = (-1) * (-1)
D_sqr[5] = 1
0 -2 3 -3 1 -1 2
0 1 2 3 4 5 6
0 4 9 9 1 1
D
i
D_sqr
65. Assignment 2
Example :-
Step 6 : Calculate square of Deviation and store in ‘D_sqr’ array
0 -2 3 -3 1 -1 2
0 1 2 3 4 5 6
0 4 9 9 1 1
D
i
D_sqr
66. Assignment 2
Example :-
Step 6 : Calculate square of Deviation and store in ‘D_sqr’ array
D_sqr[6] = (2) * (2)
D_sqr[6] = 4
0 -2 3 -3 1 -1 2
0 1 2 3 4 5 6
0 4 9 9 1 1
D
i
D_sqr
67. Assignment 2
Example :-
Step 6 : Calculate square of Deviation and store in ‘D_sqr’ array
D_sqr[6] = (2) * (2)
D_sqr[6] = 4
0 -2 3 -3 1 -1 2
0 1 2 3 4 5 6
0 4 9 9 1 1 4
D
i
D_sqr
70. Assignment 2
Example :-
Step 7 : Calculate Mean for square of the Deviation ‘D_sqr’ array
Variance = sum of all elements in ‘D_sqr’ / size
0 4 9 9 1 1 4
0 1 2 3 4 5 6
D_sqr
71. Assignment 2
Example :-
Step 7 : Calculate Mean for square of the Deviation ‘D_sqr’ array
Variance = sum of all elements in ‘D_sqr’ / size
Variance = 28 / 7
0 4 9 9 1 1 4
0 1 2 3 4 5 6
D_sqr
72. Assignment 2
Example :-
Step 7 : Calculate Mean for square of the Deviation ‘D_sqr’ array
Variance = sum of all elements in ‘D_sqr’ / size
Variance = 28 / 7
Variance = 4.000000
0 4 9 9 1 1 4
0 1 2 3 4 5 6
D_sqr