When analyzing data sets, such as data for human heights or for human weights, a common step is to adjust the data. This adjustment can be done by normalizing to values between 0 and 1, or throwing away outliers.
For this program, adjust the values by dividing all values by the largest value. The input begins with an integer indicating the number of floating-point values that follow.
Output each floating-point value with two digits after the decimal point, which can be achieved by executing
cout << fixed << setprecision(2); once before all other cout statements.
Ex: If the input is:
5 30.0 50.0 10.0 100.0 65.0
the output is:
0.30 0.50 0.10 1.00 0.65
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
int n;
double *arr;
double max_val=0.0;
cin >> n;
arr = new double[n];
for (int i=0; i<n; i++) {
cin >> arr[i];
if (arr[i] > max_val) {
max_val = arr[i];
}
}
cout << fixed << setprecision(2);
for (int i=0; i<n; i++) {
arr[i] /= max_val;
cout << arr[i] << " ";
}
delete [] arr;
return 0;
}
Comments
Leave a comment