### 2d array element shifts i need to horizontally right and vertically up shift the elements of a 2d character array. i have come up with the code below for two separate functions, and it just doesn't want to finish correctly. i just want everything in the user given row to move to the right one space. same with the column going up one space. any hints or input to set me on the tracks again is very appreciated...

 ``1234567891011121314151617181920212223242526272829303132333435363738`` ``````void cycleright(char matrix[][noCols], int n) { char temp; int row, c; cout << "Performing row cycle right...\n"; cout << "Which row would you like to cycle right...?\n"; cin >> row; for(int i=0; i=0) { matrix[row][c] = matrix[row][c-1]; c--; } matrix[row][c] = temp; } return; } void cycleup(char matrix[][noCols], int n) { char temp; int col, r; cout << "Performing column cycle up...\n"; cout << "Which row would you like to cycle up...?\n"; cin >> col; for(int i=0; i=0) { matrix[r][col] = matrix[r-1][col]; r--; } matrix[r][col] = temp; } return; }`````` Here's a hint. In both of your functions, the body of the for() loop is in no way dependent upon the loop index variable (i), which means that all the body does is the same thing N times over.

Here's a second hint. Basically, to cycle right, you want the (i+1)th element of the row to be set to the value of the (i)th element, except in the case where i is the rightmost element, in which case you want the 0th element to be set to that value.

If you're still stuck, post your modified code and I'll help some more. ok im am very much closer. im going to show you the output my program, and you'll see that the last four function performances gives me 1 piece of garbage, and that's the new problem that i can't seem to stop.

here is the output to the screen:
Enter the name of input file to read from...
input.txt
Enter the name of output file to read from...
output.txt

Printing matrix to the screen...
Abc
DEF
gwr
Performing reverse of major diagonal...
rbc
DEF
gwA
Performing reverse of minor diagonal...
Abg
DEF
cwr
Performing reverse of a row...
Which row would you like to reverse...?
1
Abc
FED
gwr
Performing reverse of a column...
Which column would you like to reverse...?
0
gbc
DEF
Awr
Performing row cycle right...
Which row would you like to cycle right...?
0
╠Ab
DEF
gwr
Performing row cycle left...
Which row would you like to cycle left...?0
bc╠
DEF
gwr
Performing column cycle up...
Which row would you like to cycle up...?
0
Dbc
gEF
╠wr
Performing column cycle up...
Which row would you like to cycle up...?
0
╠bc
AEF
Dwr
Press any key to continue . . .

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556`` ``````void cycleright(char matrix[][noCols], int n) { char temp; int row, c; cout << "Performing row cycle right...\n"; cout << "Which row would you like to cycle right...?\n"; cin >> row; for(c=n-1; c>-1; c--) { temp = matrix[row][c]; matrix[row][c] = matrix[row][c-1]; matrix[row][c-1] = temp; } return; } void cycleleft(char matrix[][noCols], int n) { char temp; int row; cout << "Performing row cycle left...\n"; cout << "Which row would you like to cycle left...?"; cin >> row; for(int i=0; i> col; for(int i=0; i> col; for(r=n-1; r>-1; r--) { temp = matrix[r][col]; matrix[r][col] = matrix[r-1][col]; matrix[r-1][col] = temp; } return; }`````` Let's look at your cycleright() function.

The loop goes while c > -1, which means in the very last iteration, c == 0.
Inside the body of the for() loop you access:

matrix[row][c-1]

So 0-1 == -1, which means you are accessing outside of array bounds.

The body of your for() loop is otherwise correct; you just need to handle the
case c==0 specially. You have the same problem in all four functions: in
cycleleft(), i+1 accesses one element beyond the end of the array; in
cycleup(), i+1 again accesses one element beyond the end of the array;
in cycledown() r-1 accesses one element before the beginning of the array.

So in each case, you'll need an if() statement inside the for loop. For example,
to fix cycleright(), you'd do:

 ``123456789101112131415161718192021`` ``````void cycleright(char matrix[][noCols], int n) { char temp; int row, c; cout << "Performing row cycle right...\n"; cout << "Which row would you like to cycle right...?\n"; cin >> row; for(c=n-1; c>-1; c--) { if( c > 0 ) { temp = matrix[row][c]; matrix[row][c] = matrix[row][c-1]; matrix[row][c-1] = temp; } else { // You fill in the code here } } return; }``````

For bonus points, there are ways to avoid the if() check. lol if yr bonus points counted toward the class im in, then i'd try to keep working on that part. that does make sense now, and it runs perfectly. thanks for the little pushes. ill be back for my final assignment later...
Topic archived. No new replies allowed.