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

题目:随机生成1024个数,存入一段内存,用指针实现获取1024个数的最大数地址,最小数地址,具体实现如下:

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. #include<stdlib.h>  
  2. #include<stdio.h>  
  3. #include<time.h>  
  4. int FindMaxMinByIndex(int * pInput, int nLen, int ** ppMax, int ** ppMin)  
  5. {  
  6.     if (!ppMax)  
  7.     {  
  8.         return 0;  
  9.     }  
  10.     if (!ppMin)  
  11.     {  
  12.         return 0;  
  13.     }  
  14.     if (!pInput)  
  15.     {  
  16.         return 0;  
  17.     }  
  18.     int i = 0;  
  19.     int * pMax = &pInput[0];  
  20.     int * pMin = &pInput[0];  
  21.     for (i = 0; i < nLen; i++)  
  22.     {  
  23.         if (*pMax < pInput[i])  
  24.         {  
  25.             pMax = pInput + i;  
  26.         }  
  27.         if (*pMin > pInput[i])  
  28.         {  
  29.             pMin = pInput + i;  
  30.         }  
  31.     }  
  32.     *ppMax = pMax;  
  33.     *ppMin = pMin;  
  34.     return 1;  
  35. }  
  36. int FindMaxMinByPtr(int * pInput, int nLen, int ** ppMax, int ** ppMin)  
  37. {  
  38.     if (!pInput || !ppMax || !ppMin)  
  39.     {  
  40.         return 0;  
  41.     }  
  42.     int * pCurPtr = pInput;  
  43.     int * pEndPtr = pInput + nLen;  
  44.     int * pMax = pInput;  
  45.     int * pMin = pInput;  
  46.     for (; pCurPtr < pEndPtr; pCurPtr++)  
  47.     {  
  48.         if (*pMax <*pCurPtr)  
  49.         {  
  50.             pMax = pCurPtr;  
  51.         }  
  52.         if (*pMin > *pCurPtr)  
  53.         {  
  54.             pMin = pCurPtr;  
  55.         }  
  56.     }  
  57.     *ppMax = pMax;  
  58.     *ppMin = pMin;  
  59.     return 1;  
  60. }  
  61. int main()  
  62. {  
  63.     int Data[1024] = { 0 };  
  64.     int i = 0;  
  65.     srand(time(NULL));  
  66.     for (i = 0; i < 1024; i++)  
  67.     {  
  68.         Data[i] = rand() % 10;  
  69.     }  
  70.     printf("数据:\n");  
  71.     for (i = 0; i < 1024; i++)  
  72.     {  
  73.         printf("%d  ", Data[i]);  
  74.     }  
  75.     printf("\n");  
  76.     int * pMax = NULL;  
  77.     int * pMin = NULL;  
  78.     if (FindMaxMinByIndex(Data, 1024, &pMax, &pMin) == 0)  
  79.     {  
  80.         printf("查找失败.\n");  
  81.     }  
  82.     else  
  83.     {  
  84.         printf("最大数:%d,最小数:%d,最大数地址:%p,最小数地址:%p\n", *pMax, *pMin, pMax, pMin);  
  85.     }  
  86.     if (FindMaxMinByPtr(Data, 1024, &pMax, &pMin) == 0)  
  87.     {  
  88.         printf("查找失败.\n");  
  89.     }  
  90.     else  
  91.     {  
  92.         printf("最大数:%d,最小数:%d,最大数地址:%p,最小数地址:%p\n", *pMax, *pMin, pMax, pMin);  
  93.     }  
  94.   
  95.     system("pause");  
  96.     return 0;  
  97. }  
运行效果如图1所示:


图1 运行效果
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值