用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\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.bkjia.com @linux:~/c编程$ gcc  example.c -o example
www.bkjia.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

  • 1
  • 2
  • 下一页

相关内容