Exception Thrown

Im trying to sort an array without using sort built-in function and i dont know why im getting this error ( using visual studio 2017)

Exception thrown: read access violation.
a was 0x1C70112.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream> 
#include <algorithm> 

using namespace std;

int main()
{
	int n,t=0;
	cin >> n;
	int* a = new int[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;

}
Have a very close look at the middle item of the for statement on line 16.

The second and third lines of your swapping block (currently lines 21 and 22) are the wrong way round.

Remove sort() if you want to test your own routine.
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.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
using namespace std;

int main()
{
	int n,t=0;
	cin >> n;
	int* a = new int[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;

}


Last edited on
abdallahosama wrote:
i did all the things u mentioned above

You didn't do either of the first two things that I mentioned.


Have a VERY close look at the centre of the for statement in this line. Currently (and wrongly):
for (int j = i + 1; i < n; j++)


I also requested that you reverse the second and third lines of this block; currently (and wrongly):
1
2
3
				t = a[i];
				a[j] = t;
				a[i] = a[j];

In fact @doug4 told you the same thing in your other thread (why did you start a new one?) and you ignored him also.
Last edited on
Thanks @lastchance for saying everything I wanted to say to the @abdallahosama.


@abdallahosama - If you are not going to incorporate suggestions and then wonder why your code still doesn't work, I won't bother responding any more.

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
@doug4 i apologize to you i really do but i couldn't handle the situation as i supposed to do
Topic archived. No new replies allowed.