Help with C++ code for Run Time Analysis

I keep getting a Access Violation error in visual studio when i run this code. It stops at "A[i] = a / (i + 1);" Thank You.

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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include <iostream>
#include <time.h>
using namespace std;

double* prefixAverage1(int X[], int n);
double* prefixAverage2(int X[], int n);
int p1(int x, int n);
int p2(int x, int n);

const int MAX = 3;
int main()
{
	clock_t start, stop;
	int num = 0;
	

	int A[MAX] = { 0 };
	int X[MAX] = { 0 };

	for (int i = 0; i < MAX; i++)
	{
		A[i] = i + 1;
	}
	for (int i = 0; i < MAX; i++)
	{
		X[i] = i + 1;
	}

	start = clock();
	prefixAverage1(X, MAX);
	stop = clock();

	
	cout << "Running time for prefixAverage1 is " << static_cast<double>(stop - start) / (CLOCKS_PER_SEC) << endl;
	cout << endl;

	start = clock();
	prefixAverage2(X, MAX);
	stop = clock();

	
	cout << "Running time for prefixAverag2e is " << static_cast<double>(stop - start) / (CLOCKS_PER_SEC) << endl;
	cout << endl;

	cout << "Enter your base number" << endl;
	cin >> num;
	start = clock();
	p1(num, MAX);
	stop = clock();
	
	cout << "Running time for p1 is " << static_cast<double>(stop - start) / (CLOCKS_PER_SEC) << endl;

	start = clock();
	p2(num,MAX);
	stop = clock();

	cout << "Running time for p2 is " << static_cast<double>(stop - start) / (CLOCKS_PER_SEC) << endl;

	system("pause"); //holds output screen
}

double* prefixAverage1(int X[], int n)
{
	double *A = new double[n];
	for (int i = 0; i < n; i++)
	{
		int a = 0;
		for (int j = 0; j <= i; i++)
		{
			 a = a + X[j];
			
		}
		 A[i] = a / (i + 1);
		
	}
	return A;
}	
	

double* prefixAverage2(int X[], int n)
{
	double *A = new double[n];
	int s = 0;
	for (int i = 0; i < n; i++)
	{
		s = s + X[i];
		A[i] = s / (i + 1);
		
	}
	return A;
}

int p1(int x, int n)
{
	if (n == 0)
	{
		return 1;
	}

	else
	{
		return x * p1(x, n - 1);
	}
}

int p2(int x, int n)
{
	int temp = 0;
	if (n == 0)
	{
		return 1;
		temp = p2(x, n / 2);
	}
	if (n % 2 == 0)
	{
		return temp * temp;
	}
	else
	{
		if (n > 0)
		{
			return x * temp * temp;
		}
		else
		{
			return (temp * temp) / x;
		}
	}
	
}
Line 68
for (int j = 0; j <= i; i++)

i++ looks wrong.
Thank You so much. The smallest things really matter dont they?
Topic archived. No new replies allowed.