资源描述:
《成都信息工程学院C语言题库答案.pdf》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、第九次课上课程序及习题参考程序1、一维数组的下标法访问:intmain(void){inta[10];inti;for(i=0;i<10;i++){scanf("%d",&a[i]);}for(i=0;i<10;i++){printf("%d",a[i]);}return0;}2、一维数组的指针法访问:intmain(void){inta[10];int*p;for(p=a;p<(a+10);p++){scanf("%d",p);}for(p=a;p<(a+10);p++){printf("%d",*p
2、);}return0;}3、冒泡排序中指针做形参#includevoidbubble_sort(intarr[],intn);intmain(void){intarr[10],i;printf("Pleaseinput10numbers:");for(i=0;i<10;i++)1{scanf("%d",&arr[i]);}bubble_sort(arr,10);printf("Thesortednumbers:");for(i=0;i<10;i++){printf("%4d",
3、arr[i]);}printf("");return0;}voidbubble_sort(int*p,intn){inti,j,temp;for(i=0;ip[j+1]){temp=p[j];p[j]=p[j+1];p[[j+1]=temp;}}}}4、有序表的有序插入#includevoidinsert(inta[],intn,intnum);intmain(void){inta[6],num,i;pr
4、intf("Inputarray(5):");for(i=0;i<5;i++){scanf("%d",&a[i]);2}printf("Inputtheinsertnum:");scanf("%d",&num);insert(a,6,num);printf("Output:");for(i=0;i<6;i++){printf("%d",a[i]);}printf("");return0;}从前向后找voidinsert(inta[],intn,intnum){inti,j;i=0;while
5、(num>a[i])/*寻找位置*/{i++;}for(j=n-1;j>i;j--)/*移动*/{a[j]=a[j-1];}a[i]=num;/*插入*/}从后向前找voidinsert(inta[],intn,intnum){inti;i=n-1;while(i>0&&num3voidinsert(inta[],intn,intnum);intmain(void){in
6、ta[6],num,i;printf("Inputarray(5):");for(i=0;i<5;i++){scanf("%d",&a[i]);}printf("Inputtheinsertnum:");scanf("%d",&num);insert(a,6,num);printf("Output:");for(i=0;i<6;i++){printf("%d",a[i]);}printf("");return0;}voidinsert(int*p,intn,intnum){inti;i=n-
7、1;while(i>0&&num
intmain(void){intarr[20][20];inti,j,m,sum;printf("Pleaseinputm:");scanf("%d",&m);4printf("Pleaseinputarray:");for(i=0;i8、;j9、
10、i+j==m-1){sum+=arr[i][j];}}}printf("sum=%d",sum);return0;}使用行指针和列指针:#includeintmain(void){intarr[20][20],*p1,(*p2)[20];inti,j