Skip to content

Commit

Permalink
spiralMatrix
Browse files Browse the repository at this point in the history
  • Loading branch information
iamkaniska committed Oct 3, 2024
1 parent bb9db68 commit ad90561
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
56 changes: 56 additions & 0 deletions C++/SpiralMatrix.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#include <iostream>
using namespace std;

int main() {
int rows, cols;
cout << "Enter the number of rows: ";
cin >> rows;
cout << "Enter the number of columns: ";
cin >> cols;

int matrix[100][100];

// Taking user input for the matrix
cout << "Enter the matrix elements:\n";
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
cin >> matrix[i][j];
}
}

// Spiral order printing without using a function
int top = 0, bottom = rows - 1, left = 0, right = cols - 1;

cout << "The spiral order is:\n";
while (top <= bottom && left <= right) {
// Print the top row
for (int i = left; i <= right; i++) {
cout << matrix[top][i] << " ";
}
top++;

// Print the right column
for (int i = top; i <= bottom; i++) {
cout << matrix[i][right] << " ";
}
right--;

// Print the bottom row, if any
if (top <= bottom) {
for (int i = right; i >= left; i--) {
cout << matrix[bottom][i] << " ";
}
bottom--;
}

// Print the left column, if any
if (left <= right) {
for (int i = bottom; i >= top; i--) {
cout << matrix[i][left] << " ";
}
left++;
}
}

return 0;
}
Binary file added C++/SpiralMatrix.exe
Binary file not shown.

0 comments on commit ad90561

Please sign in to comment.