need help with pointer array for sorting

I've been working with my lab for days and can't figure it out. I know there are people who's smart enough to figure out just for minutes which I'm spending days. My problem is that I can't sort in ascending order, descending order, with printArray function which I commented out. Can anyone one explain me please? Thanks in advance!

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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
  
/**************************************************************************************************
*
*   File name :
*
*   This program  YOU FILL IN HERE
*
*
*   Programmer:  		YOUR NAME HERE with contributions from B.J. Streller
*
*   Date Written:		in the past	
*
*   Date Last Revised:
*
***************************************************************************************************/


#include<iostream>
using std::cin;
using std::cout;
using std::cerr;
using std::endl;


#include <new>
using std::bad_alloc;


#include<cstdlib>

#define int_array 1
#define pointer2int_array 2





// Prototype declarations

int getSize();

void*  getArray( int &howMuch, int typeOf );

void populateArray( int num_of, int* ptr );

void printArray( int num_of, const int* ptr );



/////////////////////////// -- main -- ///////////////////////////////////////////////////////

int  main()
{

  int SIZE = getSize();

  int* dataArray =  ( int*) getArray(SIZE, int_array );
  int** pointerArray = ( int**)getArray(SIZE, pointer2int_array );


	//std::cout << "SIZE" << SIZE << endl;
  populateArray(  SIZE, dataArray ); //outputting arr data typed by users


  printArray( SIZE,   dataArray );





  delete [] dataArray;
  delete [] pointerArray;

  cout << endl << endl;

  return ( 0 );

}


////////////////////////////////  -- end mani --  ////////////////////////////////////////////////





/***************************************************************************************************
*
*   Function Name:		getSize
*
*   Purpose:			requests an integer from user
*
*   Input Parameters:		none
*
*   Output parameters:		none
*
*   Return Value:		retuns the user entered integer howMuch
*
***************************************************************************************************/

int getSize()
{
  int howMuch;
  cout << "enter the size of the array :  ";
  cin >> howMuch;
  return howMuch;

}



/***************************************************************************************************
*
*   Function Name:		getArray
*
*   Purpose:			dynamically allocates either an array of ints or an aaray of
*				int pointers
*
*   Input Parameters:		an int howMuch - the size of array to allocate
*				an int typeOf indicating the type of array to allocate
*
*   Output parameters:		none
*
*   Return Value:		returns a pointer to an int array or an array of int pointers
*
***************************************************************************************************/


void*  getArray( int &howMuch, int typeOf )
{

  try
    {
      switch ( typeOf )
        {
        case int_array:
        {
          int* p = new int [ howMuch ];
          return( p );
        }

        case pointer2int_array:
        {
          int** p = new int* [ howMuch ];
          return( p );
        }
        default:
        {
          cerr << " problem allocating array \n\n";
          exit( -666 );
        }

        }
    }
  catch ( bad_alloc &memoryAllocationException )
    {
      cerr << memoryAllocationException.what() << endl;
      exit( 666 );
    }


}



/***************************************************************************************************
*
*   Function Name     :
*
*   Purpose           :
*
*   Input Parameters  :
*
*   Output parameters :
*
*   Return Value      :
*
***************************************************************************************************/


void populateArray( int num_of, int* ptr )
{
	
	std::cout << "Please enter " << num_of << " int array to be sorted: ";

	for(int i = 0; i < num_of; i++)
	{
	std::cin >> ptr[i];
	
	}
	for(int i = 0; i < num_of; i++)
	{
	std::cout << ptr[i] << " " << endl;
	std::cout << &ptr[i] << " " << endl;

	}

}


/***************************************************************************************************
*
*   Function Name     :
*
*   Purpose           :
*
*
*   Input Parameters  :
*
*   Output parameters :
*
*   Return Value      :
*
***************************************************************************************************/





void printArray( int num_of, const int* ptr )
{
	std::cout << "The int arr you've entered are: ";
	for(int i = 0; i < num_of; i++)
	{
		std::cout <<	ptr[i] << " ";
		std::cout <<	&ptr[i] << " ";
	}

	std::cout << "\nThe int arr you've entered in ascending orders are: ";




}	
	/*
	// selection sort

	int indx;
	int smallIndx;
	int loc;
	int  tmp;

	for(indx = 0; indx < num_of -1; indx++)
	{
		smallIndx = indx;
		for(loc = indx + 1; loc < num_of; loc++)
			if(&ptr[loc] < ptr[smallIndx])
				smallIndx = loc;

		tmp = ptr[smallIndx];
		ptr[smallIndx] = ptr[indx];
		ptr[indx] = tmp;
	}

	cout << "The numbers in lowest to highest order: ";
	
	for (int i = 0; i < num_of; i++)
			{
				
				cout << ptr[i] << " "; 
			}
			cout << "\n";

	
	
	for(indx = 0; indx < num_of -1; indx++)
	{
		smallIndx = indx;
		for(loc = indx + 1; loc < num_of; loc++)
			if(ptr[loc] > ptr[smallIndx])
				smallIndx = loc;

		tmp = ptr[smallIndx];
		ptr[smallIndx] = ptr[indx];
		ptr[indx] = tmp;
	}

	cout << "The numbers in highest to lowest order: ";
	
	for (int i = 0; i < num_of; i++)
			{
				
				cout << ptr[i] << " "; 
			}
			cout << "\n";
}




}

*/
Your selection sort is fine.

But your code needs a few adjustments.

First, your printArray prototype in line 46 declares const *ptr.
But then , in line 251 you try to modify a const which is not allowed.
You can fix this by removing const in the prototype and subsequent function definitions.

Also, in line 247 you compare an address and an integer which is also not allowed.
remove the & from (&ptr[loc] < ptr[smallIndx] .

Also, you selection sort does not belong to any function. Maybe u intended to make it part of the printArray function. Afther all, i put it in printArray when i was testing it.

Finally, i dont know if you intended to print memory locations along the numbers.
line 226 .

Fix this and your program should work just fine.
Besides, they were just compiler errors. No big deal.



Topic archived. No new replies allowed.