【发布时间】:2015-10-20 21:16:28
【问题描述】:
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
int main(int argc, char *argv[]) {
ofstream outData;
ifstream inData;
string inString;
int matrix[12][12];
int rowSize, colSize;
inData.open("C:\\Users\\JSU\\Documents\\ArrayInput.txt");
inData >> rowSize >> colSize;
cout << "Row= " << rowSize << "\t Col= "<<colSize<< endl;
for (int r=0; r <rowSize; r++){
for (int c=0; c <colSize; c++){
inData >> matrix[r][c] ;
}
}
// print the input values
cout << "For the "<< rowSize << " x " << colSize << " array:"<<endl;
for (int r=0; r <rowSize; r++){
for (int c=0; c <colSize; c++){
cout << matrix[r][c] << " ";
}
cout<<endl;
}
cout << endl<<endl;
// find the largest in each row
for (int c=0; c <colSize; c++){
int largest = matrix[0][c]; //make the first cell value as the largest until you find otherwise
int smallest = matrix[0][c]; //make the first cell value as the smallest until you find otherwise
int sum=0;
for (int r=0; r <rowSize; r++){
if (matrix[r][c] > largest) //found a new larger value than the largest
largest = matrix[r][c];
if (matrix[r][c] < smallest) //found a new smaller value than the smallest
smallest = matrix[r][c];
sum = sum + matrix[r][c];
}
cout << "The sum of column "<< c + 1 << " is " << sum << endl;
cout << "The average of column "<< c + 1 << " is "<<fixed<<setprecision(2)<< (double)sum/rowSize << endl;
cout << "The largest value in column "<< c + 1 << " is " << largest << endl;
cout << "The smallest value in column "<< c + 1 << " is " << smallest << endl;
cout << "Is column " << c + 1 << " strictly ascending? " << endl;
cout<<endl;
}
inData.close();
return 0;
}
这是我的代码,我已经完成了几乎所有我需要的工作。我只是不知道如何计算数组的四个角,我需要打印一些内容 cout
【问题讨论】: