您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 经营企划 > 计算机C语言精讲课件,双语
1Chap7arrays数组27.2one-dimensionalarrays1、declarationofone-dimensionalarraystypearray-name[size];Forexample:inta[10];charc[200];floatf[5];32、arrayitemarray-name[size];arrange:[0,size-1]inta[10];a[0]、a[1]、……a[9]array-name[subscript]a[10]overflowscanf(%d,&a[i]);printf(%d,a[i]);scanf(“%d”,&a);4initializationofone-demensionalarraytypearray-name[size]={listofvalues};inta[10]={1,2,3,4,5,6,7,8,9,10};a[0]=1,a[1]=2,...…a[9]=105intb[5]={1,2,3};b[0]=1,b[1]=2,b[2]=3,b[3]=?,b[4]=?inta[]={0,1,2,3,4,5,6,7,8,9};6for(i=0;in;i++)printf(%d,a[i]);7Input10numbers,savetheminarrays,thenprintthem8CalculateandprintthefirstmFibonaccinumbers1,1,2,3,5,8,13,……f[0]=f[1]=1f[n]=f[n-1]+f[n-2]2≤n≤19Fibonaccinumbers1,1,2,3,5,8,13,21,34,55#includestdio.hintmain(void){inti;intfib[20]={1,1};for(i=2;i20;i++)fib[i]=fib[i-1]+fib[i-2];for(i=0;i20;i++){printf(%6d,fib[i]);if((i+1)%5==0)printf(\n);}return0;}1123581321345589144233377610987159725844181676510Input5integers,savetheminarray,theninputonenumberx,tofindxinarray,ifyoucanfind,printit’ssubscript,otherwiseprint“NotFound”。Input:298969Output:1Input:298967output:NotFoundExample:#includestdio.hintmain(void){inti,flag,x;inta[5];printf(“Enter5integers:);for(i=0;i5;i++)scanf(%d,&a[i]);printf(“Enterx:);scanf(%d,&x);flag=0;for(i=0;i5;i++)if(a[i]==x){printf(Indexis%d\n,i);flag=1;break;}if(flag==0)printf(NotFound\n);return0;}Enter5integers:29819Enterx:9Indexis1Enter5integers:29819Enterx:7NotFound12OutputminimumInputmnumbers,thenprinttheminimum13#includestdio.hintmain(void){inti,min,n;inta[10];printf(“Entern:);scanf(%d,&n);printf(“Enter%dintegers:,n);for(i=0;in;i++)scanf(%d,&a[i]);min=a[0];for(i=1;in;i++)if(a[i]min)min=a[i];printf(minis%d\n,min);return0;}printminimumEntern:6Enter6integers:29-1816minis-114Inputmnumbers,thenprinttheminimumandsubscriptIndexissubscriptofminimuma[index]isminimumprintminimumandsubscript15#includestdio.hintmain(void){inti,index,n;inta[10];printf(“Entern:);scanf(%d,&n);printf(“Enter%dintegers:,n);for(i=0;in;i++)scanf(%d,&a[i]);index=0;for(i=1;in;i++)if(a[i]a[index])index=i;printf(minis%d\tsubis%d\n,a[index],index);return0;}programEntern:6Enter6integers:29-1816minis-1subis216Changtwonumbersxyx=yy=xZ=xx=yy=z17Inputn(n10),theninputnumbers,findminimum,thenexchangetheminimumandthefirstarrayitem.Indexissubscriptofminimuma[index]isminimumexchangetheminimumandthefirstarrayitema[index]==a[0]Changeminimumselectionsort35281Inoutn(n10),theninputnnumbers,printthemfromsmallTolargeForexamplen=535281(1)15283(2)2583(3)385(4)581935281(n=5)Findminimumfrom5numbers(a[0]~a[4]),exchangeitanda[0](1)15283a[4]==a[0]Findminimumfrom4numbers(a[1]~a[4]),exchangeitanda[1](2)12583a[2]==a[1]Findminimumfrom3numbers(a[2]~a[4]),exchangeitanda[2](3)12385a[4]==a[2]Findminimumfrom3numbers(a[2]~a[4]),exchangeitanda[3](4)12358a[4]==a[3]20(1)Findminimumfromnnumbers(a[0]~a[n]),exchangeitanda[0](2)Findminimumfromn-1numbers(a[1]~a[n]),exchangeitanda[1]……(n-1)Findminimumfrom2numbers(a[n-2]~a[n-1]),exchangeitanda[n-2]21输入数组ak=0k<n-1a[i]<a[index]index=i假(0)假(0)真(非0)真(非0)i=i+1交换a[index]和a[k]输出数组aindex=ki=k+1k=k+1i<n假(0)selectionsortfor(k=0;kn-1;k++){index=k;for(i=k+1;in;i++)if(a[i]a[index])index=i;temp=a[index];a[index]=a[k];a[k]=temp;}Entern:5Enter10integers:35281Aftersorted:1235823Two-dimensionalarrays24declarationoftwo-dimensionalarrays1、typearray-name[row-size][column-size];inta[3][2];a[0][0]a[0][1]a[1][0]a[1][1]a[2][0]a[2][1]intb[5][10];25initializationoftwo-dimensionalarrays1、inta[3][3]={{1,2,3},{4,5,6},{7,8,9}};intb[4][3]={{1,2,3},{},{4,5}};staticintb[][3]={{1,2,3},{},{4,5},{}}Arraya123456789Arrayb1230004500002inta[3][3]={1,2,3,4,5,6,7,8,9};intb[4][3]={1,2,3,0,0,0,4,5};26Whenthearrayiscompletelyinitializedwithallvalues,weneednotspecifythesizeofthefirstdimensioninta[][3]={1,2,3,4,5,6,7,8,9};staticintb[][3]={{1,2,3},{},{4,5},{}}Arraya123456789Arrayb12300045000027Nestingofloopfortwo-dimensionalarraysDeclareaarraywithtwocowsandthreecolumns,inputdatathendisplaythem28Declarematrixawiththreecowsandtwocolumns,thevalueoftheelementiscomputedasfollows:a[i][j]=i+j(0≤i≤2,0≤j≤1)Displaythismatrixinta[3][2];a[0][0]a[0][1]a[1][0]a[1][1]a[2][0]a[2][1]01122329#includestdio.hintmain(void){inti,j;inta[3][2];for(i=0;i3;i++)for(j=0;j2;j++)a[i][j]=i+j;for(i=0;i3;i++){for(j=0;j2;j++)printf(%4d,a[i][j]);printf(\n);}return0;}a[0][0]a[0][1]a[1][0]a[1][1]a[2][0]a[2][1]i=0j=0i=0j=1i=1j=0i=1j=1i=2j=0i=2j=1program01122330inta[N][N];Nisintegerdataa[i][j]:arrangeofthevalueofi、j:[0,N-1]maindiagonalofasquarematrixuppertriangularmatrixlowtriangularmatrix31Aboutmatrixofsizen*na[0][0]a[0][1]a[0][2]主对角线a[1][0]a[1][1]a[1][2]上三角a[2][0]a[2][1]a[2][2]下三角i==ji=ji=j32DisplayuppertriangularmatrixFor(i=0;i3;i++)For(j=i;j3;j++)DisplaylowtriangularmatrixFor(i=0;i3;i++)For(j=0;j=i;j++)33Inputintegerdatan(1n≤6),produceamatrixofsizen*nasfollows:a[i][j]=i*n+j+1(0≤i≤n-1,0≤j≤n-1)Thenexchangetherowandthecolumn,displaythematrix123456789147258369a[i][j]a[j][i]#includestdio.hintmain(void){inti,j,n,a[6][6],b[6][6];printf(E
本文标题:计算机C语言精讲课件,双语
链接地址:https://www.777doc.com/doc-3418370 .html