SIGSEGV on Chandu

Can someone help me in this problem : https://www.hackerearth.com/practice/algorithms/sorting/merge-sort/practice-problems/algorithm/chandu-and-his-girlfriend/

My code : https://pastebin.com/4sFW0PzN

This gives SIGSEGV, someone please help me find the mistake in my code. @ssjgz @vijju1231
Thank you.

You’re initialising the array with size n before inputting n.

1 Like

Oh okay, isn’t that correct?
New code : https://pastebin.com/qAvbakxn
But still doesn’t work…

#include<bits/stdc++.h>
using namespace std;
 
int main(){
    int T;
    scanf("%d",T);
    for (int i=0; i<T; i++){
        int N,A[N];
        scanf("%d",&N);
        for (int i=0; i<N; i++){
            cin >> A[i];
        }
        sort(A,A+N);
        for (int i=0; i<N; i++){
            cout << A[i];
        }
    }

Correction

#include<bits/stdc++.h>
using namespace std;
 
int main(){
    int T;
    scanf("%d",T);
    for (int i=0; i<T; i++){
        int N;
        scanf("%d",&N);
        int A[n];
        for (int i=0; i<N; i++){
            cin >> A[i];
        }
        sort(A,A+N);
        for (int i=0; i<N; i++){
            cout << A[i];
        }
    }

Do you see why?

It still doesn’t work…

#include<bits/stdc++.h>
#include<iostream>
using namespace std;

int main(){
    int T;
    scanf("%d",&T);
    for(int i=0;i<T;i++){
        int N;
        scanf("%d",&N);
        int A[N]; //create array only after knowing N
        for (int i=0; i<N; i++){
            cin >> A[i];
        }
        sort(A,A+N);
        for (int i=N-1; i>=0; i--){ //decreasing order
            cout << A[i]<<" ";//space between numbers
        }
        cout<<'\n';//new line per test case
    }
}

Works, thanks a lot !