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...

1
2
3
4
5
6
7
8
9
10
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
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<n; i++)
	{
		c = n-1;
		temp = matrix[row][c];
		if((c-1)>=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<n; i++)
	{
		r = n-1;
		temp = matrix[r][col];
		if((r-1)>=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

Reading in the matrix...
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 . . .

1
2
3
4
5
6
7
8
9
10
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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<n; i++)
	{
		temp = matrix[row][i];
		matrix[row][i] = matrix[row][i+1];
		matrix[row][i+1] = temp;
	}
	return;
}
void cycleup(char matrix[][noCols], int n)
{
	char temp;	int col;
	cout << "Performing column cycle up...\n";
	cout << "Which row would you like to cycle up...?\n";
	cin >> col;
	for(int i=0; i<n; i++)
	{
		temp = matrix[i][col];
		matrix[i][col] = matrix[i+1][col];
		matrix[i+1][col] = temp;
	}
	return;
}
void cycledown(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(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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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.