Skip to content
Open
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
61 changes: 61 additions & 0 deletions Matrix/2DMatrixFindRow.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// CPP program to find the row
// with maximum number of 1s
#include <bits/stdc++.h>
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/
90 changes: 90 additions & 0 deletions Matrix/2DMatrixSpiralForm.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
// C++ Program to print a matrix spirally

#include <bits/stdc++.h>
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/
40 changes: 40 additions & 0 deletions Matrix/2DMatrixSum.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// C++ program for implementation of sum of the elements of a matrix

#include <bits/stdc++.h>
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
Binary file added Matrix/row
Binary file not shown.
Binary file added Matrix/spiral
Binary file not shown.
Binary file added Matrix/sum
Binary file not shown.