forked from Learning-org-69/Learning-PRs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstats.cpp
More file actions
101 lines (76 loc) · 2 KB
/
stats.cpp
File metadata and controls
101 lines (76 loc) · 2 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/*
Basic statistics of a set of numbers
Author: Srikar
*/
#include<iostream>
#include<cmath>
#include<fstream>
#include<string>
using namespace std;
// Global array for conviniece. Max elements = 100 as per requirements
float nums[100];
int main()
{
// Function prototypes
void bubbleSort(float[], int);
int readFile(void);
int num_inputs = readFile();
float avg = 0, std = 0, median = 0, temp_sum = 0;
// Finding the average
for(int i = 0; i < num_inputs; i++)
avg += nums[i];
avg /= num_inputs;
cout<<endl<<"Average = \t\t"<<avg<<endl;
// Standard deviation
for(int i = 0; i < num_inputs; i++)
temp_sum += pow((nums[i] - avg), 2);
temp_sum /= num_inputs - 1;
std = sqrt(temp_sum);
cout<<"Std deviation = \t"<<std<<endl;
// Median
// Bubble sorting elements. Time complexity not an issue here
bubbleSort(nums, num_inputs);
// Even number of elements
if(num_inputs % 2 == 0)
median = (nums[num_inputs / 2 - 1] + nums[num_inputs / 2 ]) / 2;
else
median = nums[num_inputs / 2];
cout<<"Median = \t\t"<<median<<endl;
return(0);
}
// Function to swap elements
void swap(float *x, float *y)
{
float temp = *x;
*x = *y;
*y = temp;
}
// Function to bubble sort the array
void bubbleSort(float nums[], int num_inputs)
{
for (int i = 0; i < num_inputs - 1; i++)
for (int j = 0; j < num_inputs - i - 1; j++)
if (nums[j] > nums[j + 1])
swap(&nums[j], &nums[j +1 ]);
}
// Function to read from file and assign values in the array
int readFile()
{
fstream fio; // File stream
string line; // string to store the line read from file
fio.open("input.txt", ios::in | ios::out);
// Execute a loop until EOF (End of File)
// point read pointer at beginning of file
fio.seekg(0, ios::beg);
int num = 0; // Number of lines, i.e, number of elements
while (fio)
{
// Read a Line from File
getline(fio, line);
// Converting read string to float
nums[num++] = stof(line);
}
// Close the file
fio.close();
return num - 1; //Since we incremement once post the loop closure
}