主题:求教:为何经过冒泡排序后,数组首元素的值会改变?
#include<iostream>
#include<iomanip>
#include <time.h>
#include<cmath>
using namespace std;
void swap(int *const p1,int *const p2)
{
int t;
t = *p1;
*p1 = *p2;
*p2 = t;
}
void bubble(int *arr,const int n)
{
int i,j;
for(j = 0;j < n;j++)
for(i = 0;i < n - j;i++)
if(arr[i] > arr[i+1])
swap(&arr[i],&arr[i+1]);
}
void output(int *arr,const int n)
{
int i;
cout << "黑桃: ";
for(i = 0;i < n;i++)
{
if(arr[i] < 14)
cout << setw(3) << arr[i];
}
cout <<endl;
cout << "红桃: ";
for(i = 0;i < n;i++)
{
if(arr[i] < 27 && arr[i] > 13)
cout << setw(3) << arr[i] - 13;
}
cout <<endl;
cout << "梅花: ";
for(i = 0;i < n;i++)
{
if(arr[i] < 40 && arr[i] > 26)
cout << setw(3) << arr[i] - 26;
}
cout <<endl;
cout << "方片: ";
for(i = 0;i < n;i++)
{
if(arr[i] < 53 && arr[i] > 39)
cout << setw(3) << arr[i] - 39;
}
cout <<endl;
cout <<endl;
}
class CardGame
{
public:
virtual void deal()
{}
};
class Bridge : public CardGame
{
private:
int a[13];
int b[13];
int c[13];
int d[13];
public:
void deal()
{
int i,j,arr[52];
srand( time( NULL ));
for(i = 0;i < 52;i++)
{
L: arr[i] = rand() % 52 + 1;
j = i - 1;
while(j > -1)
{
if(arr[i] == arr[j])
goto L;
j--;
}
cout << arr[i] <<endl;
if((i+1)%13 == 0)
cout<<endl;
}
for(i = 0;i < 13;i++)
a[i] = arr[i];
for(i = 0;i < 13;i++)
b[i] = arr[i + 13];
for(i = 0;i < 13;i++)
c[i] = arr[i + 26];
for(i = 0;i < 13;i++)
d[i] = arr[i + 39];
cout << " d "<<endl;
for(i=0;i<13;i++)cout<<setw(3)<<d[i];//这里d[0]的值没变
cout<<endl;
bubble(a,13);
bubble(b,13);
bubble(c,13);//经过我测试,这里d[0]的值就已经变了,但数组d还未排序啊!
bubble(d,13);
cout << " d "<<endl;
for(i=0;i<13;i++)cout<<setw(3)<<d[i];//这里d[0]的值就跟着变了,为什么?
cout<<endl;
cout << "玩家一的牌为: " <<endl;
output(a,13);
cout << "玩家二的牌为: " <<endl;
output(b,13);
cout << "玩家三的牌为: " <<endl;
output(c,13);
cout << "玩家四的牌为: " <<endl;
output(d,13);
}
};
希望大虾能具体说明原因
#include<iomanip>
#include <time.h>
#include<cmath>
using namespace std;
void swap(int *const p1,int *const p2)
{
int t;
t = *p1;
*p1 = *p2;
*p2 = t;
}
void bubble(int *arr,const int n)
{
int i,j;
for(j = 0;j < n;j++)
for(i = 0;i < n - j;i++)
if(arr[i] > arr[i+1])
swap(&arr[i],&arr[i+1]);
}
void output(int *arr,const int n)
{
int i;
cout << "黑桃: ";
for(i = 0;i < n;i++)
{
if(arr[i] < 14)
cout << setw(3) << arr[i];
}
cout <<endl;
cout << "红桃: ";
for(i = 0;i < n;i++)
{
if(arr[i] < 27 && arr[i] > 13)
cout << setw(3) << arr[i] - 13;
}
cout <<endl;
cout << "梅花: ";
for(i = 0;i < n;i++)
{
if(arr[i] < 40 && arr[i] > 26)
cout << setw(3) << arr[i] - 26;
}
cout <<endl;
cout << "方片: ";
for(i = 0;i < n;i++)
{
if(arr[i] < 53 && arr[i] > 39)
cout << setw(3) << arr[i] - 39;
}
cout <<endl;
cout <<endl;
}
class CardGame
{
public:
virtual void deal()
{}
};
class Bridge : public CardGame
{
private:
int a[13];
int b[13];
int c[13];
int d[13];
public:
void deal()
{
int i,j,arr[52];
srand( time( NULL ));
for(i = 0;i < 52;i++)
{
L: arr[i] = rand() % 52 + 1;
j = i - 1;
while(j > -1)
{
if(arr[i] == arr[j])
goto L;
j--;
}
cout << arr[i] <<endl;
if((i+1)%13 == 0)
cout<<endl;
}
for(i = 0;i < 13;i++)
a[i] = arr[i];
for(i = 0;i < 13;i++)
b[i] = arr[i + 13];
for(i = 0;i < 13;i++)
c[i] = arr[i + 26];
for(i = 0;i < 13;i++)
d[i] = arr[i + 39];
cout << " d "<<endl;
for(i=0;i<13;i++)cout<<setw(3)<<d[i];//这里d[0]的值没变
cout<<endl;
bubble(a,13);
bubble(b,13);
bubble(c,13);//经过我测试,这里d[0]的值就已经变了,但数组d还未排序啊!
bubble(d,13);
cout << " d "<<endl;
for(i=0;i<13;i++)cout<<setw(3)<<d[i];//这里d[0]的值就跟着变了,为什么?
cout<<endl;
cout << "玩家一的牌为: " <<endl;
output(a,13);
cout << "玩家二的牌为: " <<endl;
output(b,13);
cout << "玩家三的牌为: " <<endl;
output(c,13);
cout << "玩家四的牌为: " <<endl;
output(d,13);
}
};
希望大虾能具体说明原因