-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path29_Variance_Calculation.c
More file actions
77 lines (71 loc) · 1.94 KB
/
Copy path29_Variance_Calculation.c
File metadata and controls
77 lines (71 loc) · 1.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/*
* Author: Girish Gaude
* Date: 19/Dec/2019
* Desciption: Program to do variance calculation with static arrays & with dynamic arrays
* Input: Enter array of N integer.
* Output: Display mean and sigma of given array.
*/
#include<stdio.h>
#include<stdlib.h>
int variance( int *array, int size) //Call function and pass base address and size
{
int x, sum = 0, sqr = 0;
int sigma=0; //Variable Declaration and Defination
for(int i=0; i<size; i++)
{
sum += array[i]; //Add all elements
}
for(int i=0; i<size; i++)
{
x = array[i] - (sum/size); //Calculate median
sqr = (x * x); //Square of median
sigma += sqr; //Add all Square median
}
return (sigma/size); //Return sigma value
}
int main()
{
char ch;
do
{
int opt,size,var; //Declare varible
printf("Please select the funtion.\n 1. Static Array\n 2. Dynamic Array\n");
scanf("%d",&opt); //Ask user for input
switch (opt)
{
case 1: //Static option
{
printf("Enter the Array size : \n");
scanf("%d",&size); //Ask array size
int array[size];
for (int i=0; i<size; i++)
{
scanf("%d", &array[i]); //Input array elent
}
var = variance(array, size); //Call function
printf("The Variance of the entered numbers is %d\n", var);
break;;
}
case 2: //Dynamic option
{
printf("Enter the Array size : \n");
scanf("%d",&size); //Ask for size
int *array = malloc(size*sizeof(int)); //malloc
for (int i=0; i<size; i++)
{
scanf("%d", array+i); //Fill array elements
}
var = variance(array, size); //Call function
printf("The Variance of the entered numbers is %d\n", var);
free(array); //Free malloc
break;;
}
default:
printf("ERROR : Please enter valid Input\n"); //Error
}
printf("Do you want to continue [y/n].\n");
getchar();
scanf("%c", &ch); //Continue again
}while(ch=='y');
return 0;
}