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
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.
Thanks for clarifying! I didn't realize it was about teaching pointers specifically.
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.
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!
This really helped clear things up, thanks!
Glad to hear that! It's all about getting used to how pointers operate.
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.
I appreciate the step-by-step breakdown! I honestly hadn’t thought about using functions for this.
Yeah, definitely helps to approach it systematically!
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.