-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMINGUEZ_PE05.cpp
137 lines (121 loc) · 4.23 KB
/
MINGUEZ_PE05.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
135
136
137
// Errol James Minguez
// BSCS - 1
// CMSC 28 (K) - Programming Exercise 05
// This is a program that asks the user to input various numbers (positive or negative) and perform basic statistics on these numbers. Find the largest, smallest, average, standard deviation, and variance of the numbers inputted.
#include <iostream>
using namespace std;
// Function Prototype
void description();
void displayResult(int max, int min, float ave, float sDev, float variance);
float sqrtNum(float num);
int findMax(int num[], int length);
int findMin(int num[], int length);
float average(int num[], int length);
float sDev(int num[], int length, float ave);
float var(float sdev);
// Function Definitions
// Function to display the description of the program
void description() {
system("cls");
cout << "=====================================================" << endl;
cout << "\t\tProgramming Exercise 05" << endl;
cout << "\t Submitted by: Errol James Minguez" << endl;
cout << "=====================================================" << endl;
cout << "\tThis program will ask the user to input \n various numbers (positive or negative but not zero) \n\t and perform basic statistics \n\t\t on these numbers" << endl;
cout << "\t DISCLAIMER! INPUT INTEGER ONLY!" << endl;
cout << "=====================================================" << endl;
}
// Function to display the results
void displayResult(int max, int min, float ave, float sDev, float variance){
cout << "=====================================================" << endl;
cout << "Largest: \t\t" << max << endl;
cout << "Smallest: \t\t" << min << endl;
cout << "Average: \t\t" << ave << endl;
cout << "Standard Deviation: \t" << sDev << endl;
cout << "Variance: \t\t" << variance << endl;
cout << "=====================================================" << endl;
}
// Babylonian method to find the square root of a number
float sqrtNum(float num) {
float x = num;
float y = 1;
float e = 0.000001; // precision
while (x - y > e) {
x = (x + y) / 2;
y = num / x;
}
return x;
}
// Find the largest number in the array
int findMax(int num[], int length) {
int temp = num[0];
for (int i = 0; i < length; i++) {
if (num[i] > temp) {
temp = num[i];
}
if (num[i] == 0) {
break;
}
}
return temp;
}
// find the smallest number in the array
int findMin(int num[], int length) {
int temp = num[0];
for (int i = 0; i < length; i++) {
if (num[i] < temp) {
temp = num[i];
}
}
return temp;
}
// Find the average of the numbers in the array
float average(int num[], int length) {
float temp;
for (int i = 0; i < length; i++) {
temp = temp + num[i];
}
temp = temp / length;
return temp;
}
// Find the standard deviation of the numbers in the array
float sDev(int num[], int length, float ave) {
float total = num[0], temp, temp2, temp3, temp4, temp5;
for (int i = 1; i < length; i++) {
total = total + num[i];
}
for (int j = 0; j < length; j++) {
temp = num[j] - ave;
temp2 = temp * temp;
temp3 = temp3 + temp2;
}
temp4 = temp3 / (length -1);
temp5 = sqrtNum(temp4);
return temp5;
}
// Find the variance of the numbers in the array
float var(float sdev) {
return sdev * sdev;
}
// Main function
int main () {
int arrayNum[10], max, min, arrayLength;
float ave, standardDev, vari;
arrayLength = sizeof(arrayNum) / sizeof(arrayNum[0]); // get the length of the array
description();
for (int i = 0; i < 10; i++) { // loop to input the numbers
cout << "Input an integer: ";
cin >> arrayNum[i];
if (arrayNum[i] == 0) {
arrayLength = i;
break;
}
}
max = findMax(arrayNum, arrayLength);
min = findMin(arrayNum, arrayLength);
ave = average(arrayNum, arrayLength);
standardDev = sDev(arrayNum, arrayLength, ave);
vari = var(standardDev);
displayResult(max, min, ave, standardDev, vari); // display the results
return 0;
}