This article is a mirror article of machine translation, please click here to jump to the original article.

View: 14889|Reply: 0

[C] Pointer method Enter 10 numbers, swap the smallest number with the first number, and the largest number with the last number.

[Copy link]
Posted on 12/15/2015 2:19:52 PM | | |
  1. #include "stdafx.h"
  2. #include "stdio.h"
  3. int main(int argc, char* argv[])
  4. {void input(int *number);
  5. void maxmin(int *number);
  6. void output(int *number);
  7. int number[10];
  8. input(number);
  9. maxmin(number);
  10. output(number);
  11. return 0;
  12. }
  13. void input(int *number)
  14. {printf("please input 10 integer numbers:");
  15. int i;
  16.     for(i=0;i<10;i++)
  17.     scanf("%d",&number<i>);
  18. }
  19. void maxmin(int *number) //交换函数
  20. {int *max, *min,*p,temp;  //定义最大最小,存储变量
  21. max=min=number;   //开始是最大最小值指向第一个数
  22. for(p=number+1;p<number+10;p++)//循环他的第一个数
  23. if(*p>*max)max=p;        //若p指向的数字大于max指向的数,就使max指向p指向的大数字
  24. else if(*p<*min)min=p;   //若p指向的数字小于min指向的数,就使min指向p指向的小数字
  25. temp=number[0];number[0]=*min;*min=temp; //将最小数与第一个数number【0】交换
  26. if(max==number) max=min; //如果max和number相等,,表示第一个数是最大数,则使max指向当前的最大数
  27. temp=number[9];number[9]=*max;*max=temp;//将最大数与最后一个数交换
  28. }
  29. void output(int *number)
  30. {int *p;
  31. printf("now,they are:   ");
  32. for(p=number;p<number+10;p++)
  33. printf("%d,",*p);
  34. printf("\n");
  35. }</i>
Copy code

This function is still relatively complex, it uses three functions, and uses the pointer method to solve it






Previous:The external link rejection function of the Baidu webmaster platform is gone, and the hyperchain algorithm has been upgraded
Next:Pointer method Input 3 strings and output them in order from smallest to largest
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com