i trying make shell algorithm arrange [5][5][5] array, far can print whole array when run shell() prints same array. doesn't print numbers arranged. please?
#include <iostream> #include <stdlib.h> using namespace std; void ordshell(int numbers[5][5][5], int n); void exchange(int& x, int& y); int main() { int numbers[5][5][5] = { { {1,2,3,4,5}, {41,42,43,44,45}, {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}, {6,7,8,9,10}, {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} } }; for(int i=0;i<5;i++) { for(int j=0;j<5;j++) { for(int l=0;l<5;l++) { cout<<numbers[i][j][l]<<","; } }} cout<<"whatever"<<""; cout<<"\n"<<""; cout<<"now comes shell"<<""; ordshell(numbers,5); cout<<"numbers arranged after shell"<<""; for(int i=0;i<5;i++) { for(int j=0;j<5;j++) { for(int l=0;l<5;l++) { cout<<numbers[i][j][l]<<","; } }} return 0; } void ordshell(int numbers[5][5][5], int n) { int jump, i, j, k,j1,j2,k1,k2; jump = n / 2; while (jump > 0) { (i = jump; < n; i++) { j = - jump; j1= - jump; j2= - jump; while (j >= 0 ) { k = j + jump; k1 = j + jump; k2 = j + jump; if (numbers[j][j1][j2] <= numbers[k][k1][k2]) {j = -1; // arranged pair j1 = -1; j2 = -1;} else { cout<<"exchange: "<<""; cout<<numbers[j][j1][j2]<<" "; cout<<numbers[k][k1][k2]<<"\n"; exchange(numbers[j][j1][j2], numbers[k][k1][k2]); j -= jump; j1 -= jump; j2 -= jump; } } } jump = jump / 2; cout<<"jump: "<<jump<<"\n"; } } void exchange(int& x, int& y) { int aux = x; x = y; y = aux; }
the problem quite simple in function: aren't modifying array copy of it:
void ordshell(int numbers[5][5][5], int n)//makes copy of numbers
if want avoid issue, need give pointer function instead.
however, c++ way use stl instead should make array way: std::vector<std::vector<std::vector<int> > >
(you can use same initialization after)
that way in function need pass reference this:
void ordshell(std::vector<std::vector<std::vector<int> > >& numbers, int n)
you can use typedef
make variable name easier read:
typedef std::vector<std::vector<std::vector<int> > > vector3d
and 1 last thing, don't need exchange
function, std::swap
same thing.
Comments
Post a Comment