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
}
}