手机版
你好,游客 登录 注册
背景:
阅读新闻

用C语言求一组数组的最大值以及最小值

[日期:2013-01-04] 来源:Linux社区  作者:xiongyaoqiongyao [字体:大 中 小]

用C语言求一组数组的最大值以及最小值

#include<stdio.h>//stdio.h是c的标准的i/o库,是以函数的方式向buffer写入或读取字符,iostream.h是c++的标准i/o库,引入了输入/输出流的概念,是一个类库,是以类方法从streambuf中读取,写入字符。
int max=0;
int min=1000;
void change(int a[],int n)
{ 
  int i,j,k;
  for(i=1;i<n;i++)
  if(a[i]<min)
    {
    min=a[i];
    j=i;
    }
  for(i=1;i<n;i++)
  if(a[i]>max)
    {
    max=a[i];
      k=i;
    }
a[k]=min;
a[j]=max;
printf("the position of min is:%3d\n",j);
printf("the position of max is:%3d\n",k);
printf("Now the array is:\n");
for(i=0;i<n;i++)
printf("%5d",a[i]);
}
 main()
{
int a[20],i,n;
printf("please input the number of elements:\n");
scanf("%d",&n);
printf("please imput the elements:\n");
for(i=0;i<n;i++)
  scanf("%d",&a[i]);
change(a,n);
printf("\nmax=%d\nmin=%d\n",max,min);
}

www.linuxidc.com @linux:~/c编程$ gcc  example.c -o example
www.linuxidc.com @linux:~/c编程$ ./example
please input the number of elements:
3
please imput the elements:
1
3
5
the position of min is:  0
the position of max is:  2
Now the array is:
    5    3    1
max=5
min=1

linux
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款