forked from AlgoGenesis/C
-
Notifications
You must be signed in to change notification settings - Fork 0
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
b97162a
commit 8c7c4a5
Showing
3 changed files
with
143 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,38 @@ | ||
#include <stdio.h> | ||
|
||
void ColWiseWavePrint(int rows, int cols, int matrix[rows][cols]){ | ||
int i, j; | ||
for(j=0; j<cols; j++){ | ||
if(j % 2 == 0){ | ||
for(i=0; i<rows; i++){ | ||
printf("%d\t", matrix[i][j]); | ||
} | ||
} | ||
else{ | ||
for(i=rows-1; i>=0; i--){ | ||
printf("%d\t", matrix[i][j]); | ||
} | ||
} | ||
} | ||
} | ||
|
||
int main(){ | ||
int rows; | ||
printf("Enter no. of rows: "); | ||
scanf("%d", &rows); | ||
|
||
int cols; | ||
printf("Enter no. of columns: "); | ||
scanf("%d", &cols); | ||
|
||
int matrix[rows][cols]; | ||
int i, j; | ||
for(i=0; i<rows; i++){ | ||
for(j=0; j<cols; j++){ | ||
printf("Enter value: "); | ||
scanf("%d", &matrix[i][j]); | ||
} | ||
} | ||
|
||
ColWiseWavePrint(rows, cols, matrix); | ||
} |
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,67 @@ | ||
# Row-Wise Wave Print in a Matrix | ||
|
||
## Problem Statement | ||
Given a 2D matrix of size n x m, print the matrix in a wave-like manner. | ||
|
||
## Example | ||
Given matrix: | ||
A = [ | ||
[11, 12, 13, 14] | ||
[21, 22, 23, 24] | ||
[31, 32, 33, 34] | ||
[41, 42, 43, 44] | ||
] | ||
|
||
The wave print output should be: | ||
11 12 13 14 24 23 22 21 31 32 33 34 44 43 42 41 | ||
|
||
## Approach | ||
|
||
# Input | ||
A 2D matrix of size n x m where n is the number of rows and m is the number of columns. | ||
|
||
# Output | ||
Single list or sequence of elements representing the matrix in a row-wise wave form. | ||
|
||
# Algorithm: | ||
1. Traverse through the rows one by one. | ||
2. For each even-indexed row (0, 2, 4,...), print the elements from left to right. | ||
3. For each odd-indexed row (1, 3, 5,...), print the elements from right to left. | ||
4. Continue until all rows are processed. | ||
|
||
_____________________________________________________________ | ||
|
||
# Column-Wise Wave Print in a Matrix | ||
|
||
## Problem Statement | ||
Given a 2D matrix of size n x m, print the matrix in a wave-like manner. | ||
|
||
## Example | ||
Given matrix: | ||
A = [ | ||
[11, 12, 13, 14] | ||
[21, 22, 23, 24] | ||
[31, 32, 33, 34] | ||
[41, 42, 43, 44] | ||
] | ||
|
||
The wave print output should be: | ||
11, 21, 31, 41, 42, 32, 22, 12, 13, 23, 33, 43, 44, 34, 24, 14 | ||
|
||
## Approach | ||
|
||
# Input | ||
A 2D matrix of size n x m where n is the number of rows and m is the number of columns. | ||
|
||
# Output | ||
Single list or sequence of elements representing the matrix in a column-wise wave form. | ||
|
||
# Algorithm: | ||
1. Traverse through the columns one by one. | ||
2. For each even-indexed row (0, 2, 4,...), print the elements from top to bottom. | ||
3. For each odd-indexed row (1, 3, 5,...), print the elements from bottom to top. | ||
4. Continue until all columns are processed. | ||
|
||
## Complexity (in both cases) | ||
1. Time Complexity: O(n * m), where n is the number of rows and m is the number of columns, since we visit each element exactly once. | ||
2. Space Complexity: O(1), as no extra space is used apart from the input matrix. |
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,38 @@ | ||
#include <stdio.h> | ||
|
||
void RowWiseWavePrint(int rows, int cols, int matrix[rows][cols]){ | ||
int i, j; | ||
for(i=0; i<rows; i++){ | ||
if(i % 2 == 0){ | ||
for(j=0; j<cols; j++){ | ||
printf("%d\t", matrix[i][j]); | ||
} | ||
} | ||
else{ | ||
for(j=cols-1; j>=0; j--){ | ||
printf("%d\t", matrix[i][j]); | ||
} | ||
} | ||
} | ||
} | ||
|
||
int main(){ | ||
int rows; | ||
printf("Enter no. of rows: "); | ||
scanf("%d", &rows); | ||
|
||
int cols; | ||
printf("Enter no. of columns: "); | ||
scanf("%d", &cols); | ||
|
||
int matrix[rows][cols]; | ||
int i, j; | ||
for(i=0; i<rows; i++){ | ||
for(j=0; j<cols; j++){ | ||
printf("Enter value: "); | ||
scanf("%d", &matrix[i][j]); | ||
} | ||
} | ||
|
||
RowWiseWavePrint(rows, cols, matrix); | ||
} |