资源描述:
《C语言程序 实例》由会员上传分享,免费在线阅读,更多相关内容在应用文档-天天文库。
1、数组的引用:#includevoidmain(intargc,char*argv[]){intx[5]={2,3,1,4,6};inti;for(i=0;i<5;i++){printf("%d",2*x[i]);}}一维数组的遍历:#includevoidmain(intargc,char*argv[]){intx[5]={1,2,3,4,5};inti=0;for(i=0;i<5;i++){printf("x[%d]:%d",i,x[i]);}}一维数组的最值:#includeintm
2、ain(intargc,char*argv[]){intx[5]={1,2,3,4,5};intnMax=x[0];inti=0;for(i=1;i<5;i++){if(x[i]>nMax){nMax=x[i];}}printf("max:%d",nMax);return0;}一维数组排序:#includevoidmain(intargc,char**argv[]){intarr[5]={9,8,3,5,2};inttemp=0;inti,j;printf("冒泡排序前:");for(i=0;i<5;i++){printf
3、("%d",arr[i]);}printf("");for(i=0;i<5-1;i++){for(j=0;j<5-1-i;j++){if(arr[j]>arr[j+1]){temp=arr[j];arr[j]=arr[j+1];arr[j+1]=temp;}}}printf("冒泡排序后:");for(i=0;i<5;i++){printf("%d",arr[i]);}printf("");}二维数组的遍历#includevoidmain(){//声明并初始化数组intarray[3][4]={{1,2,3,4,},{
4、5,6,7,8,},{9,10,11,12}};for(inti=0;i<3;i++)//循环遍历行{for(intj=0;j<4;j++)//循环遍历列{printf("[%d][%d]:%d",i,j,array[i][j]);}printf("");//每一行的末尾添加换行符}}二维数组应用#includevoidmain(intargc,char*argv[]){intStuScore[5][3]={//张同学{88,70,90},//王同学{80,80,60},//李同学{89,60,85},//赵同学{80,75,
5、78},//周同学{70,80,80},};intm=0,n=0;intnStuTotalScore;intnMathTotalScore=0;intnChineseTotalScore=0;intnEnglishTotalScore=0;printf("个人总成绩:");for(m=0;m<5;m++){nStuTotalScore=0;for(n=0;n<3;n++){nStuTotalScore+=StuScore[m][n];switch(n){case0:{nMathTotalScore+=StuScore[m][0];break;}
6、case1:{nChineseTotalScore+=StuScore[m][1];break;}case2:{nEnglishTotalScore+=StuScore[m][2];}}}switch(m){case0:{printf("张同学:%d",nStuTotalScore);break;}case1:{printf("王同学:%d",nStuTotalScore);break;}case2:{printf("李同学:%d",nStuTotalScore);break;}case3:{printf("赵同学:%d",nStu
7、TotalScore);break;}case4:{printf("周同学:%d",nStuTotalScore);break;}}}printf("小组数学总分:%d小组数学平均分:%.2f",nMathTotalScore,(double)nMathTotalScore/5);printf("小组语文总分:%d小组语文平均分:%.2f",nChineseTotalScore,(double)nChineseTotalScore/5);printf("小组英语总分:%d小组英语平均分:%.2f",nEnglishTotalScor
8、e,(double)nEnglishTotalScore/5);}无参函数#includevoidfun