-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplusMinus.cpp
More file actions
34 lines (29 loc) · 1.08 KB
/
plusMinus.cpp
File metadata and controls
34 lines (29 loc) · 1.08 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
#include <vector>
#include <iostream>
using namespace std;
void plusMinus(vector<int> arr) {
auto arraySize = float (arr.size());
float positiveValues = 0;
float negativeValues = 0;
float zeroValues = 0;
for (int i = 0; i < int (arraySize); i++) {
if (arr[i] < 0) {
negativeValues++;
} else if (arr[i] > 0) {
positiveValues++;
} else {
zeroValues++;
}
}
auto zeroRatio = float(zeroValues / arraySize);
auto positiveRatio = float(positiveValues / arraySize);
auto negativeRatio = float(negativeValues / arraySize);
cout << positiveRatio << endl;
cout << negativeRatio << endl;
cout << zeroRatio;
}
// Info: TestCases Worked
/*
Given an array of integers, calculate the ratios of its elements that are positive, negative, and zero. Print the decimal value of each fraction on a new line with places after the decimal.
Note: This challenge introduces precision problems. The test cases are scaled to six decimal places, though answers with absolute error of up to are acceptable.
*/