Wierd output after sorting

I am getting weird output after sorting

If giving input using scanf the line is causing error. The output is in some weird arrangement. (I have commented the line)

If I use cin the output is fine. Also the problem is not present in online compilers. Same thing is happening on different computers.

Eg if I input

5
23 44 32 2 233

the output is

32 23 233 2 44

Code:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <iomanip>


using namespace std;

int main() {

    unsigned long long int n=0,i=0;

    //cin>>n;
    scanf("%llu",&n);

    unsigned long long int arr[n];

    for(i=0;i<n;i++){
      //cin>>arr[i]; //if use this no error but if use next line it is
      scanf("%llu",&arr[i]); //causing error
    }

    sort(arr,arr+n);

    for(i=0;i<n;i++){
//      cout<<arr[i]<<" ";
printf("%llu ",arr[i]);
    }
return 0;
}


Source: c++

Leave a Reply