作者Scofield (LincismyBro)
站內Programming
標題Re: [問題] C++以函數傳送陣列,而以指標方式接收
時間Wed Apr 11 19:39:36 2007
※ 引述《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
→ windf4:你的array_dump會一直印同一個元素哦 125.231.74.249 04/11 20:14
→ windf4:sort的部份指標也一樣沒在動 125.231.74.249 04/11 20:16
※ 編輯: Scofield 來自: 211.74.6.208 (04/11 21:53)
→ Scofield:這樣咧?? 211.74.6.208 04/11 21:53
→ Scofield:剛剛驗證過了 可以執行; 140.118.218.52 04/12 16:23