program writing to memory after the end of the heap

hey guys i wrote this code and it keeps giving me an error which says that the program is writing to memory after the end of the heap. how can i fix this problem? thank you.
here is the code:
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <iostream>
#include <ctime>
using namespace std;

void printarray (double A[], int n){

	int i=0;
	
	while (i<n){
	
		cout<<A[i]<<' ';
		i++;
	}

	return;
}
int main () {
	
	int n;
	int i=0;
	int j=0;

	double a=0;
	double *x;
	double temp=0;
	
	srand (time(0));
	
	cout<<"please enter the size of your first array of abscissas"<<endl;
	cin>>n;
	
	x = new double (n);

	while (i<n){

		a = rand ()% 101;
		x[i]=a;
		i++;
	}
	while (j<n){

		int k=j+1;

		while (k<n){ 

			if(x[k]<x[j]){
			
				temp = x[j];
				x[j]=x[k];
				x[k]=temp;
			}

			k++;
		}

		j++;
	} 

	printarray (x,n);

	delete x;
	x=NULL;

	return 0;
}
Line 32:
x = new double (n);

This doesn't create an array of doubles of size n, instead it creates a single double with the value n. You need [ ] instead of ( ).
thank you a lot :)
Topic archived. No new replies allowed.