日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

随机生成1024个数,存入一段内存,用指针实现获取1024个数的最大数地址,最小数地址

發布時間:2023/11/30 编程问答 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 随机生成1024个数,存入一段内存,用指针实现获取1024个数的最大数地址,最小数地址 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

http://blog.csdn.net/itcastcpp//details/39277193

題目:隨機生成1024個數,存入一段內存,用指針實現獲取1024個數的最大數地址,最小數地址,具體實現如下:

[cpp]?view plain?copy
  • #include<stdlib.h>??
  • #include<stdio.h>??
  • #include<time.h>??
  • int?FindMaxMinByIndex(int?*?pInput,?int?nLen,?int?**?ppMax,?int?**?ppMin)??
  • {??
  • ????if?(!ppMax)??
  • ????{??
  • ????????return?0;??
  • ????}??
  • ????if?(!ppMin)??
  • ????{??
  • ????????return?0;??
  • ????}??
  • ????if?(!pInput)??
  • ????{??
  • ????????return?0;??
  • ????}??
  • ????int?i?=?0;??
  • ????int?*?pMax?=?&pInput[0];??
  • ????int?*?pMin?=?&pInput[0];??
  • ????for?(i?=?0;?i?<?nLen;?i++)??
  • ????{??
  • ????????if?(*pMax?<?pInput[i])??
  • ????????{??
  • ????????????pMax?=?pInput?+?i;??
  • ????????}??
  • ????????if?(*pMin?>?pInput[i])??
  • ????????{??
  • ????????????pMin?=?pInput?+?i;??
  • ????????}??
  • ????}??
  • ????*ppMax?=?pMax;??
  • ????*ppMin?=?pMin;??
  • ????return?1;??
  • }??
  • int?FindMaxMinByPtr(int?*?pInput,?int?nLen,?int?**?ppMax,?int?**?ppMin)??
  • {??
  • ????if?(!pInput?||?!ppMax?||?!ppMin)??
  • ????{??
  • ????????return?0;??
  • ????}??
  • ????int?*?pCurPtr?=?pInput;??
  • ????int?*?pEndPtr?=?pInput?+?nLen;??
  • ????int?*?pMax?=?pInput;??
  • ????int?*?pMin?=?pInput;??
  • ????for?(;?pCurPtr?<?pEndPtr;?pCurPtr++)??
  • ????{??
  • ????????if?(*pMax?<*pCurPtr)??
  • ????????{??
  • ????????????pMax?=?pCurPtr;??
  • ????????}??
  • ????????if?(*pMin?>?*pCurPtr)??
  • ????????{??
  • ????????????pMin?=?pCurPtr;??
  • ????????}??
  • ????}??
  • ????*ppMax?=?pMax;??
  • ????*ppMin?=?pMin;??
  • ????return?1;??
  • }??
  • int?main()??
  • {??
  • ????int?Data[1024]?=?{?0?};??
  • ????int?i?=?0;??
  • ????srand(time(NULL));??
  • ????for?(i?=?0;?i?<?1024;?i++)??
  • ????{??
  • ????????Data[i]?=?rand()?%?10;??
  • ????}??
  • ????printf("數據:\n");??
  • ????for?(i?=?0;?i?<?1024;?i++)??
  • ????{??
  • ????????printf("%d??",?Data[i]);??
  • ????}??
  • ????printf("\n");??
  • ????int?*?pMax?=?NULL;??
  • ????int?*?pMin?=?NULL;??
  • ????if?(FindMaxMinByIndex(Data,?1024,?&pMax,?&pMin)?==?0)??
  • ????{??
  • ????????printf("查找失敗.\n");??
  • ????}??
  • ????else??
  • ????{??
  • ????????printf("最大數:%d,最小數:%d,最大數地址:%p,最小數地址:%p\n",?*pMax,?*pMin,?pMax,?pMin);??
  • ????}??
  • ????if?(FindMaxMinByPtr(Data,?1024,?&pMax,?&pMin)?==?0)??
  • ????{??
  • ????????printf("查找失敗.\n");??
  • ????}??
  • ????else??
  • ????{??
  • ????????printf("最大數:%d,最小數:%d,最大數地址:%p,最小數地址:%p\n",?*pMax,?*pMin,?pMax,?pMin);??
  • ????}??
  • ??
  • ????system("pause");??
  • ????return?0;??
  • }??
  • 運行效果如圖1所示:

    總結

    以上是生活随笔為你收集整理的随机生成1024个数,存入一段内存,用指针实现获取1024个数的最大数地址,最小数地址的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。