1

Given n numbers, find maximum difference between some two of them. For example, for input data 1 2 7 -6 we have 13=7-(-6). But my code doesn't seem output correct results: 

#include <iostream>
#include <algorithm>
int main()
{
    int n, j, k;
    std::cin >> n;
    int *a;
    a = new int[n];

    for (j=0; j<n; j++)
    {
        std::cin >> a[j];
    }

   std::sort(a, a + sizeof(int));


    k=a[n-1]-a[0];
    std::cout << k;

    delete [] a;

    return 0;
}
amdixon
  • 3,814
  • 8
  • 25
  • 34
  • `std::sort(a, a + sizeof(int));` is sorting only the first `sizeof(int)` (which should be 4) elements. – Abstraction Dec 11 '15 at 22:07
  • What results *does* your code give? – Wai Ha Lee Dec 12 '15 at 00:28
  • @Abstraction: _"which should be 4"_ I beg to differ. 8 is now a reasonable value for `sizeof(int)`. Furthermore, there appear to be numerous Indian software developers still using 1980s Turbo C++ in a DOS emulator for no discernible reason, for whom `sizeof(int)` is 2 _at best_. – Lightness Races in Orbit Dec 12 '15 at 15:37

2 Answers2

0

As Olivier answered, your mistake is in the call to std::sort(), but I want to point out that unless this is mandatory for your task (it may be part of an homework), You don't need to sort the array nor you need an array too.

#include <iostream>
#include <climits>

int main() {
    int n, j, a;
    int max = INT_MIN;
    int min = INT_MAX;

    if ( std::cin >> n  &&  n > 0 ) {
        while ( j < n  &&  std::cin >> a ) {
            if ( a < min ) min = a;
            if ( a > max ) max = a;
            j++;
        }    
        std::cout << max - min;
    }
    return 0;
}
Bob__
  • 12,361
  • 3
  • 28
  • 42
-1

Your problem is when you call std::sort

You do:

std::sort(a, a + sizeof(int));

So you actually only sort 4 elements (sizeof(int)=4 most of the time). For correct result you could do:

std::sort(a, a+n); (better)

or

std::sort(a, &a[n]);
  • `sizeof(int)=4` That's an unfounded assumption. – Lightness Races in Orbit Dec 12 '15 at 15:39
  • 1
    `&a[n]` No, that has undefined behaviour. `a+n` is correct. Ping me when corrected. – Lightness Races in Orbit Dec 12 '15 at 15:40
  • sizeof(int)=4 is most of the time true. I say that for illustrating the problem. I didn't take that assumption in the code – Olivier Pellier-Cuit Dec 12 '15 at 15:43
  • &a[n] is perfectly correct and for a beginner easiest to understand than a+n. At the end the generated code will be strictly the same – Olivier Pellier-Cuit Dec 12 '15 at 15:45
  • 2
    You are mistaken. `&a[n]` has undefined behaviour, as I said already. Teaching it, especially to a beginner, is grossly irresponsible. _"At the end the generated code will be strictly the same."_ No, this is another unfounded assumption. – Lightness Races in Orbit Dec 12 '15 at 15:46
  • If you have an array for example int array[10]. array[3] is equivalent to (*(array+3)). So &array[3] is equivalent to &(*(array+3)) => (array+3). Try at least to explain your point "irresponsible" is not a demonstration. – Olivier Pellier-Cuit Dec 12 '15 at 15:55
  • If only life were as simple as your own not-backed-up assertion makes out. `&*` "strictly cancel out" in C99 but not in C++. It is UB. This is well-documented (although unfortunately you have to sift through a lot of misinformation, which is one reason why I'm so strongly trying to stop you from adding to it); at the moment, `&array[3]` is only equivalent to `&*(array+3)` _if that is an element in the array_. There is an open issue to make this well-defined in the standard. e.g. http://stackoverflow.com/a/3144917/560648 Until then, it's _terrible_ advice to recommend its use. – Lightness Races in Orbit Dec 12 '15 at 15:58
  • From the linked answer: "If there's a single compiler for which it actually doesn't work, nobody in any of the arguments has been able to find or cite it." It's pedantry. – Don Reba Dec 12 '15 at 16:06
  • 1
    I am agree with you that (a+n) is better than &a[n] and i will modify my response in that sense, But this is working perfectly with a C array. And understanding why &a[n] is equivalent to (a+n) for C array is in my opinion needed if you want to be a good low level programmer – Olivier Pellier-Cuit Dec 12 '15 at 16:09