課件第7講講義數(shù)組_第1頁
課件第7講講義數(shù)組_第2頁
課件第7講講義數(shù)組_第3頁
課件第7講講義數(shù)組_第4頁
課件第7講講義數(shù)組_第5頁
已閱讀5頁,還剩66頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

1、計(jì)算機(jī)程序設(shè)計(jì)基礎(chǔ)第七講批量數(shù)據(jù)組織數(shù)組一維數(shù)組 a10;/* M 10 V8R; 8*/#defineNUM10arrayNUM;count=0;&count);scanf(%d,bcount;0C89(VC6,VS2005)C99(gcc4,dev-cpp)連續(xù)的數(shù)組名字是數(shù)組首元素的內(nèi)存地址0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000#include #define NUM 10/* J6LA8R; Emain

2、()aNUM;i;*/for(i=0; iNUM; i+)ai=i;pr prf( (t Xtt+/n);f(n);for(i=0; iNUM; i+)prf(a%dt%pt%dn,i, &ai, ai);pr prf(n);f(at%pt%dn, a, *a);acs.creturn 0;x5=2,3,5,1,4;0 x5=2,3;x=2,3,5,1,4;#include/*J6W$T M 2 5 */ main()x5=2,3,5,1,4;i=0;for(i=0;pri5;i+)f(%d,xi);return0;init1.c#includemain()i; x5;x5=2,3,5,1;f

3、or(i=0;pri5;i+)f(%d,xi);return0;init2.ca8,b;b8;a=for(i=0; i8;i+)c=a-b;ci=ai-bi;memset(,);array10;memset(array,0,sizeof(array);memory.hmemset#include #include/*J64Pmemset 5 */ main()i=0;x5;memset(x,1,sizeof(x);for(i=0; i5;i+)prf(%d,xi);return0;init3.cmemset對每個字節(jié)賦值0、-1或字符000000010000000100000001000000

4、0100000001000000010000000100000001000000010000000100000001000000010000000100000001#include #include/*J64Pmemset 5 */ main()i char=0;x5;memset(x,a,sizeof(x);for(i=0; i5;i+)prf(%d,xi);return0;init4.c整型變量解決方法:程序員在對下標(biāo)變量進(jìn)行操作前,仔細(xì)檢查下標(biāo)的合法性#include main()i; a3= 111, 222, 333;for (i =ai pr1; i = 3; i+)= 0;f(%

5、dn, ai);return(0);overflow.carray3; i;array3; i;for(i=0;i3;i+)scanf(%d%d%d,&array);scanf(%d,&arrayi);array3; i;array3; i;for(ipr=0;i3;i+)prf(%d,array);f(%d,arrayi);(1)score140level11; (2)score; (3)level; (4);#include #include #define NUM 5#define SCOPE 11/* 1H ? ! LB8 OP6&*/main()float scoreNUM; lev

6、elSCOPE; i;memset(level, 0, sizeof(level);for(i = 0; i NUM; i+)scanf(%f, &scorei); k = scorei/10;levelk+;pr prf( ! 0F:n);f(n);for(i = 0; i SCOPE; i+)prf(%3d - %3d: %3dn,i*10,(i+1)*10-1, leveli);prf(n);score1.creturn 0;數(shù)組元素?cái)?shù)組名#include #define NUM 5/* J68R; 8 */void swap(tmp;x,y)tmp x =y = x;y;x;main(

7、)scoreNUM i=0;= 10,20,30,40,50;for(i=0; i (NUM/2); i+)swap(scorei, scoreNUM-i-1);return 0;swap_error.c#include #define NUM 5/* J68R; 8 */void swap(tmp;x,y)tmp x =y = x;y;x;main()scoreNUM i=0;= 10,20,30,40,50;for(i=0; i (NUM/2); i+)swap(scorei, scoreNUM-i-1);return 0;swap_error.cfloataverage(floatsco

8、re);#include #define NUM 10/* J6 8 8 */ float average(float array,length);main()float scoreNUM = 78,82,85,60,90,100,98,94,99,100;prf(Average score = %.2f, average(score,NUM);return 0;average.cfloat average(float array,i;length)float floatavg=0; sum=0;for(i= 0; i length; i+)sum = sum + arrayi;avg =su

9、m/length;return avg;#include #include #define NUM 9void swap(tmp;array,m,n)swap_array.c/* J68 8 , 3G8R;# */void swap(array,m,n);void pr_array(array);tmp = arraym;arraym arrayn=arrayn; tmp;main()scoreNUM i = 0;=10,20,30,40,50,60,70,80,90;voidpr_array(array)pr_array(score);/7 C# 2 8i; for(i=0;priNUM;i

10、+)for(i=0; i (NUM/2);swap(score, i,i+)f(%d , arrayi);NUM-i-1);/# 8R;prf(n);pr_array(score);/7 # 8return 0;#define NUM 9999/* 9I U 8 */ search_sequence(= -1;i;array,x)for(i=0; irightmid=(left+right)/2 keyif(key=)midelseif(key) leftmid left = mid + 1 rightelsemidright left right = mid - 1/* U 8 */ bin

11、ary_search(array,left,right,x)#include #include #define NUM 1000000prf(%2d left:%8d= -1;mid = 0;right:%8dn,+count,left,right);/* :=J6 */ count = 0; binary_search(array,left,right,x);if(leftright)= -1;elsemid = (left+right)/2; if(arraymid = x )= mid;else if(arraymid x)= binary_search(array,else= bina

12、ry_search(array,main()scoreNUM; x;i;for(i=0;iKU 8Tscore%dn,);elseprf( UR; T);mid+1, right, x);return 0;return;binary_search.clog2N01234567890123456789456789032689573042void bubbleSort(array,assert(length = 1);i = 0;exchange = 0;length)for(i=1; iarrayi)swap(array, i-1, i); exchange=1;if(exchange)bubb

13、leSort(array, length-1);#include #include #define NUM 10/* *.-I 8 */ void bubbleSort(/* 8R;# 8array,array_size);*/void swap(array,/* 8 N 8 */m,n);void pr_array(array);/*.-IJ6 */ main()void swap(array,m,n)aNUM=5,7,3,0,4,2,1,9,6,8;assert(m = 0 & n =0); temp;temp = arraym; arraym = arrayn; arrayn = tem

14、p;/ N8pr_array(a);/*.-I bubbleSort(a,/S N8NUM);pr_array(a);void pr_array(array)return 0;i;for(i=0; i= end)return;elsebegin,end)#include #include #define NUM 10/* %T DYpivot pivot = begin;void swap(array,/* 8 N 8 */m,n);i =j =begin+1; end;void pr_array(array);while(i= j)/*%-IJ6 */main()while(arrayi i+;while(arrayj j-;if( i j)swap(array,= arraypivot & i arraypivot &j=begin)aNUM=5,7,3,0,4,2,1,9,6,8;/ N8i, j);pr_array(a);/%-I quicksort(a,/S N80,NUM-1);swap(array, pivot, j); pivot = j;pr_array(a);pr_array(array);/I)%-Ireturn 0;quicksort(array

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論