作者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
1F:→ windf4:你的array_dump会一直印同一个元素哦 125.231.74.249 04/11 20:14
2F:→ windf4:sort的部份指标也一样没在动 125.231.74.249 04/11 20:16
※ 编辑: Scofield 来自: 211.74.6.208 (04/11 21:53)
3F:→ Scofield:这样咧?? 211.74.6.208 04/11 21:53
4F:→ Scofield:刚刚验证过了 可以执行; 140.118.218.52 04/12 16:23