Welcome 微信登录
编程资源 图片资源库 蚂蚁家优选 PDF转换器

首页 / 操作系统 / Linux / 用C语言求一组数组的最大值以及最小值

用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 ",j);
printf("the position of max is:%3d ",k);
printf("Now the array is: ");
for(i=0;i<n;i++)
printf("%5d",a[i]);
}
 main()
{
int a[20],i,n;
printf("please input the number of elements: ");
scanf("%d",&n);
printf("please imput the elements: ");
for(i=0;i<n;i++)
  scanf("%d",&a[i]);
change(a,n);
printf(" max=%d min=%d ",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