Re: [問題] C++以函數傳送陣列,而以指標方式接收

看板Programming作者 (LincismyBro)時間18年前 (2007/04/11 11:39), 編輯推噓0(004)
留言4則, 2人參與, 最新討論串2/8 (看更多)
※ 引述《quota (怎樣轉移檔案?)》之銘言: : 我想以函數傳送陣列,而以指標方式接收 : 不過結果卻讓人失望,附上我的原始碼, : 請各位幫我看看是哪裡出了問題,謝謝! : #include <iostream> : using namespace std; : void array_dump(int *a, int size ) : { : int *ptr; : for ( ptr = a; ptr < (a+size) ; ptr++ ) : cout << *ptr ; : cout << endl;} void array_dump(int* array, int count) { for(int i = 0; i < count; i++) cout << *(array + i); cout << endl; } : void bubble_sort(int *a, int size) : { : int i,temp; : int *ptr; : for( i = 0 ; i < size ; i++ ) : for( ptr = a; ptr < (a+size) ; ptr++ ) : { : if( *ptr < *(ptr+1) ) : { : temp=*(ptr+1); : *(ptr+1)=*ptr; : *ptr=temp; : } : array_dump(a,size); : }} void bubble_sort(int* array, int count) { int count_j = count - 1; int temp; for(int i = 0; i < count; i++) { for(int j = 0; j < count_j; j++) { if(*(array + j) < *(array + j + 1)) { temp = *(array + j + 1); *(array + j + 1) = *(array + j); *(array + j) = temp; } } count_j--; } array_dump(array, count); } : main () : { : int a[5] = {21,53,60,78,89}; : int count = (sizeof a)/(sizeof a[0]); : cout << " Bubble Sort " <<endl; : array_dump( a, count ); : cout << "-------------" <<endl; : bubble_sort( a, count ); : system("PAUSE"); : return 0; : } 沒編譯過喔.不知道對不對 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.118.218.31

04/11 20:14, , 1F
你的array_dump會一直印同一個元素哦
04/11 20:14, 1F

04/11 20:16, , 2F
sort的部份指標也一樣沒在動
04/11 20:16, 2F
※ 編輯: Scofield 來自: 211.74.6.208 (04/11 21:53)

04/11 21:53, , 3F
這樣咧??
04/11 21:53, 3F

04/12 16:23, , 4F
剛剛驗證過了 可以執行;
04/12 16:23, 4F
文章代碼(AID): #167CZvb2 (Programming)
討論串 (同標題文章)
文章代碼(AID): #167CZvb2 (Programming)