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
|
#include <iostream>
#include <fstream>
void Readfile(double**&, int*&, int&, std::ifstream&);
int ColumnSize(const int*, int);
void RowSum(const double*const*, const int*, int);
void ColumnSum(const double*const*, const int*, int, int);
int main() {
char FileName[100] {};
std::ifstream Main;
std::cout << "Enter File Name here: ";
std::cin >> FileName;
//????? What's this supposed to do ???
/*
for (int i = 0; i < 100; i++) {
if (FileName[i] == -52 || FileName[i] == '\0') {
for (int j = i; j < (i + 4); j++) {
if (j == i) {
FileName[j] = '.';
} else if (j == i + 1 || j == i + 3) {
FileName[j] = 't';
if (j == i + 3) {
FileName[j + 1] = '\0';
}
} else if (j == i + 2) {
FileName[j] = 'x';
}
}
break;
}
}
*/
Main.open(FileName);
if (Main.is_open()) {
std::cout << "This file exists!\n\n";
double** Matrix {};
int* MatrixColumns {};
int Rows {};
Readfile(Matrix, MatrixColumns, Rows, Main);
std::cout << "\n---------------------------------------------\n\n";
const auto MaxColumnSize { ColumnSize(MatrixColumns, Rows) };
//cout << MaxColumnSize << '\n';
RowSum(Matrix, MatrixColumns, Rows);
ColumnSum(Matrix, MatrixColumns, Rows, MaxColumnSize);
delete[] MatrixColumns;
for (int i = 0; i < Rows; ++i)
delete[] Matrix[i];
delete[] Matrix;
} else
std::cout << FileName << " - This file does not exist.\n";
}
void Readfile(double**& Matrix, int*& MatrixColumns, int& Rows, std::ifstream& Main) {
Main >> Rows;
MatrixColumns = new int[Rows] {};
Matrix = new double* [Rows] {};
std::cout << "Matrix Input is:\n";
for (int i = 0; i < Rows; i++) {
int Columns {};
Main >> Columns;
MatrixColumns[i] = Columns;
Matrix[i] = new double[Columns];
for (int j = 0; j < Columns; j++) {
Main >> Matrix[i][j];
std::cout << Matrix[i][j] << " ";
}
std::cout << '\n';
}
}
int ColumnSize(const int* MatrixColumns, int Rows) {
auto maxcol { MatrixColumns[0] };
for (int i = 1; i < Rows; ++i)
if (MatrixColumns[i] > maxcol)
maxcol = MatrixColumns[i];
return maxcol;
}
void RowSum(const double*const* Matrix, const int* MatrixColumns, int Rows) {
std::cout << "Sum of Matrix Rows: ";
for (int i = 0; i < Rows; i++) {
double SumofRows {};
for (int j = 0; j < MatrixColumns[i]; j++)
SumofRows += Matrix[i][j];
std::cout << SumofRows << " ";
}
std::cout << '\n';
}
void ColumnSum(const double*const* Matrix, const int* MatrixColumns, int Rows, int ColumnSize) {
std::cout << "Sum of Matrix Columns: ";
for (int i = 0; i < ColumnSize; i++) {
double SumofColumns {};
for (int j = 0; j < Rows; j++)
if (i < MatrixColumns[j])
SumofColumns += Matrix[j][i];
std::cout << SumofColumns << " ";
SumofColumns = {};
}
std::cout << '\n';
}
|