鱼C论坛

 找回密码
 立即注册
查看: 1204|回复: 6

[已解决]编写函数fun

[复制链接]
发表于 2020-6-28 10:53:44 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
请编写函数fun,其功能是:找出2×M整型二维数组中最大元素的值,并将此值返回调用函数。

注意:部分源程序给出如下。请勿改动主函数main和其他函数中的任何内容,仅在函数fun的花括号中填入你编写的若干语句。

#include <stdio.h>

#define M 4

int fun(int a[][M])

{







}

int main()

{  

int arr[2][M] = {5, 8, 3, 45, 76, -4, 12, 82};

printf("max = %d\n", fun(arr));

return 0;

}
最佳答案
2020-6-28 11:05:31
  1. #include <stdio.h>

  2. #define M 4

  3. int fun(int a[][M])

  4. {

  5. int i=0, *ptemp=(int *)a,temp=a[0][0];
  6. for (i=0;i<2*M;i++)
  7. {
  8. if(temp<*ptemp) temp=*ptemp;
  9. ptemp++;
  10. }


  11. return temp;
  12. }

  13. int main()

  14. {  

  15. int arr[2][M] = {5, 8, 3, 45, 76, -4, 12, 82};

  16. printf("max = %d\n", fun(arr));

  17. return 0;

  18. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-6-28 11:05:31 | 显示全部楼层    本楼为最佳答案   
  1. #include <stdio.h>

  2. #define M 4

  3. int fun(int a[][M])

  4. {

  5. int i=0, *ptemp=(int *)a,temp=a[0][0];
  6. for (i=0;i<2*M;i++)
  7. {
  8. if(temp<*ptemp) temp=*ptemp;
  9. ptemp++;
  10. }


  11. return temp;
  12. }

  13. int main()

  14. {  

  15. int arr[2][M] = {5, 8, 3, 45, 76, -4, 12, 82};

  16. printf("max = %d\n", fun(arr));

  17. return 0;

  18. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-6-28 11:05:38 | 显示全部楼层
本帖最后由 永恒的蓝色梦想 于 2020-6-28 12:17 编辑
  1. #include <stdio.h>

  2. #define M 4

  3. int fun(int a[][M])

  4. {
  5.     int* ptr = *a, max = *ptr++;

  6.     for (int count = M << 1; --count; ++ptr) {
  7.         if (*ptr > max) {
  8.             max = *ptr;
  9.         }
  10.     }

  11.     return max;
  12. }

  13. int main()

  14. {

  15.     int arr[2][M] = { 5, 8, 3, 45, 76, -4, 12, 82 };

  16.     printf("max = %d\n", fun(arr));

  17.     return 0;

  18. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-6-28 11:30:00 | 显示全部楼层

感觉你代码有点小问题
  1. #include <stdio.h>

  2. #define M 4

  3. int fun(int a[][M])

  4. {
  5.     int* ptr = *a, max = *ptr++;

  6.     for (int count = (M << 1); --count; ++ptr) {//这里左移一位 ,由于 count到0不运行 前面不要减一
  7.         if (*ptr > max) {
  8.             max = *ptr;
  9.         }
  10.     }

  11.     return max;
  12. }

  13. int main()

  14. {

  15.     int arr[2][M] = { 5, 8, 3, 45, 76, -4, 12, 82 };

  16.     printf("max = %d\n", fun(arr));

  17.     return 0;

  18. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-6-28 12:00:11 | 显示全部楼层
jhanker 发表于 2020-6-28 11:30
感觉你代码有点小问题

哦……失误
把两个减少循环次数的方法用在一起了……
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-6-28 12:16:30 | 显示全部楼层

M << 2 也有问题应该是M << 1 吧
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-6-28 12:18:01 | 显示全部楼层

今天脑子坏了……
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-7-7 09:40

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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