简体   繁体   中英

Finding min max in C++

My exercise is as follows: Given a list of N integers, find its mean (as a double), maximum value, minimum value, and range. Your program will first ask for N, the number of integers in the list, which the user will input. Then the user will input N more numbers.

Here is my code:

#include "stdafx.h"
#include <iostream>
using namespace std;

int main()
{
    int n;
    cout << "Input n: ";
    cin >> n;

    int first;             //**LINE NUMBER 1**
    cout << "No 1 is: ";
    cin >> first;
    int max = first;
    int min = first;

    for (int i=1; i<n;i++) //**LINE NUMBER 2**
    {
        int x;
        cout << "No " << i+1 << " is: ";
        cin >> x;
        first = x;         //**LINE NUMBER 3**
        if (max < x)
        {
           max = x;
        }
        if (min > x)
        {
           min = x;
        }
    }
    cout << "max is: " << max << endl;
    cout << "min is: " << min << endl;
    cout << "mean is: " << (double) (max+min)/2 << endl; //**LINE NUMBER 4**
    cout << "range is: " << max - min << endl;

    return 0;
}

However, the solution is a bit different:

  • For line number 1, it is int first=0; instead of int first;

  • For line number 2, it is for (int i=1; i<n;++i) instead of for (int i=1; i<n;i++)

  • For line number 3, it is first += x; instead of first = x;

  • For line number 4, it is cout << "mean is: " << (double) first/n<< endl; instead of cout << "mean is: " << (double) (max+min)/2 << endl;

I tried some examples but both codes produce exactly the same results. Why is that? Or am I wrong somewhere?

Well:

None of the lines are, per se, WRONG. More so, they are just preferably done another way. ie:

  • int first =0 just ensures that first == 0 to begin with. int first; can cause problems because it is assigned, initially to an arbitrary number (whatever happens to be at the memory address that the variable points to)

  • although ++i and i++ work the same way in for loops as an iterator, they have a very slight difference in other usages. read more about that here

  • first += x is essentially first = first + x which more clearly outlines the difference between your code and it. This also makes it very clear why int first = 0 is so important. If first was some random value, then the sum of all the numbers (which is what first is supposed to be) would be randomly assigned to start at a number, not always 0.

  • this last one is the only one where you are, in fact, wrong. the mean is defined as the sum of all your data divided by the number of data points (same as average ) where as the median is described as the middle point of the data, or the number that describes the middle point between the max and min of your data. Where you are calculating what you think is the mean is actually the median . The intricacy of this is that sometimes, depending on your data, the mean = median . For example, if your data is 3,4,5 then both the mean and median is equal to 4

I hope this clarifies things for you :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM