selection sort issue

my second function Sort_arrays is copying my array1 into arrayPtr. i have determined its somewhere in the selection sort but i can not figure it out.
the output should be the original array and then the sorted one but i get both sorted...

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
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;

void initialize_array(int array[],int number); // initialize array 1 prototype
int *sort_array(int array[],int size); // sort array 1 and send to array 2
void print_array(int array[], int pointer);// prints array 1 and 2

int main() {

int *arrayP = 0; //pointer
int size; // size of array
cout << "enter size of array";
cin>> size;

int array1[size]; // array 1

initialize_array(array1,size); //create array1


arrayP=sort_array(array1, size); // sort and return arrayP

for(int i=0; i<size; i++)
{
	cout << array1[i] << " ";
}


cout << endl;
for(int i=0; i < size; i++)
{
	cout << *(arrayP + i) << " ";
}
	return 0;
}

void initialize_array(int array[],int number)
{

		srand(time(0));

	for(int i=0; i<number; i++) // Initializing elements
		{
			array[i] = rand()% 25 + 1;
		}

}

int *sort_array(int array[], int size)

{
	int *arrayPtr=0;
	arrayPtr= new int[size];

	for(int i=0; i <size; i++)
	{
		arrayPtr[i]=i*i;
	}

	arrayPtr = array;

	int startScan, minIndex, minValue;
	for(startScan=0; startScan <(size-1); startScan++)
	{
		minIndex = startScan;
		minValue= arrayPtr[startScan];
		for(int index = startScan +1; index <size; index++)
		{
			if(arrayPtr[index] < minValue)
			{
				minValue = arrayPtr[index];
				minIndex=index;
			}
		}
		arrayPtr[minIndex] = arrayPtr[startScan];
		arrayPtr[startScan] = minValue;
	}

	return arrayPtr;
}


void print_array(int array[], int pointer)
{

}

Line 61: overwrites the new array with the old one.

The new one is lost, and is called a memory leak or garbage. arrayP now refers to array.
Topic archived. No new replies allowed.