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 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
|
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstring>
using namespace std;
const int nCol =6;
void loadArray (int [] [nCol], int);
void printArray (int a2d [] [nCol], int nRow);
void colSum (int a2d [] [nCol], int nRow);
void rowSum (int a2d [] [nCol], int nRow);
void largestInt(int a2d [] [nCol], int nRow);
ofstream outFile;
outFile.open("C:\\Users\\joey\\Documents\\Outputfile9.txt", ios::out);
if ( outFile.fail() )
{
cerr << "\a\aOutput file could not be opened" << endl;
exit(1);
}
int main ()
{
const int nRow = 4;
int arr2d [nRow] [nCol];
loadArray (arr2d, nRow);
printArray (arr2d, nRow);
rowSum (arr2d, nRow);
colSum (arr2d, nRow);
largestInt (arr2d, nRow);
outFile.close();
cin.get();
return 0;
}
void loadArray (int a2d[] [nCol], int nRow)
{
fstream inputFile;
int count=0;
inputFile.open("C:\\Users\\joey zasa\\Documents\\input9.txt", ios::in);
if(inputFile.fail())
{ cerr <<"\a\a\aInput File Path Incorrect\n\n";
cin.get();
exit(EXIT_FAILURE);
}
for (int r=0; r < nRow; r++)
{
for (int c=0; c < nCol; c++)
inputFile >> a2d [r] [c];
}
inputFile.close();
}
void printArray (int a2d[] [nCol], int nRow)
{
for (int r=0; r < nRow; r++)
{
for (int c=0; c < nCol; c++)
{
outFile << setw(10) << r << setw(10) << a2d [r][c] << "\n";
}
}
}
void rowSum (int a2d [] [nCol], int nRow)
{
int rNum=0;
int maxRow=0;
for (int r=0; r < nRow; r++)
{
int sumRow=0;
for (int c=0; c< nCol; c++)
{
sumRow += a2d [r] [c];
}
outFile << setw(20) <<"The sum of Row " << r << " is " << setw(5) << sumRow << endl;
if (r==0)
maxRow=sumRow; // sum of row 1, that is the max so far.
else
if (sumRow > maxRow) // if the sum of the current row is greater than the maximum, than Maximum row is equal to the sum of that row.
// (rNum) keeps track of the number of rows.
{
maxRow=sumRow;
rNum=r;
}
}
outFile << setw(20) <<"The largest sum is " << maxRow << " and its located in row " << rNum << endl;
}
void colSum (int a2d [] [nCol], int nRow)
{
int cNum=0;
int minCol=0;
for (int c=0; c < nCol; c++)
{
int sumCol=0;
for (int r=0; r< nRow; r++)
{
sumCol+= a2d [r] [c];
}
outFile << setw(20) <<"The sum of Column " << c << " is " << setw(5) << sumCol << endl;
if (c==0)
minCol=sumCol;
else
if (sumCol< minCol)
{
sumCol=minCol;
cNum=c;
}
}
outFile << setw(25) << "The smallest sum is " << minCol << " and it is located in column " << cNum << "." <<endl;
}
void largestInt(int a2d [] [nCol], int nRow)
{
int largest= a2d [0] [0];
int rowIndex=0;
int colIndex=0;
for (int r=0; r < nRow; r++)
{
for (int c=0; c < nCol; c++)
{
if (a2d [r] [c] > largest)
{ largest= a2d [r][c];
rowIndex=r;
colIndex=c;
}
}
}
outFile << setw(27) << "The largest number is " << largest << " and is located in row index " << rowIndex
<< " and Column index " << colIndex << "." << endl;
}
| |