-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathassn1.cpp
134 lines (113 loc) · 2.48 KB
/
assn1.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
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
//
// assn1.cpp
// assn1
//
// Created by Thomas on 2018-09-20.
// Copyright © 2018 Thomas. All rights reserved.
//
#include <iostream>
void selectionSort(double arr[], int sz) {
for (int x = 0; x < sz - 1; x++) {
int min = x;
for (int y = x + 1; y < sz; y++) {
if (arr[y] < arr[min]) min = y;
}
if (min != x) {
double temp = arr[min];
arr[min] = arr[x];
arr[x] = temp;
}
}
}
double calcAverage(char * values[], int sz)
{
double sum = 0, average = 0;
double *arr = nullptr;
arr = new double[sz];
for (int x = 2; x < sz; x++)
{
arr[x] = atof(values[x]);
}
for (int x = 2; x < sz; x++)
{
sum += arr[x];
}
average = sum / (sz-2);
std::cout << "Average" << std::endl;
for (int x = 2; x < sz; x++)
{
std::cout << arr[x] << " ";
}
std::cout << std::endl << average << std::endl;
return average;
}
double calcMedian(char * values[], int sz)
{
double median = 0;
double *arr = nullptr;
arr = new double[sz];
for (int x = 2; x < sz; x++)
{
arr[x] = atof(values[x]);
}
selectionSort(arr, sz);
if (sz % 2 == 0)
{
median = *(arr + ((sz/2))) + *(arr + ((sz/2)+1));
median = median / 2;
}
if (sz % 2 == 1)
{
median = *(arr + ((sz/2)+1));
}
std::cout << "median" << std::endl;
for (int x = 2; x < sz; x++)
{
std::cout << arr[x] << " ";
}
std::cout << std::endl;
std::cout << median << std::endl;
return median;
}
double calcSum(char * values[])
{
double sum = 0;
double *arr = nullptr;
arr = new double[3];
for (int x = 2; x < 5; x++)
{
arr[x] = atof(values[x]);
}
for (int x = 2; x < 5; x++)
{
sum += arr[x];
}
std::cout << "Sum" << std::endl;
for (int x = 2; x < 5; x++)
{
std::cout << arr[x] << " ";
}
std::cout << std::endl;
std::cout << sum << std::endl;
return sum;
}
int main(int argc, char * argv[]) {
switch (*argv[1])
{
case 'A':
case 'a':
calcAverage(argv, argc);
break;
case 'M':
case 'm':
calcMedian(argv, argc);
break;
case 'F':
case 'f':
calcSum(argv);
break;
default:
break;
}
return 0;
}