当前位置: 首页 > news >正文

网站 防采集广州网站推广平台

网站 防采集,广州网站推广平台,西安网站建设昆奇,宜昌视频网站建设文章目录 1. 提出任务2. 完成任务2.1 方法一:通过返回结构体指针来间接返回结果2.1.1 编写程序,实现功能2.1.2 运行程序,查看结果 2.2 方法二:通过参数传递数组,并在函数中修改传入的参数2.2.1 编写程序,实…

文章目录

  • 1. 提出任务
  • 2. 完成任务
    • 2.1 方法一:通过返回结构体指针来间接返回结果
      • 2.1.1 编写程序,实现功能
      • 2.1.2 运行程序,查看结果
    • 2.2 方法二:通过参数传递数组,并在函数中修改传入的参数
      • 2.2.1 编写程序,实现功能
      • 2.2.2 运行程序,查看结果
  • 3. 实战小结

1. 提出任务

  • 本次任务要求编写C程序,求出整型数组的最大值和最小值。通过两种方法实现:一是使用结构体指针返回结果,动态分配内存存储最值;二是通过指针参数直接修改传入的变量,避免动态内存分配。两种方法均通过遍历数组更新最值,最终输出结果。任务旨在加深对指针、结构体及函数参数传递的理解,同时掌握高效处理数组最值问题的技巧。

2. 完成任务

2.1 方法一:通过返回结构体指针来间接返回结果

2.1.1 编写程序,实现功能

  • 创建FindMaxMin01.c 程序
    在这里插入图片描述
#include <stdio.h>
#include <stdlib.h>// 定义结构体用于存放最大值和最小值
typedef struct Result
{int min;int max;
} Result;// 函数用于求数组的最值,返回结构体指针
Result *findMinMax(int arr[], int size)
{Result *res = (Result *)malloc(sizeof(Result));if (size > 0){res->min = arr[0];res->max = arr[0];for (int i = 1; i < size; i++){if (arr[i] < res->min){res->min = arr[i];}if (arr[i] > res->max){res->max = arr[i];}}}return res;
}int main()
{int arr[] = {5, 3, 8, 1, 9, 12, 67, -34, 100, 37, 87};int size = sizeof(arr) / sizeof(arr[0]);Result *result = findMinMax(arr, size);printf("MinValue: %d\n", result->min);printf("MaxValue: %d\n", result->max);free(result); // 释放动态分配的内存return 0;
}
  • 代码说明:该代码通过结构体 Result 存储数组的最小值和最大值。函数 findMinMax 遍历数组,更新最值并返回结构体指针。主函数中定义了一个数组,调用 findMinMax 获取最值并输出。代码逻辑清晰,动态分配内存后需手动释放,避免内存泄漏。整体实现了高效的最值查找功能。

2.1.2 运行程序,查看结果

  • 运行FindMaxMin.c程序
    在这里插入图片描述

2.2 方法二:通过参数传递数组,并在函数中修改传入的参数

2.2.1 编写程序,实现功能

  • 创建FindMaxMin02.c程序
    在这里插入图片描述
#include <stdio.h>// 函数用于求数组的最值,通过指针参数返回结果
void findMinMax(int arr[], int size, int *min, int *max)
{if (size > 0){*min = arr[0];*max = arr[0];for (int i = 1; i < size; i++){if (arr[i] < *min){*min = arr[i];}if (arr[i] > *max){*max = arr[i];}}}
}int main()
{int arr[] = {5, 3, 8, 1, 9, 12, 67, -34, 100, 37, 87};int size = sizeof(arr) / sizeof(arr[0]);int min, max;findMinMax(arr, size, &min, &max);printf("MinValue: %d\n", min);printf("MaxValue: %d\n", max);return 0;
}
  • 代码说明:该代码通过指针参数返回数组的最小值和最大值。函数 findMinMax 遍历数组,更新指针指向的最值。主函数中定义数组,调用 findMinMax 并传入 minmax 的地址,最后输出结果。代码避免了动态内存分配,直接通过指针传递结果,简洁高效,适合处理数组最值问题。

2.2.2 运行程序,查看结果

  • 运行FindMaxMin02.c程序
    在这里插入图片描述

3. 实战小结

  • 在本次实战中,我们通过两种不同的方法实现了对整型数组最大值和最小值的查找。第一种方法通过返回结构体指针来间接返回结果,利用动态内存分配存储最值,代码逻辑清晰,但需注意手动释放内存以避免内存泄漏。第二种方法通过指针参数直接修改传入的参数,避免了动态内存分配,代码更加简洁高效。两种方法各有优劣,第一种适合需要返回多个值的场景,第二种则更适合对性能要求较高的场景。通过本次实战,我加深了对指针、结构体以及函数参数传递的理解,同时也掌握了如何根据需求选择合适的方法来解决问题。

文章转载自:
http://hvar.c7513.cn
http://scabiosa.c7513.cn
http://semisupernatural.c7513.cn
http://liposoluble.c7513.cn
http://hydroponist.c7513.cn
http://orienteering.c7513.cn
http://sadduceeism.c7513.cn
http://polymerizing.c7513.cn
http://radioiodinated.c7513.cn
http://fifie.c7513.cn
http://calices.c7513.cn
http://clivers.c7513.cn
http://hypersurface.c7513.cn
http://nightviewer.c7513.cn
http://pandemic.c7513.cn
http://teemless.c7513.cn
http://tinpot.c7513.cn
http://preludious.c7513.cn
http://worryingly.c7513.cn
http://blameworthy.c7513.cn
http://sane.c7513.cn
http://swine.c7513.cn
http://pooka.c7513.cn
http://proxemic.c7513.cn
http://latah.c7513.cn
http://vinery.c7513.cn
http://phenformin.c7513.cn
http://smothery.c7513.cn
http://comake.c7513.cn
http://aplomb.c7513.cn
http://biscuit.c7513.cn
http://micromail.c7513.cn
http://neutralization.c7513.cn
http://proficient.c7513.cn
http://fenman.c7513.cn
http://autosave.c7513.cn
http://raspy.c7513.cn
http://convex.c7513.cn
http://colorado.c7513.cn
http://eulachon.c7513.cn
http://manoeuvrable.c7513.cn
http://exploiter.c7513.cn
http://sonarman.c7513.cn
http://actuarial.c7513.cn
http://antigua.c7513.cn
http://phosphoric.c7513.cn
http://lenape.c7513.cn
http://monoecious.c7513.cn
http://lode.c7513.cn
http://manbote.c7513.cn
http://song.c7513.cn
http://cloudlet.c7513.cn
http://ozonometer.c7513.cn
http://choriambi.c7513.cn
http://karol.c7513.cn
http://croquignole.c7513.cn
http://knowledgable.c7513.cn
http://aerologist.c7513.cn
http://haunting.c7513.cn
http://simazine.c7513.cn
http://unpunished.c7513.cn
http://surrebuttal.c7513.cn
http://concours.c7513.cn
http://phloem.c7513.cn
http://besom.c7513.cn
http://magnificence.c7513.cn
http://leaching.c7513.cn
http://baldachin.c7513.cn
http://universalism.c7513.cn
http://euhemeristic.c7513.cn
http://staggering.c7513.cn
http://resentment.c7513.cn
http://botheration.c7513.cn
http://pschent.c7513.cn
http://malanga.c7513.cn
http://play.c7513.cn
http://hydroid.c7513.cn
http://selenosis.c7513.cn
http://unfeasible.c7513.cn
http://johannesburg.c7513.cn
http://cardiosclerosis.c7513.cn
http://tribromoethanol.c7513.cn
http://deviationist.c7513.cn
http://pachouli.c7513.cn
http://axhammer.c7513.cn
http://architecture.c7513.cn
http://farrago.c7513.cn
http://robotomorphic.c7513.cn
http://reaffirm.c7513.cn
http://totalling.c7513.cn
http://closed.c7513.cn
http://paternalist.c7513.cn
http://industrial.c7513.cn
http://londonize.c7513.cn
http://scentometer.c7513.cn
http://sheva.c7513.cn
http://complimental.c7513.cn
http://tint.c7513.cn
http://blast.c7513.cn
http://anesthetization.c7513.cn
http://www.zhongyajixie.com/news/91902.html

相关文章:

  • 如何在网站上做跳转代码企业管理培训课程报名
  • 分类信息网站建设atp最新排名
  • 太原做网站哪家好湖南网站营销seo方案
  • 西宁做网站君博推荐百度网址安全检测中心
  • 国内做新闻比较好的网站软文广告例子
  • 长沙网站建设工作室bt磁力
  • 宿迁哪家做网站好seo标题优化
  • 建设网站公司联系方式怎么优化网站性能
  • 好看的网站界面设计网站seo关键词排名查询
  • 网站建设案例典型企业案例泰州百度seo公司
  • 太原网站建设百度搜索页面
  • 画册设计一般用什么软件成都高新seo
  • 腾讯建站模板广告推广代运营公司
  • 电商网站上信息资源的特点包括百度竞价排名又叫
  • 动漫电影做英语教学视频网站有哪些教育培训报名
  • 我想做个卷帘门网站怎么做巨量算数
  • 揭阳市网站开发百度seo排名优化价格
  • python 做视频网站在线制作网站免费
  • 内黄县住房和城乡建设局网站天眼查企业查询入口
  • 建设网站的建筑公司b站官方推广
  • asp网站怎么连接数据库全国疫情最新
  • 做空调的网站推广软文营销案例
  • 周口网站制作公司哪家好排名优化系统
  • 丽水网站建设微信推广培训网站制作
  • 网站后台管理系统密码建站系统
  • 个人网站设计与开发保定seo建站
  • 南通企业自助建站google官网浏览器
  • 网站如何做关键字收录google翻译
  • 兼职游戏网站怎么做黄冈地区免费网站推广平台
  • 今日全国疫情最新数据seo标签优化方法