2

I am new to coding and need to know how to display the Max, Min, Avg, and the new normalized values of my array. I have the max min and avg working correctly as of now, but am very confused on how i would have my function display the proper new values of the array. The point is to use the function; xi'=(xi-min)/(max-min) My current code is the following:

#include <iostream>
using namespace std;

void normalizeMinMaxAvg(double data[], int size,double& min, double& max, double& avg)
{
    max = 0;
    min = 0;
    int sum = 0;
    int i;
    avg = 0;

    for (i = 0; i < size; i++) {
        sum += data[i];
        if ( max < data[i]) {
            max = data[i];
        }
        if (min > data[i]) {
            min = data[i];
        }
    }
    avg = sum/size;

    for (i = 0; i < size; i++){
        data[i] = (data[i]-min)/(max-min);
    }
}


int main() 
{
    double data[] = {-10.0,0.0,20.0,30.0,500.0};
    double min, max, avg;

    normalizeMinMaxAvg (data, 5, min, max, avg);
    cout << "min = " << min << "\n";
    cout << "max = " << max << "\n";
    cout << "avg = " << avg << "\n";
    cout << data;
    return 0;
}
7
  • 2
    Don't use a raw array, but std::array or std::vector instead. Commented Jul 7, 2017 at 18:51
  • Strong chance this is school work where those would not be allowed... yet. Commented Jul 7, 2017 at 18:51
  • @MichaelDorgan is correct we aren't supposed to be using those yet Commented Jul 7, 2017 at 18:53
  • arrays are not passed by value. Commented Jul 7, 2017 at 18:53
  • 2
    One little gotcha above - what happens if min and max are the same? Commented Jul 7, 2017 at 18:57

3 Answers 3

2

Your code is correctly updating the array values. But to display the array values properly use for loop like this:

int arrSize = sizeof(data)/sizeof(data[0]);
for(int i =0; i<arrSize; ++i)
    cout << data[i]<<" ";

Alternatively, you can use the vector instead of a raw array like this:

vector<double> data ={-10.0,0.0,20.0,30.0,500.0};

And pass it by reference to your function.

Sign up to request clarification or add additional context in comments.

5 Comments

Why not simply for(auto val: data)?
Totally agree, but question is tagged under c++ not c++11
c++11 is the current standard. So why the distinction?
@HemantGangwar I think you can assume at least c++14. If the question is tagged C++98, then give a C++98 answer.
@GuillaumeRacicot Thanks Man will keep this in mind while answering next question.
1

If you want to "print out" your array you need to iterate over each member as you have already done several times:

for (int i = 0; i < array_size; i++){
    cout << data[i] << "\n";
}

What you are doing currently:

cout << data;

Will simply print the address of the array.

2 Comments

Why not simply for(auto val: data)?
@user0042 This depends on the version of c++. Either is fine.
0

data only holds the address of the array, which, in your case, is a pointer to a double, you need iterate the array and print out each value individually.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.