Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create Reverse2DArray.py #442

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions Matrix/Reverse2DArray.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using System;

class GFG{

static int M = 3;
static int N = 3;

// Function to reverse
// the given 2D [,]arr
static void reverseArray(int [,]arr)
{

// Traverse each row of [,]arr
for (int i = 0; i < M; i++) {

// Initialise start and end index
int start = 0;
int end = N - 1;

// Till start < end, swap the element
// at start and end index
while (start < end) {

// Swap the element
int temp = arr[i,start];
arr[i, start] = arr[i, end];
arr[i, end] = temp;

// Increment start and decrement
// end for next pair of swapping
start++;
end--;
}
}

// Print the [,]arr after
// reversing every row
for (int i = 0; i < M; i++) {
for (int j = 0; j < N; j++) {
Console.Write(arr[i, j] + " ");
}
Console.WriteLine();
}
}

// Driver Code
public static void Main(String[] args)
{
int [,]arr = { { 1, 2, 3 },
{ 4, 5, 6 },
{ 7, 8, 9 } };

// Function call
reverseArray(arr);
}
}