-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlec023.cpp
210 lines (173 loc) · 5.58 KB
/
lec023.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
//? 2D ARRAYS
#include<climits> // for INT_MIN & INT_MAX
#include<iostream>
using namespace std;
// 2D arrays are for visualizing, in reality they are 1D array. Formula to find location in 1D array is ' col * i + j ', where col is total number of columns, i is row number, j is column number. This indexing is done in memory
void inputArray(int arr[][4], int row, int col) { //we have to tell the number of columns in function paratemter for 2D array
/*
// row wise input
for(int i=0; i<row; i++) {
for(int j=0; j<col; j++) {
cin>> arr[i][j];
}
}
// col wise input
for(int j=0; j<col; j++) {
for(int i=0; i<row; i++) {
cin>> arr[i][j];
}
}
*/
}
void printArray(int arr[][4], int row, int col) {
for(int i=0; i<row; i++) {
for(int j=0; j<col; j++) {
cout<< arr[i][j] << " ";
}
cout<< endl;
}
}
bool linearSearch(int arr[][4], int target, int row, int col) {
for(int i=0; i<row; i++) {
for(int j=0; j<col; j++) {
if (arr[i][j] == target) {
return 1;
}
}
}
return 0;
}
void rowWiseSum(int arr[][4], int row, int col) {
int sum;
for(int i=0; i<row; i++) {
sum = 0;
for(int j=0; j<col; j++) {
sum += arr[i][j];
}
cout << "Sum for row number " << i+1 << " is: " << sum <<endl;
}
}
void colWiseSum(int arr[][4], int row, int col) {
int sum;
for(int j=0; j<col; j++) {
sum = 0;
for(int i=0; i<row; i++) {
sum += arr[i][j];
}
cout << "Sum for column number " << j+1 << " is: " << sum <<endl;
}
}
void largestRowSum(int arr[][4], int row, int col) {
int sum;
int largest = INT_MIN;
int rowIndex = -1;
for(int i=0; i<row; i++) {
sum = 0;
for(int j=0; j<col; j++) {
sum += arr[i][j];
}
if (sum > largest) {
largest = sum;
rowIndex = i;
}
}
cout<< "Largest sum is " << largest << " for row number " << rowIndex+1 <<endl;
}
void colSizeWise(int arr[][4], int row, int col) { //print col in normal & reverse alternatively
for(int j=0; j<col; j++) {
if (j%2 != 1) {
for(int i=0; i<row; i++) {
cout<< arr[i][j] << " ";
}
} else {
for(int i=row-1; i>=0; i--) {
cout<< arr[i][j] << " ";
}
}
}
}
void spiralMatrix(int arr[][4], int row, int col) {
//They will reduce each time so every time loop is run for 1 less previous time
int limitR = row-1;
int limitC = col-1;
//For keeping track of indexes of row and column
int currentRow = 0;
int currentCol = 0;
// Last Row and Column for loop to stop
int lastRow = row-2; //1
int lastCol = col-2; //2
int step = 1;
while(currentCol!=lastCol || currentRow!=lastRow) {
step += 1; //for telling if loop is running for odd mumber of times or even
if (step%2 == 0) { //if even, first print row forwards then column downwards
for(int j=currentCol; j<=limitC; j++) { //row forwards
cout<< arr[currentRow][j] << " ";
currentCol++;
}
currentRow++;
currentCol--;
for(int i=currentRow; i<=limitR; i++) { //column downwards
cout<< arr[i][currentCol] << " ";
currentRow++;
}
currentRow--;
if (currentCol!=lastCol && currentRow!=lastRow) {
currentCol--;
}
limitC -= 1;
limitR -= 1;
} else { //if not, print row backwards, then column upwards
for(int j=limitC; j>=0; j--) { //row backwards
cout<< arr[currentRow][j] << " ";
currentCol--;
}
currentCol++;
currentRow--;
for(int i=currentRow; i>=limitR; i--) { //column upwards
cout<< arr[i][currentCol] << " ";
currentRow--;
}
currentRow++;
currentCol++;
}
}
}
bool binarySearchMatrix(int arr[][4], int row, int col, int target) {
int s = 0, e = row*col-1;
int mid = s + (e-s)/2;
while(s<=e) {
//? FORUMLA: mid/col gives row number, mid%col gives column number
int element = arr[mid/col][mid%col];
if(element == target) {
return 1;
} else if (element > target) {
e = mid-1;
} else {
s = mid+1;
}
mid = s + (e-s)/2;
}
return -1
}
int main() {
int arr0[3][3] = {1,2,3,4,5,6,7,8,9}; // array with 3 row & 3 col
int arr[3][4] = {1,2,3,4,5,6,7,8,9,10,11,12};
int arr2[3][4] = {{1,11,111,1111}, {2,22,222,2222}, {3,33,333,3333}};
int arr4[4][4] = {1, 2, 4, 5, 3, 6, 8, 10, 11, 12, 13, 15, 16, 14, 9, 7};
// inputArray(arr0, 3, 4);
// printArray(arr, 3, 4);
// cout<< " Enter the element to search " <<endl;
// int target;
// cin>> target;
// if(linearSearch(arr, target, 3, 4)) {
// cout<< "FOUND" <<endl;
// } else {
// cout<< "NOT FOUND" <<endl;
// }
// rowWiseSum(arr, 3, 4);
// colWiseSum(arr, 3, 4);
// largestRowSum(arr, 3, 4);
// colSizeWise(arr4, 4, 4);
// spiralMatrix(arr, 3, 4);
// binarySearchMatrix(arr4, 4, 4, 10);
}