#include <iostream>
#include <algorithm>
usingnamespace std;
int main()
{
int n,t=0;
cin >> n;
int* a = newint[n];
for (int i = 0; i < n; i++)
cin >> a[i];
for (int i = 0; i < n; i++)
for (int j = i + 1; i < n - 1; j++)
{
if (a[i] > a[j])
{
t = a[i];
a[j] = t;
a[i] = a[j];
}
}
sort(a, a + n);
for (int i = 0; i < n; i++)
cout << a[i] << " ";
delete a;
return 0;
}
thanks for your attention and i did all the things u mentioned above and im still getting the error on line 17 on run time
Exception thrown: read access violation.
a was 0x21C0112.
also im getting this 3 warnings before i run it
1- Reading invalid data from 'a': the readable size is 'n*4' bytes, but '8' bytes may be read.
2- Buffer overrun while writing to 'a': the writable size is 'n*4' bytes, but '8' bytes might be written
3-'a' is allocated with array new [], but deleted with scalar delete.
#include <iostream>
usingnamespace std;
int main()
{
int n,t=0;
cin >> n;
int* a = newint[n];
for (int i = 0; i < n; i++)
cin >> a[i];
for (int i = 0; i < n; i++)
for (int j = i + 1; i < n; j++)
{
if (a[i] > a[j])
{
t = a[i];
a[j] = t;
a[i] = a[j];
}
}
for (int i = 0; i < n; i++)
cout << a[i] << " ";
delete a;
return 0;
}
ah sorry how stupid i was :D , now it works fine and about the other thread the code wasn't clean after i edited it and since im new to post threads i couldn't either delete it so i just marked it as solved :) also thanks to your response