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

Added Wave Print In a Matrix (both row-wise and column wise) #1189

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
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
38 changes: 38 additions & 0 deletions 2D Arrays/WavePrintOfMatrix/ColumnWise.c
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);
}
67 changes: 67 additions & 0 deletions 2D Arrays/WavePrintOfMatrix/README.md
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.
38 changes: 38 additions & 0 deletions 2D Arrays/WavePrintOfMatrix/RowWise.c
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);
}