Help with Using Pointers to Sort an Array in C++

0
3
Asked By CuriousCoder92 On

Hey folks, I'm pretty new to C++ and I'm struggling with understanding pointers. I have a programming exam where I need to sort an array of 20 elements using pointers in ascending order, but I'm not really sure how to go about it. My friend shared a piece of code with me that looks like this:

```cpp
#include
using namespace std;

int main() {
int niz[20];
int* p1, * p2;
cout << "Enter 20 values:n";
for (int i = 0; i < 20; i++) {
cout << "Enter number " << i + 1 <> niz[i];
}
for (int i = 0; i < 19; i++) {
for (int j = 0; j *p2) {
int temp = *p1;
*p1 = *p2;
*p2 = temp;
}
}
}
cout << "nsorted array:n";
for (int i = 0; i < 20; i++) {
cout << niz[i] << " ";
}
cout << endl;
return 0;
}
```

I'm not seeing how pointers are helping in this context. Could someone explain how to implement this and why pointers are used here? I want to learn!

4 Answers

Answered By CPlusPlusCritic On

Honestly, the code is kinda outdated. Using `using namespace std` is frowned upon, and you might want to use `std::array` instead of the C-style array. As for the pointer code, you could make it simpler like this: if `niz[j] > niz[j+1]` then use `std::swap(niz[j], niz[j+1]);`. The pointer method isn't necessary unless specified by your professor, which seems to have happened here.

TechieTom -

I think you're right, they probably just wanted to see if students understood pointers, even if there's a cleaner way using `std::swap`. It's a teaching technique.

CuriousCoder92 -

Thanks for clarifying! I didn't realize it was about teaching pointers specifically.

Answered By LearningLingo101 On

So, about sorting with pointers, it's a bit broad, but the code you shared is indeed using bubble sort. Pointers here let you directly manipulate the values in the array without needing to access the elements the usual way. By using pointers `p1` and `p2`, the code swaps the values at specific locations in the `niz` array when needed. If the professor wants you to use pointers, this is one way of doing it, albeit it might feel a bit clunky. Do you know about how bubble sort works? That could help you understand why they used pointers for the swapping process.

Answered By PointerPal On

If you're just starting with pointers, think of it this way: every variable has a memory address. When you declare a pointer like `int* mypointer = &myvar;`, you're storing the address of `myvar`. To get the value at that address, you dereference it with `*mypointer`. That allows for direct manipulation in sorting, but if you're not comfortable, don't stress about using simpler methods too!

CuriousCoder92 -

This really helped clear things up, thanks!

CodeCrafters -

Glad to hear that! It's all about getting used to how pointers operate.

Answered By SortingSavvy On

Here’s how you could tackle this problem:
1. Create a function that takes two pointers and swaps their values.
2. In your loops, compare the values at the ith index with the i+1th index. If the ith index is larger, swap the values using your function.
3. Run an outer loop to keep sorting the array.
That process defines bubble sort! It’ll help you break it down into manageable steps.

CuriousCoder92 -

I appreciate the step-by-step breakdown! I honestly hadn’t thought about using functions for this.

SyntaxSage -

Yeah, definitely helps to approach it systematically!

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.