Re: [問題] C++以函數傳送陣列,而以指標方式接收
※ 引述《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
04/11 20:14, 1F
→
04/11 20:16, , 2F
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
討論串 (同標題文章)
完整討論串 (本文為第 2 之 8 篇):