-
Notifications
You must be signed in to change notification settings - Fork 107
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bb9db68
commit ad90561
Showing
2 changed files
with
56 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 not shown.