in working with my 2d arrays, i have to read in from an input file a char array of NxN and manipulate the matrix in various ways. i have gotten just about everything finished, except i cannot figure out the logic of shifting a row left or right, or shifting a column up or down.

i know i have to temporarily store the last element in a row or column, and then i get stuck on figuring out moving an element from there. i have this for trying to shift a row right that the user specifies. this part here ends up reversing the elements of the row instead of shifting them. n is the size of an NxN matrix.

for(int i=0; i<n/2; i++)
{
    temp = matrix[row][n-i-1];
    matrix[row][n-i-1] = matrix[row][i];
    matrix[row][i] = temp;
}

Edited 3 Years Ago by mike_2000_17: Fixed formatting

ok so i was able to get a little further, though i can't seem to get the other characters to get into the correct places. i am just trying to shift all the elements of a row to the right one space, and the last element to the front. here's what i've accomplished...

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;
}

Edited 3 Years Ago by mike_2000_17: Fixed formatting

Start of with a 1d array, and try to do a simple rotate left or right.

Then when that is working try and mix it into your 2d array.

my output always comes out the same. if the row is ABC, the function keeps returning ACB. do i need another for loop somewhere in there, or is the c variable messing me up?

I don't know what your 2d array looks like, but try my suggestion in post #3.

Start of simple then build on it from there.

Also look at your code... you have the variable i incrementing but you don't actually use it anywhere in your code.

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

Code:

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;
}

Edited 3 Years Ago by mike_2000_17: Fixed formatting

i just got the code to work. thanks for the little pushes. i think i need to sleep now. i just had some out of bounds crap going on.

This question has already been answered. Start a new discussion instead.