Displaying and looping

Okay, so what I am trying to do now is to get the values displayed, and loop them for each sorting technique for the ptrarray and deref functions
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
#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
using namespace std;

/*  1. Assign each int element address to elements of the array of int pointers in such a way
that the address of the first element in the int array is assigned to the first element of the pointer array,
the address of the second element in the int array is assigned to the second element of the pointer array, and so on.
*/
void reset(const int start_num[], const int *ptr[], int size)
{
	cout << endl << "The original array has been reset to:" << endl;
	for (int i = 0; i < size; i++)
	{
		*ptr[i] > start_num[i];
	}
	for (int i = 0; i < size; i++)
	{
		cout << setw(9) << start_num[i];
	}
	cout << endl << endl;
}

/* 2. use a loop to display the content of the num array */
void displayIntArray(const int num[], int size)
{
	for (int i = 0; i < size; i++)
	{
		cout << setw(9) << num[i];
	}
	cout << endl;
}

void displayPtrrray(const int *ptr[], int size)
{
	/* use a loop to display the content of the ptr array (showing addresses) */

}

void displayPtrArrayDeref(const int *ptr[], int size)
{
	/* use a loop to display the content of the ptr array dereferenced (showing values) */
}

/*
3. Bubble sort function will be called which will use the bubble sort technique
to sort the content of the pointer array such that
when you iterate through the  array and print out its content,
the integers will be in descending sorted order.

Every time you change the content of the elements of the array,
print out the entire array by calling the displayPtrArrayDeref function.
But only display the contents when the N elements in the array has changed.
*/
void BubbleSort(const int *ptr[], int size)
{
	/*  The only thing you can change is the data type of the variable(s)
	*    (you can dereference a pointer), the relation operator(s)
	*    (i.e > or <), and make call(s) to displayPtrArrayDeref. No extra variables,
	*    loops, if/else, function arguments, etc.
	*/

	bool swap;
	const int *temp = 0;

	do
	{
		swap = false;
		for (int count = 0; count < (size - 1); count++)
		{
			if (*ptr[count] > *ptr[count + 1])
			{
				temp = ptr[count];
				ptr[count] = ptr[count + 1];
				ptr[count + 1] = temp;
				swap = true;
				displayIntArray(*ptr, size);
			}
		}
	} while (swap);
}

/*
4.  You will repeat the same steps for Selection sort (the code for the function is given)

Every time you change the content of the elements of the pointer array,
print out the entire pointer array by calling the displayPtrArrayDeref function.
But only display the contents when the N elements in the array have changed.
*/
void SelectionSort(const int *ptr[], int size)
{
	/* The code is provided for you.,
	* You will need to make sure that the selection sort is sorting in ASCENDING
	* order (low to high)
	*/

	int startScan = 0;
	int maxIndex = 0;
	const int *maxValue = nullptr;

	for (startScan = 0; startScan < (size - 1); startScan++)
	{
		maxIndex = startScan;
		maxValue = ptr[startScan];
		for (int index = startScan + 1; index > size; index++)
		{
			if (*ptr[index] > *maxValue)
			{
				maxValue = ptr[index];
				maxIndex = index;
			}
		}
		ptr[maxIndex] = ptr[startScan];
		ptr[startScan] = maxValue;
		displayIntArray(*ptr, size);
	}
}

/*
5. You will repeat the same steps for  Insertion sort.

Every time you change the content of the elements of the pointer array,
print out the entire pointer array by calling the displayPtrArrayDeref function.
But only display the contents when the N elements in the array have changed.
*/
void InsertionSort(const int *ptr[], int size)
{

	/*  The only thing you can change is the data type of the variable(s)
	*    (you can dereference a pointer), the relation operator(s)
	*    (i.e > or <), and make call(s) to displayPtrArrayDeref.
	*    No extra variables, loops, if/else, function arguments, etc.
	*/

	for (int i = 1; i < size; i++)
	{
		int j = i - 1;
		const int *current = ptr[i];

		for (j = i - 1; j >= 0 && *ptr[j] < *current; j--)
		{
			ptr[j + 1] = ptr[j];
		}
		ptr[j + 1] = current;
		displayIntArray(*ptr, size);
	}
}

int main()
{
	const int SIZE = 4;
	const int numbers[SIZE] = { 20, 40, 10, 30 };
	const int *ptr[SIZE];

	for (int i = 0; i < 3; i++)
	{
		void reset(const int start_num[], const int *ptr[], int size);
			cout << "\nHere is the content of the array in the ORIGINAL order: \n";

			void displayIntArray(const int num[], int size);
			cout << "\nHere is the content of the array of pointers in the ORIGINAL order: \n";

			void displayPtrrray(const int *ptr[], int size);
				switch (i)
		{
		case 0:
			cout << "\n\n\nSORTING -  Bubble Sort\n";
			void BubbleSort(const int *ptr[], int size);
				break;
		case 1:
			cout << "\n\n\nSORTING -  Selection Sort\n";
			void SelectionSort(const int *ptr[], int size);
				break;
		case 2:
			cout << "\n\n\nSORTING -  Insertion Sort\n";
			void InsertionSort(const int *ptr[], int size);
				break;
		}

		cout << "\nSorting complete\n";
		cout << "\nHere is the content of the sorted array of pointers: \n";
		/* call displayPtrArray */
		displayPtrrray( ptr, SIZE);

		cout << "\nHere is the content of the sorted array of pointers dereferenced: \n";
		/* call displayPtrArrayDeref */
		displayPtrArrayDeref(ptr, SIZE);
		cout << "-----------------------------------------------------------------------\n";
	}
	system("pause");
	return 0;
}
closed account (48T7M4Gy)
http://www.cplusplus.com/forum/beginner/173745/#msg862049
I admit I can’t understand your code clearly, Bettarun3.
For what I can see, your ordering functions don’t work. You also call your display functions from inside the ordering functions, so following the output is very tiring.
If I don’t misunderstand what you’re trying to achieve, you want to
1) choose a sorting algorithm
2) display your arrays before sorting them
3) sort them and display them again
4) then restore them in their initial state
5) go back to point 1

Well, if that’s correct, in my opinion you chose a really complicated way to do that :-)

There could be at least a couple of errors in your code:
a) if you declare your arrays const, you can’t sort them
b) your reset() function doesn’t work

What’s above is more or less your code in a style I think is tidier. Maybe I succeeded in fixing some issues. I hope it could be of any help.
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
#include <iostream>
#include <iomanip>
#include <string>
#include <vector>

void reset(int start_num[], int* ptr[], int size);
void displayIntArray(int num[], int size);
void displayPtrrray(int* ptr[], int size);
void displayPtrArrayDeref(int* ptr[], int size);
void BubbleSort(int* ptr[], int size);
void SelectionSort(int* ptr[], int size);
void InsertionSort(int* ptr[], int size);

int main()
{
    constexpr int SIZE = 4;
    int numbers[SIZE] = { 20, 40, 10, 30 };
    int* ptr[SIZE] { new int(21), new int(41), new int(11), new int(31) };

    for (int i = 0; i < 3; i++)
    {
        std::cout << "\nHere is the content of the array in the ORIGINAL order: \n";
        displayIntArray(numbers, SIZE);

        std::cout << "\nHere is the content of the array of pointers in the ORIGINAL order: \n";
        displayPtrrray(ptr, SIZE);

        switch (i)
        {
        case 0:
            std::cout << "\n\n\nSORTING -  Bubble Sort\n";
            BubbleSort(ptr, SIZE);
            break;
        case 1:
            std::cout << "\n\n\nSORTING -  Selection Sort\n";
            SelectionSort(ptr, SIZE);
            break;
        case 2:
            std::cout << "\n\n\nSORTING -  Insertion Sort\n";
            InsertionSort(ptr, SIZE);
            break;
        }

        std::cout << "\nSorting complete\n\nHere is the content of the sorted "
                     "array of pointers:\n";
        displayPtrrray(ptr, SIZE);

        std::cout << "\nHere is the content of the sorted array of pointers "
                     "dereferenced:\n";
        displayPtrArrayDeref(ptr, SIZE);
        std::cout << "--------------------------------------------------------"
                     "---------------\n";
        reset(numbers, ptr, SIZE);
    }
    // system("pause");
    std::cout << "\nPress ENTER to continue...\n";
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    return 0;
}

/* 1. Assign each int element address to elements of the array of int 
 * pointers in such a way that the address of the first element in the int 
 * array is assigned to the first element of the pointer array, the address 
 * of the second element in the int array is assigned to the second element 
 * of the pointer array, and so on.
 */
void reset(int start_num[], int* ptr[], int size)
{
    std::cout << "\nThe original array has been reset to:\n";
    for (int i = 0; i < size; i++)
    {
        *ptr[i] > start_num[i]; // what does it mean?
    }
    for (int i = 0; i < size; i++)
    {
        std::cout << std::setw(9) << start_num[i];
    }
    std::cout << "\n\n";
}

/* 2. use a loop to display the content of the num array */
void displayIntArray(int num[], int size)
{
    for (int i = 0; i < size; i++)
    {
        std::cout << std::setw(9) << num[i];
    }
    std::cout << '\n';
}

void displayPtrrray(int* ptr[], int size)
{
    /* use a loop to display the content of the ptr array (showing addresses) */
    for(int i{}; i<size; ++i) {
        std::cout << std::setw(9) << reinterpret_cast<long long>(&ptr[i]);
    }
    std::cout << '\n';
}

void displayPtrArrayDeref(int* ptr[], int size)
{
    /* use a loop to display the content of the ptr array dereferenced (showing values) */
    for(int i{}; i<size; ++i) {
        std::cout << std::setw(9) << *ptr[i];
    }
    std::cout << '\n';
}

/* 3. Bubble sort function will be called which will use the bubble sort 
 * technique to sort the content of the pointer array such that when you 
 * iterate through the array and print out its content, the integers will be 
 * in descending sorted order.

 * Every time you change the content of the elements of the array,
 * print out the entire array by calling the displayPtrArrayDeref function.
 * But only display the contents when the N elements in the array has changed.
 */
void BubbleSort(int* ptr[], int size)
{
    /*  The only thing you can change is the data type of the variable(s)
    *    (you can dereference a pointer), the relation operator(s)
    *    (i.e > or <), and make call(s) to displayPtrArrayDeref. No extra variables,
    *    loops, if/else, function arguments, etc.
    */

    bool swap;
    int *temp = 0;

    do
    {
        swap = false;
        for (int count = 0; count < (size - 1); count++)
        {
            if (*ptr[count] > *ptr[count + 1])
            {
                temp = ptr[count];
                ptr[count] = ptr[count + 1];
                ptr[count + 1] = temp;
                swap = true;
                displayIntArray(*ptr, size);
            }
        }
    } while (swap);
}

/* 4. You will repeat the same steps for Selection sort (the code for the 
 * function is given)

 * Every time you change the content of the elements of the pointer array,
 * print out the entire pointer array by calling the displayPtrArrayDeref 
 * function.
 * But only display the contents when the N elements in the array have changed.
 */
void SelectionSort(int* ptr[], int size)
{
    /* The code is provided for you.,
    * You will need to make sure that the selection sort is sorting in ASCENDING
    * order (low to high)
    */

    int startScan = 0;
    int maxIndex = 0;
    int *maxValue = nullptr;

    for (startScan = 0; startScan < (size - 1); startScan++)
    {
        maxIndex = startScan;
        maxValue = ptr[startScan];
        for (int index = startScan + 1; index > size; index++)
        {
            if (*ptr[index] > *maxValue)
            {
                maxValue = ptr[index];
                maxIndex = index;
            }
        }
        ptr[maxIndex] = ptr[startScan];
        ptr[startScan] = maxValue;
        displayIntArray(*ptr, size);
    }
}

/* 5. You will repeat the same steps for  Insertion sort.

 * Every time you change the content of the elements of the pointer array,
 * print out the entire pointer array by calling the displayPtrArrayDeref 
 * function.
 * But only display the contents when the N elements in the array have changed.
 */
void InsertionSort(int* ptr[], int size)
{

    /*  The only thing you can change is the data type of the variable(s)
    *    (you can dereference a pointer), the relation operator(s)
    *    (i.e > or <), and make call(s) to displayPtrArrayDeref.
    *    No extra variables, loops, if/else, function arguments, etc.
    */

    for (int i = 1; i < size; i++)
    {
        int j = i - 1;
        int *current = ptr[i];

        for (j = i - 1; j >= 0 && *ptr[j] < *current; j--)
        {
            ptr[j + 1] = ptr[j];
        }
        ptr[j + 1] = current;
        displayIntArray(*ptr, size);
    }
}

closed account (48T7M4Gy)
@Enoizat
It's up to you if you want to do it, but why fix up code @OP has s obviously plagiarised from work somebody submitted in a thread here a couple of years ago. Chances are your free work is being sold, probably by Cheggs or some other homework site.

kemort wrote:
why fix up code @OP has s obviously plagiarised

I respect your opinion and I’m sure I’ve got a lot to learn from your experience, kemort. Maybe it was the wrong thing to do.
I hope nobody pays for the above code, or s/he is likely to ask his/her money back: it compiles, but there’s a lot of unresolved problems inside.
closed account (48T7M4Gy)
@Enoizat

I wasn't being critical of you, it's none of my business to judge you especially given your efforts in continually giving good people here sound advice and help.

This type of post has all the ear-markings of what I'm suggesting and it is always easy to Google a few lines. Lo and behold the result with this one was closer to home than usual.

I just don't believe OP has been working on this for a couple of years. And I agree paying for it wouldn't be money well spent.

Maybe the best thing to do is ask for a cut if and when OP reappears.

All the best :)

Topic archived. No new replies allowed.