Answer to Question #322714 in C++ for Program Training

Question #322714

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 





1
Expert's answer
2022-04-02T19:08:19-0400
#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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS