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

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;
}
Member Avatar for iamthwee

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?

Member Avatar for iamthwee

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

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.

Member Avatar for ALHANOUF

hi
i want some helps if i want to shifting row up or down, and the last row want to be zero
how can i do ?please help me because i should to submit the project

Hi ALHANOUF

Even though you've obviously searched for and found a relevant thread to which you have attached your question, you may have failed to notice that this particular discussion has been dead for NINE years. Resurrecting long dead threads is frowned upon in forums such as ours, and does you no favours in getting the help you seek.

May I suggest that you ask your question again, but as a new post and with as much background information as is required to help our experts answer it for you in a timely fashion? That way you are much more likely to get the help you want, thanks...

commented: "Someday, and that day may never come, I will call upon you to do a service for me. But until that day, accept this justice as a gift..." +15
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.