VC驿站

 找回密码
 加入驿站

QQ登录

只需一步,快速开始

搜索
查看: 469|回复: 5

求助这道二维数组的题,谢谢大家!

[复制链接]
41_avatar_middle
最佳答案
0 
在线会员 发表于 2020-3-3 14:55:48 | 显示全部楼层 |阅读模式
【问题描述】从标准控制台输入一个二维矩阵,然后分别计算该矩阵的行和/行均值/列和/列均值及所有元素的和。
【输入形式】开始首先输入矩阵的行数(rows)与列数(cols),均为整型;
其后则是rows*cols个浮点型数据。
【输出形式】输出见【样例输出】
【样例输入】
3 4
8.124 6.778 4.365 5.133
4.255 8.630 9.528 3.849
2.323 6.158 2.253 9.096
【样例输出】
==Row Calculation==

All Sum:70.492

Row Sum:

14.702 21.566 16.146 18.078

Row Mean:

4.90067 7.18867 5.382 6.026

========================================



==Column Calculation==

All Sum:70.492

Column Sum:

24.4 26.262 19.83

Column Mean:

6.1 6.5655 4.9575
【说明】
请在以下代码的基础上编写:
#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 Matrix_ColumnCalculation(float ** arr, int rows, int cols, float * pColumnSum, float * pColumnMean);

int main(void){
// 请在此处写入相关的输入语句
// !并且禁止使用固定数组
// 数组名使用arr

// 请不要修改以下内容
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;

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);

// 请在此处输入相关的处理



return 0;
}




上一篇:syc站长界面开发第二步的问题
下一篇:关于CWPSTRUCT成员的疑惑
77_avatar_middle
最佳答案
31 
online_vip 发表于 2020-3-3 17:06:18 | 显示全部楼层
QQQin 发表于 2020-3-3 16:32
所以您能帮我看一下吗?谢谢您。

大体上这样吧,正好手头有时间帮你写了一份,你试试!

  1. #include <iostream>
  2. using std::endl;
  3. using std::cin;
  4. using std::cout;

  5. void Print1DArray(const float * arr, int length){
  6.         for (int i = 0; i < length; ++i){
  7.                 cout << arr[i] << " ";
  8.         }
  9.         cout << endl;
  10. }
  11. void Print2DArray(float ** arr, int rows, int cols){
  12.         cout << rows << " " << cols << endl;
  13.         for (int i = 0; i < rows; ++i){
  14.                 for (int j = 0; j < cols; ++j)
  15.                         cout << arr[i][j] << " ";
  16.                 cout << endl;
  17.         }
  18. }

  19. float Matrix_RowCalculation(float ** arr, int rows, int cols, float * pRowSum, float * pRowMean)
  20. {
  21.         float sum = 0.0;
  22.         for (int i = 0; i < rows; ++i)
  23.         {
  24.                 for (int j = 0; j < cols; ++j)
  25.                 {
  26.                         pRowSum[i] += arr[i][j];
  27.                         sum += arr[i][j];
  28.                 }

  29.                 pRowMean[i] = pRowSum[i] / cols;
  30.         }

  31.         return sum;
  32. }

  33. float Matrix_ColumnCalculation(float ** arr, int rows, int cols, float * pColumnSum, float * pColumnMean)
  34. {
  35.         float sum = 0.0;
  36.         for (int i = 0; i < cols; ++i)
  37.         {
  38.                 for (int j = 0; j < rows; ++j)
  39.                 {
  40.                         pColumnSum[i] += arr[j][i];
  41.                         sum += arr[j][i];
  42.                 }

  43.                 pColumnMean[i] = pColumnSum[i] / rows;
  44.         }

  45.         return sum;
  46. }

  47. int main(void)
  48. {
  49.         // 请在此处写入相关的输入语句
  50.         // !并且禁止使用固定数组
  51.         // 数组名使用arr

  52.         cout << "Please input matrix rows and columns : " << endl;

  53.         int rows = 0, cols = 0;
  54.         cin >> rows >> cols;

  55.         cout << "Please input nums : " << endl;

  56.         float **arr = new float*[rows];
  57.         for (int i = 0; i < rows; i++)
  58.                 arr[i] = new float[cols];

  59.         for (int i = 0; i < rows; ++i)
  60.         {
  61.                 for (int j = 0; j < cols; ++j)
  62.                 {
  63.                         cin >> arr[i][j];
  64.                 }
  65.         }

  66.         cout << "Please matrix is : " << endl;
  67.         float* pRowSum = new float[cols];
  68.         memset(pRowSum, 0, sizeof(float) * cols);

  69.         float* pRowMean = new float[cols];
  70.         memset(pRowMean, 0, sizeof(float) * cols);

  71.         //// 请不要修改以下内容
  72.         Print2DArray(arr, rows, cols);
  73.         cout << "========================================" << endl << endl;

  74.         cout << "==Row Calculation==" << endl
  75.                 << "All Sum:" << Matrix_RowCalculation(arr, rows, cols, pRowSum, pRowMean) << endl;
  76.         cout << "Row Sum:" << endl;
  77.         Print1DArray(pRowSum, cols);
  78.         cout << "Row Mean:" << endl;
  79.         Print1DArray(pRowMean, cols);

  80.         cout << "========================================" << endl << endl;

  81.         float* pColumnSum = new float[rows];
  82.         memset(pColumnSum, 0, sizeof(float) * rows);

  83.         float* pColumnMean = new float[rows];
  84.         memset(pColumnMean, 0, sizeof(float) * rows);

  85.         cout << "==Column Calculation==" << endl
  86.                 << "All Sum:" << Matrix_ColumnCalculation(arr, rows, cols, pColumnSum, pColumnMean) << endl;
  87.         cout << "Column Sum:" << endl;
  88.         Print1DArray(pColumnSum, rows);
  89.         cout << "Column Mean:" << endl;
  90.         Print1DArray(pColumnMean, rows);

  91.         // 请在此处输入相关的处理

  92.         delete[] pRowSum;
  93.         delete[] pRowMean;

  94.         delete[] pColumnSum;
  95.         delete[] pColumnMean;

  96.         for (int i = 0; i < rows; ++i)
  97.                 delete[] arr[i];
  98.         delete[] arr;

  99.         return 0;
  100. }
复制代码

评分

参与人数 1驿站币 +2 热心值 +2 收起 理由
58_avatar_small thzzl + 2 + 2 很给力!

查看全部评分

77_avatar_middle
最佳答案
31 
online_vip 发表于 2020-3-3 16:24:04 | 显示全部楼层
这个题目不难吧,就是麻烦,怎么有点类似作业呢。。。
楼主来要求大家帮你做作业?求助这道二维数组的题,谢谢大家!
41_avatar_middle
最佳答案
0 
ico_lz  楼主| 发表于 2020-3-3 16:26:01 | 显示全部楼层
Health 发表于 2020-3-3 16:24
这个题目不难吧,就是麻烦,怎么有点类似作业呢。。。
楼主来要求大家帮你做作业?

不是作业......是一道思考题,还没学动态数组,想看看到底怎么做.......谢谢您!
41_avatar_middle
最佳答案
0 
ico_lz  楼主| 发表于 2020-3-3 16:30:46 | 显示全部楼层
QQQin 发表于 2020-3-3 16:26
不是作业......是一道思考题,还没学动态数组,想看看到底怎么做.......谢谢您!

所以您能帮我吗?
41_avatar_middle
最佳答案
0 
ico_lz  楼主| 发表于 2020-3-3 16:32:04 | 显示全部楼层
Health 发表于 2020-3-3 16:24
这个题目不难吧,就是麻烦,怎么有点类似作业呢。。。
楼主来要求大家帮你做作业?

所以您能帮我看一下吗?谢谢您。
您需要登录后才可以回帖 登录 | 加入驿站 qq_login

本版积分规则

×【发帖 友情提示】
1、请回复有意义的内容,请勿恶意灌水;
2、纯数字、字母、表情等无意义的内容系统将自动删除;
3、若正常回复后帖子被自动删除,为系统误删的情况,请重新回复其他正常内容或等待管理员审核通过后会自动发布;
4、感谢您对VC驿站一如既往的支持,谢谢合作!

关闭

站长提醒上一条 /2 下一条

QQ|小黑屋|手机版|VC驿站 ( 辽ICP备09019393号-4 )|网站地图wx_jqr

GMT+8, 2023-5-29 23:45

Powered by CcTry.CoM

© 2009-2021 cctry.com

快速回复 返回顶部 返回列表