public
Last active

C++ Beginner Solution for /r/dailyprogrammer #85(Easy) by landscapez

  • Download Gist
AnalyzeMatrix.cpp
C++
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
#include "AnalyzeMatrix.h"
#include <iostream>
using namespace std;
AnalyzeMatrix::AnalyzeMatrix(fstream &fileInc)
{
if(fileInc.is_open()){
if(fileInc.good()){
vector <int> cols;
string edge;
while(getline(fileInc,edge)){
edge += " ";
int temp = 0;
int val = 0;
for(int k = 0; k < edge.size(); k++){
if(edge[k] == ' '){
while(temp != 0){
val += decimalMe(edge[k-temp]-48,temp);
temp--;
}
cols.push_back(val);
val = 0;
}else {temp++;continue;}
}
rows.push_back(cols);
cols.clear();
}
}
}
FillVecs();
}
 
AnalyzeMatrix::~AnalyzeMatrix()
{
//dtor
}
void AnalyzeMatrix::PrintMatrix(){
for(int i = 0; i < rows.size(); i++){
for(int kk = 0; kk < rows[i].size(); kk++){
cout << rows[i][kk] << " ";
}
cout << endl;
}
}
void AnalyzeMatrix::PrintAscendingRow(){
BubbleSortPairs(rowZ,1);
for(int i = 0; i < rows.size();i++){
int row = rowZ[i].second;
for(int kk = 0; kk < rows[i].size(); kk++){
cout << rows[row][kk] << " ";
}
cout << endl;
for(int bb = 0; bb < (colZ.size() * 2); bb++){
cout << "__";
}
cout << endl;
}
}
void AnalyzeMatrix::PrintDescendingRow(){
BubbleSortPairs(rowZ,0);
for(int i = 0; i < rows.size();i++){
int row = rowZ[i].second;
for(int kk = 0; kk < rows[i].size(); kk++){
cout << rows[row][kk] << " ";
}
cout << endl;
for(int bb = 0; bb < (colZ.size() * 2); bb++){
cout << "__";
}
cout << endl;
}
}
void AnalyzeMatrix::PrintAscendingCol(){
BubbleSortPairs(colZ,1);
for(int i = 0; i < rows.size();i++){
for(int kk = 0; kk < rows[i].size(); kk++){
int col = colZ[kk].second;
cout << rows[i][col] << " | ";
}
cout << endl;
}
}
void AnalyzeMatrix::PrintDescendingCol(){
BubbleSortPairs(colZ,0);
for(int i = 0; i < rows.size();i++){
for(int kk = 0; kk < rows[i].size(); kk++){
int col = colZ[kk].second;
cout << rows[i][col] << " | ";
}
cout << endl;
}
}
void AnalyzeMatrix::FillVecs()
{
 
int tempRC = 0;
for(int i = 0; i < rows.size(); i++){
for(int k = 0; k < rows[0].size(); k++){
tempRC += rows[i][k];
}
zumRC = make_pair(tempRC,i);
rowZ.push_back(zumRC);
tempRC = 0;
}
for(int i = 0; i < rows[0].size(); i++){
for(int k = 0; k < rows.size(); k++){
tempRC += rows[k][i];
}
zumRC = make_pair(tempRC,i);
colZ.push_back(zumRC);
tempRC = 0;
}
}
int AnalyzeMatrix::decimalMe(int val,int exp){
if(exp <= 0){
return 0;
}
if(exp > 0){
int temp = 1;
for(int i = 1; i < exp; i++){temp *= 10;}
return temp*val;
}
}
 
void AnalyzeMatrix::BubbleSortPairs(vector<pair<int,int> > &inc,bool ascending){
if(ascending){
for(int i = inc.size(); i > 0; i--){
for(int k = 0; k < i-1; k++){
if(inc[k].first > inc[k+1].first){
swap(inc[k],inc[k+1]);
}
}
}
}else{
for(int i = inc.size(); i > 0; i--){
for(int k = 0; k < i-1; k++){
if(inc[k].first < inc[k+1].first){
swap(inc[k],inc[k+1]);
}
}
}
}
}
AnalyzeMatrix.h
C++
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
#ifndef ANALYZEMATRIX_H
#define ANALYZEMATRIX_H
#include <vector>
#include <utility>
#include <string>
#include <fstream>
using namespace std;
 
class AnalyzeMatrix{
public:
AnalyzeMatrix(fstream &fileInc);
~AnalyzeMatrix();
void PrintMatrix();
void PrintAscendingRow();
void PrintAscendingCol();
void PrintDescendingRow();
void PrintDescendingCol();
protected:
private:
void FillVecs();
int decimalMe(int val, int exp);
void BubbleSortPairs(vector<pair<int,int> >&inc,bool ascending);
vector < vector<int> > rows;
pair <int,int> zumRC; // first == sum // second == pos
vector<pair<int,int> > rowZ;
vector<pair<int,int> > colZ;
};
 
#endif // ANALYZEMATRIX_H
main.cpp
C++
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
/*
http://www.reddit.com/r/dailyprogrammer/comments/xq0yv/832012_challenge_85_easy_rowcolumn_sorting/
*/
/** Files #include DOC:
* main.cpp:
iostream
* AnalyzeMatrix.h:
vector | utility | string | fstream
* AnalyzeMatrix.cpp
iostream
*
*
*
*
*/
#include <iostream>
 
using namespace std;
 
#include "AnalyzeMatrix.h"
 
int main(int argc, char **argv){
fstream fileInc("matrix.txt");
AnalyzeMatrix matrix(fileInc);
matrix.PrintAscendingCol();
cout << endl;
matrix.PrintDescendingCol();
cout << endl;
matrix.PrintAscendingRow();
cout << endl;
matrix.PrintDescendingRow();
return 0;
}
matrix.txt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
5 58 88 60 11 23 97 48 59 82 95 24 6 67 47
45 14 36 99 16 70 77 18 43 39 97 54 11 53 98
85 14 96 66 34 86 95 49 4 49 72 76 45 49 37
72 88 20 56 37 16 20 97 71 11 91 33 90 5 96
15 53 54 95 61 93 75 95 51 83 71 70 2 57 83
54 29 56 80 79 93 40 55 40 14 63 94 77 12 90
96 97 3 47 2 43 12 2 82 92 1 99 90 13 35
24 19 54 96 82 96 10 40 62 30 35 16 70 83 64
59 81 8 84 14 46 32 45 41 35 98 66 87 51 49
13 49 12 51 34 82 36 77 88 14 84 41 66 18 56
6 68 82 63 77 72 67 36 85 53 66 70 21 86 80
40 51 87 5 78 56 99 44 39 48 78 56 19 55 40
5 94 62 46 85 73 24 67 95 63 42 95 43 53 4
14 99 7 36 25 65 22 71 20 80 16 10 71 97 23
99 77 85 53 13 32 37 19 61 32 45 62 25 18 32
98 79 35 17 26 96 22 3 76 20 81 9 40 95 72
18 39 55 99 96 63 90 78 77 81 2 99 68 6 84
53 27 68 43 48 29 27 14 50 29 53 65 5 56 46
94 36 17 64 2 93 5 95 47 78 90 3 85 26 32
46 62 70 63 81 6 86 51 44 96 47 83 33 28 28

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.