-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsd-freq.cpp
54 lines (49 loc) · 1.71 KB
/
sd-freq.cpp
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
#include <iostream>
#include <cmath>
using namespace std;
void calculateSD(int n, int seriesType) {
double sumfx = 0, sumfxsqr = 0, N = 0, sd = 0;
float f, first, interval, x; // Unified variables for both series types
if (seriesType==1)
{
cout << "Enter the first variable: ";
cin >> first;
}
else
{
cout << "Enter the lower limit of first class: ";
cin >> first;
}
cout << "Enter the interval : ";
cin >> interval;
for (int i = 0; i < n; i++) {
if (seriesType == 1) { // Discrete Series
x = first; // Set the current variable
cout << "Enter frequency for variable " << x << ": ";
} else if (seriesType == 2) { // Continuous Series
float l = first; // Lower limit
float u = first + interval; // Upper limit
x = (l + u) / 2; // Midpoint calculation
cout << "Enter frequency for class " << i + 1 << " (" << l << " - " << u << "): ";
} else {
cout << "Invalid Series Type!" << endl;
return; // Exit if invalid type
}
cin >> f; // Input frequency
sumfx += f * x;
sumfxsqr += f * pow(x, 2);
N += f;
first += interval; // Increment the first variable by the interval
}
sd = sqrt((sumfxsqr / N) - pow((sumfx / N), 2));
cout << "Standard Deviation: " << sd << endl;
}
int main() {
int n, option;
cout << "Choose series type:\n1. Discrete Series\n2. Continuous Series\nEnter choice: ";
cin >> option;
cout << "Enter the number of elements or classes in the series: ";
cin >> n;
calculateSD(n, option);
return 0;
}