4、n])min=j;t=a[i];a[i]=a[min];a[min]=t;}}main(){inti,j,min;scanf("%d",&n);for(i=1;i<=n;i++)scanf("%d",&a[i]);//选择排序paixu();for(i=1;i<=n;i++)printf("%dt",a[i]);printf("");getch();}实例2:两个数对调的函数实现(值传递和地址传递)#include#includeinta=3,b=4;voidswap1(inta,intb){//值
5、传递,也成为单向传递intt;t=a;a=b;b=t;}voidswap2(int*a,int*b){//地址传递,也成为双向传递intt;t=*a;*a=*b;*b=t;}main(){swap1(a,b);printf("a=%d,b=%d",a,b);swap2(&a,&b);printf("a=%d,b=%d",a,b);getch();}三、文件的读写操作:文件读写操作的一般格式:FILE*fp1,fp2;fp1=fopen(“文件名”,”r”);fp2=fopen(“文件名”,”w”);fscanf(fp1,”…”,…)
6、;fprintf(fp2,”….”,….);fclose(fp1);fclose(fp2);注意:文件在程序结束时,必须关闭。实例3:从文件data.in中读入n个数并求和,将和输出到文件data.out中(两个文件均是文本文件)#include#includemain(){FILE*fp1,*fp2;fp1=fopen("data.in","r");fp2=fopen("data.out","w");inta[20],i,n,s=0;;fscanf(fp1,"%d",&n);for(i=1;i<=n;i
7、++){fscanf(fp1,"%d",&a[i]);s=s+a[i];}printf("%d",s);getch();fprintf(fp2,"%d",s);fclose(fp1);fclose(fp2);}四、练习:1、编程打印杨辉三角形(第0层至第n(n<=20)层的所有元素值,n从屏幕输入)杨辉三角形111121133114641……………………参考源程序:#include#includemain(){longa[30][30];inti,j,n;scanf("%d",&n);for(i=
8、0;i<=n;i++){a[i][0]=1;a[i][i]=1;}for(i=2;i<=n;i++){for(j=1;j