-
Notifications
You must be signed in to change notification settings - Fork 368
/
Diagonal_Printing_in_2D_arrays.cpp
112 lines (106 loc) · 3.05 KB
/
Diagonal_Printing_in_2D_arrays.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*
---------------------------------------------- Problem Statement --------------------------------------------------------
First line of input consist of number of rows and the second line consist of number of column the third line is the
matrix elements. We have to print it in diagonal print pattern.
INPUT :- 3
3
1 2 3
4 5 6
7 8 9
OUTPUT :- 1 2 4 7 5 3 6 8 9
------------------------------------------------- Complexities ----------------------------------------------------------
Time Complexity :- BigO(n*m) --> where n is the number of rows and m is the number of columns
Space Complexity :- BigO(1)
*/
#include<bits/stdc++.h>
using namespace std;
// Declaring the global variables so that we can use anywhere in the functions
int row;
int column;
// Function to print in diagnol pattern.
diagonalprint (int mat[100][100])
{
// Declaring the iterating variables and putting their value to 0
int i=0,j=0;
// A boolean variable to switch directions
bool isUp = true;
// Loop to print in diagnol pattern of 2D arrays
for (int k = 0; k < row*column;)
{
// If isUp = true then iterate from downward to upward
if (isUp)
{
// Loop for printing the values from downward to upward
for (; i >= 0 && j < column; j++, i--)
{
cout << mat[i][j] << " ";
k++;
}
// Set i and j according to isUp value (ie, according to direction)
if (i < 0 && j <= row - 1)
{
i = 0;
}
if (j == row)
{
i = i + 2;
j--;
}
}
// if isUp is false then traverse upwards to downwards
else
{
// Loop for printing the values from upward to downward
for (; j >= 0 && i < column; i++, j--)
{
cout << mat[i][j] << " ";
k++;
}
// Set i and j according to isUp value (ie, according to direction)
if (j < 0 && i <= row - 1)
{
j = 0;
}
if (i == row)
{
j = j + 2;
i--;
}
}
// Changing the value of isUp according to the direction.
isUp = !isUp;
}
}
int main()
{
// Reading the number of rows from the user
cout<<"Enter number of rows = ";
cin>>row;
// Reading the number of columns from the user
cout<<"Enter number of columns = ";
cin>>column;
// Declaring the matrix of size rows and columns
int matrix[100][100];
// Reading the matrix values from the user
for(int i=0;i<row;i++)
{
for(int j=0;j<column;j++)
{
cout<<"Enter matrix elements = ";
cin>>matrix[i][j];
}
}
// Printing the matrix
cout<<"The matrix is,\n";
for(int i=0;i<row;i++)
{
for(int j=0;j<column;j++)
{
cout<<matrix[i][j]<<" ";
}
cout<<"\n";
}
cout<<"Printing the matrix in diagnol form,\n";
// Calling the void diagonal print function to print in diagonal pattern.
diagonalprint(matrix);
}