0

Sorting with pointer (solved)

Hi Every body ! Is someone can help me ? i'm just beginning with pointers. i'm trying using pointer to sort an array of numbers. i wrote a code with 2 loops. First loop, find the biggest number, the second put the number from the biggest to the shortest. the output give me 89 89 89 89 89 89... I don't understand why my pointers doesn't erase the biggest value before each loop ... thank you very much #include <iostream> using namespace std; int number[10]={10,43,56,83,43,89,56,89,76,7}; int big=number[0]; int *p; int sorting[10]={}; int main() { p = &number[0]; for (int i=0;i<10;i++) { for (int x=0;x<10;x++) { if (big>=number[x]) {big=big;} else {big=number[x]; p=&number[x]; } } *p=0; sorting[i] = big; cout << sorting[i] << " "; } return 0; }

8th Jun 2018, 4:09 PM
Gaël Des Iris
Gaël Des Iris - avatar
3 Respostas
+ 1
my issue is solved ! :) instead of assigning (big) before 'main', i assigned it in the first loop. That works perfectly ! using namespace std; int number[10]={10,43,56,83,43,89,56,88,76,07}; int big; int *p; int sorting[10] = {}; int main() { for (int i=0;i<10;i++) { big = number[0]; p =& number[0]; for (int x=0;x<10;x++) { if (big>=number[x]) {} else {big=number[x]; p=&number[x]; } } *p = 0; sorting[i] = big; cout << sorting[i] << " "; } return 0; }
8th Jun 2018, 10:20 PM
Gaël Des Iris
Gaël Des Iris - avatar
0
can you please try p = (number + x);
8th Jun 2018, 6:06 PM
Vineet Bharadwaj
Vineet Bharadwaj - avatar
0
thanks Vineet but i stil have the same result . do i make something wrong?
8th Jun 2018, 9:13 PM
Gaël Des Iris
Gaël Des Iris - avatar