problem with the function printarray

hey guys,
i wrote this code and i am having trouble with the function printarray. everything else is working fine i guess. please help ASAP! 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
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
#include <iostream>
#include <ctime>
using namespace std;
void sortarray(double B[], int s){
	int j=0;
	double temp=0.0;
	
	while (j<s){

		int k = j+1;

		while (k<s){ 

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

			k++;
		}

		j++;
	} 
}

void randomVariables(double C[], int S, int M, int N){
	srand (time(0));
	int l=0;
	double A=0.0;
	while (l<S){

		A = (rand ()% M)-N;
		C[l] = A;
		l++;
	}
}
void printarray (double * A, int K){

	int i=0;
	double X=0.0;
	
	while (i<K){
		X=*(A+(i*8));
		cout<<X<<' ';
		i++;
	}

	return;
}
int main () {
	int g=0;
	int h=0;
	int n;
	int m=0;

	double a=0.0;
	double *x;

	double b=0.0;
	double *y;
	
	
	
	cout<<"please enter the size of the array of abscissas"<<endl;
	cin>>n;

	cout<<"please enter the size of the array of ordinates"<<endl;
	cin>>m;

	x = new double [n];
	y = new double [m];

	randomVariables (x, n, 101, 0);

	randomVariables (y, m,101, 0);
	
	sortarray(x,n);

	sortarray(y,m);

	x = new double [n+n-2];
	y = new double [m+n-2];

	sortarray (x, (n+n-2));

	printarray (x ,n);
	cout<<'\n';
	
	printarray (y ,m);
	cout<<'\n';
	
	printarray (x, (n+n-2));
	cout<<'\n';

	printarray (y, (m+n-2));
	cout<<'\n';

	delete x;
	x = NULL;
	
	delete y;
	y = NULL;

	delete x;
	x=NULL;

	delete y;
	y=NULL;

	return 0;
}
First of all never use this stupid style of placing braces

void printarray (double * A, int K){

You can write the function the following way

1
2
3
4
void printarray ( const double * A, int K )
{
	for ( int i = 0; i < K; i++ ) cout << A[i] << ' ';
}


Or if you want to use the pointer arithmetic then you can write

1
2
3
4
void printarray ( const double * A, int K )
{
	for ( const double *p = A; p != A + K; ++p ) cout << *p << ' ';
}

Last edited on
it is printing but it is printing weird values that are not in the arrays
shit!! i think i figured out where the problem is. it starts from lines 83. i have to expand the size of the array then add elements to the array and sort them. can you help me? thanks a lot
Topic archived. No new replies allowed.