|
发表于 2020-3-3 17:06:18
|
显示全部楼层
大体上这样吧,正好手头有时间帮你写了一份,你试试!
- #include <iostream>
- using std::endl;
- using std::cin;
- using std::cout;
- void Print1DArray(const float * arr, int length){
- for (int i = 0; i < length; ++i){
- cout << arr[i] << " ";
- }
- cout << endl;
- }
- void Print2DArray(float ** arr, int rows, int cols){
- cout << rows << " " << cols << endl;
- for (int i = 0; i < rows; ++i){
- for (int j = 0; j < cols; ++j)
- cout << arr[i][j] << " ";
- cout << endl;
- }
- }
- float Matrix_RowCalculation(float ** arr, int rows, int cols, float * pRowSum, float * pRowMean)
- {
- float sum = 0.0;
- for (int i = 0; i < rows; ++i)
- {
- for (int j = 0; j < cols; ++j)
- {
- pRowSum[i] += arr[i][j];
- sum += arr[i][j];
- }
- pRowMean[i] = pRowSum[i] / cols;
- }
- return sum;
- }
- float Matrix_ColumnCalculation(float ** arr, int rows, int cols, float * pColumnSum, float * pColumnMean)
- {
- float sum = 0.0;
- for (int i = 0; i < cols; ++i)
- {
- for (int j = 0; j < rows; ++j)
- {
- pColumnSum[i] += arr[j][i];
- sum += arr[j][i];
- }
- pColumnMean[i] = pColumnSum[i] / rows;
- }
- return sum;
- }
- int main(void)
- {
- // 请在此处写入相关的输入语句
- // !并且禁止使用固定数组
- // 数组名使用arr
- cout << "Please input matrix rows and columns : " << endl;
- int rows = 0, cols = 0;
- cin >> rows >> cols;
- cout << "Please input nums : " << endl;
- float **arr = new float*[rows];
- for (int i = 0; i < rows; i++)
- arr[i] = new float[cols];
- for (int i = 0; i < rows; ++i)
- {
- for (int j = 0; j < cols; ++j)
- {
- cin >> arr[i][j];
- }
- }
- cout << "Please matrix is : " << endl;
- float* pRowSum = new float[cols];
- memset(pRowSum, 0, sizeof(float) * cols);
- float* pRowMean = new float[cols];
- memset(pRowMean, 0, sizeof(float) * cols);
- //// 请不要修改以下内容
- Print2DArray(arr, rows, cols);
- cout << "========================================" << endl << endl;
- cout << "==Row Calculation==" << endl
- << "All Sum:" << Matrix_RowCalculation(arr, rows, cols, pRowSum, pRowMean) << endl;
- cout << "Row Sum:" << endl;
- Print1DArray(pRowSum, cols);
- cout << "Row Mean:" << endl;
- Print1DArray(pRowMean, cols);
- cout << "========================================" << endl << endl;
- float* pColumnSum = new float[rows];
- memset(pColumnSum, 0, sizeof(float) * rows);
- float* pColumnMean = new float[rows];
- memset(pColumnMean, 0, sizeof(float) * rows);
- cout << "==Column Calculation==" << endl
- << "All Sum:" << Matrix_ColumnCalculation(arr, rows, cols, pColumnSum, pColumnMean) << endl;
- cout << "Column Sum:" << endl;
- Print1DArray(pColumnSum, rows);
- cout << "Column Mean:" << endl;
- Print1DArray(pColumnMean, rows);
- // 请在此处输入相关的处理
- delete[] pRowSum;
- delete[] pRowMean;
- delete[] pColumnSum;
- delete[] pColumnMean;
- for (int i = 0; i < rows; ++i)
- delete[] arr[i];
- delete[] arr;
- return 0;
- }
复制代码 |
评分
-
查看全部评分
|