Instantly share code, notes, and snippets.

# phlandscape/AnalyzeMatrix.cpp Created Aug 15, 2012

What would you like to do?
C++ Beginner Solution for /r/dailyprogrammer #85(Easy) by landscapez
 #include "AnalyzeMatrix.h" #include using namespace std; AnalyzeMatrix::AnalyzeMatrix(fstream &fileInc) { if(fileInc.is_open()){ if(fileInc.good()){ vector 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 > &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]); } } } } }
 #ifndef ANALYZEMATRIX_H #define ANALYZEMATRIX_H #include #include #include #include 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 >&inc,bool ascending); vector < vector > rows; pair zumRC; // first == sum // second == pos vector > rowZ; vector > colZ; }; #endif // ANALYZEMATRIX_H
 /* 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 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; }
 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
to join this conversation on GitHub. Already have an account? Sign in to comment