diff --git a/Matrix/2DMatrixFindRow.cpp b/Matrix/2DMatrixFindRow.cpp new file mode 100644 index 0000000..2aabdbb --- /dev/null +++ b/Matrix/2DMatrixFindRow.cpp @@ -0,0 +1,61 @@ +// CPP program to find the row +// with maximum number of 1s +#include +using namespace std; +#define R 4 +#define C 4 + +// Function that counts how many 1s there are in a row +int count_ones(bool row[], int len) { + int count = 0; + + for (int j = 0 ; j < len ; j++ ){ + if(row[j] == 1){ + count++; + } + } + + return count; +} + +// Function that returns index of row +// with maximum number of 1s. +int rowWithMax1s(int m, int n, bool mat[R][C]) { + + int rowIndex = -1 ; + int maxCount = 0 ; + + for(int i = 0 ; i < n ; i++){ + int count = 0 ; + + count = count_ones(mat[i], m); + + if(count > maxCount){ + maxCount = count ; + rowIndex = i ; + } + } + + return rowIndex ; +} + + +// Driver Code +int main() +{ + int m = 4; + int n = 4; + + bool mat[R][C] = { {0, 0, 0, 1}, + {0, 1, 1, 1}, + {1, 1, 1, 1}, + {0, 0, 0, 0}}; + + cout << "Index of row with maximum 1s is " << rowWithMax1s(C, R, mat) << "\n"; + + return 0; +} + +// This code is very basic, yet, it was authored by someone else, who posted it on geeksforgeeks, +// and was edited slightly by myself to fit the educational purpose of this request. +// Unknown author, https://www.geeksforgeeks.org/find-the-row-with-maximum-number-1s/ \ No newline at end of file diff --git a/Matrix/2DMatrixSpiralForm.cpp b/Matrix/2DMatrixSpiralForm.cpp new file mode 100644 index 0000000..a3d9c36 --- /dev/null +++ b/Matrix/2DMatrixSpiralForm.cpp @@ -0,0 +1,90 @@ +// C++ Program to print a matrix spirally + +#include +using namespace std; +#define R 3 +#define C 6 + +void print(int n) { + cout << n << " "; +} + +void top_row(int n, int i, int l, int k, int a[R][C]) { + for (i = l; i < n; ++i) { + print(a[k][i]); + } +} + +void right_column(int n, int m, int i, int k, int a[R][C]) { + for (i = k; i < m; ++i) { + print(a[i][n - 1]); + } +} + +void bottom_row(int n, int m, int i, int l, int a[R][C]){ + for (i = n - 1; i >= l; --i) { + print(a[m - 1][i]); + } +} + +void left_column(int m, int i, int k, int l, int a[R][C]){ + for (i = m - 1; i >= k; --i) { + print(a[i][l]); + } +} + +void spiralPrint(int m, int n, int a[R][C]) +{ + int i, k = 0, l = 0; + + /* k - starting row index + m - ending row index + l - starting column index + n - ending column index + i - iterator + */ + + while (k < m && l < n) { + /* Print the first row from + the remaining rows */ + top_row(n, i, l, k, a); + k++; + + /* Print the last column + from the remaining columns */ + right_column(n, m, i, k, a); + n--; + + /* Print the last row from + the remaining rows */ + if (k < m) { + bottom_row(n, m, i, l, a); + m--; + } + + /* Print the first column from + the remaining columns */ + if (l < n) { + left_column(m, i, k, l, a); + l++; + } + } +} + +/* Driver Code */ +int main() +{ + int a[R][C] = { { 1, 2, 3, 4, 5, 6 }, + { 7, 8, 9, 10, 11, 12 }, + { 13, 14, 15, 16, 17, 18 } }; + + // Function Call + spiralPrint(R, C, a); + + cout << "\n"; + + return 0; +} + +// This is code is a slightly edited version by rathbhupendra, edited to fit the educational purpose of this project. +// The original codelet can be found in https://www.geeksforgeeks.org/print-a-given-matrix-in-spiral-form/ \ No newline at end of file diff --git a/Matrix/2DMatrixSum.cpp b/Matrix/2DMatrixSum.cpp new file mode 100644 index 0000000..71f2907 --- /dev/null +++ b/Matrix/2DMatrixSum.cpp @@ -0,0 +1,40 @@ +// C++ program for implementation of sum of the elements of a matrix + +#include +using namespace std; +#define R 3 +#define C 4 + +int sum_of_row(int row[], int len) { + int sum = 0; + + for (int j = 0; j < len; j++){ + sum += row[j]; + } + + return sum; +} + +// A function to implement 2d matrix sum +int MatrixSum2D(int n, int m, int matrix[R][C]) +{ + int sum = 0; + for (int i = 0; i < n; i++){ + sum += sum_of_row(matrix[i], m); + } + return sum; + +} + +// Driver code +int main() +{ + int matrix[R][C] = {{0,1,0,0}, {0,3,0,0}, {2,0,0,4}}; + + cout << "Sum: \n"; + cout << MatrixSum2D(R, C, matrix) << "\n"; + return 0; +} + +// This codelet should have all of the basics for a starter +// to iterate through a matrix in a naive fashion \ No newline at end of file diff --git a/Matrix/row b/Matrix/row new file mode 100755 index 0000000..df92e3b Binary files /dev/null and b/Matrix/row differ diff --git a/Matrix/spiral b/Matrix/spiral new file mode 100755 index 0000000..ef53161 Binary files /dev/null and b/Matrix/spiral differ diff --git a/Matrix/sum b/Matrix/sum new file mode 100755 index 0000000..b285122 Binary files /dev/null and b/Matrix/sum differ