随机数排序问题(补)

lchh0917 2008-06-12 02:19:14
高手帮忙!!!我这个程序通过产生随机数,然后进行相关排序,问题是我选择了'直接插入法排序'后,再选择其他排序时调用的是上一次排序完成后的数组,即不是原始无序随机数,也就是这里用的是地址传递,怎么修改为值传递,才保证每次进行排序时调用的数组是原始随机数。。。

#include<iostream.h>
#include <algorithm>
#include<stdlib.h>
#include <time.h>
#include <tchar.h>

#define ARRAY_SIZE 20
using namespace std;

void print()
{
cout<<"====== 要使用直接插入法排序请按i ======"<<endl;
cout<<"====== 要使用直接选择法排序请按s ======"<<endl;
cout<<"====== 要使用起泡法排序请按b ======"<<endl;
cout<<"====== 要重新生成随机数请按r ======"<<endl;
cout<<"********上述排序指令均可重复执行*********"<<endl;
cout<<"====== 退出本程序请按x ======"<<endl;
}

void InsertSort(int arrForSort[],int nLength) //直接插入排序
{
int i,j,temp,n;
for(i=1;i<nLength;i++)
{
temp=arrForSort[i];
for(j=i;j>0&&temp<arrForSort[j-1];j--)
arrForSort[j]=arrForSort[j-1];
arrForSort[j]=temp;
for(n=0;n<nLength;n++)
cout<<arrForSort[n]<<" ";
cout<<endl;
}
}


void SelectSort(int arrForSort[],int nLength) //直接选择排序
{
int min,temp,
i,j,n;
for(i=0;i<nLength-1;i++)
{
min=i;
for(j=i+1;j<nLength;j++)
if(arrForSort[j]<arrForSort[min]) min=i;
temp=arrForSort[i];
arrForSort[i]=arrForSort[min];
arrForSort[min]=temp;
for(n=0;n<nLength;n++)
cout<<arrForSort[n]<<" ";
cout<<endl;
}
}

void BubbleSort(int arrForSort[],int nLength) //起泡法排序
{
int i,j,temp,n;
i=nLength-1;
while(i>0)
{
for(j=0;j<i;j++)
{
if(arrForSort[j+1]<arrForSort[j])
{temp=arrForSort[j+1];
arrForSort[j+1]=arrForSort[j];
arrForSort[j]=temp;}
}
i--;
for(n=0;n<nLength;n++)
cout<<arrForSort[n]<<" ";
cout<<endl;
}
}

void put_array(int x[],int size) {
for(int i=0;i<size;i++)
cout<<x[i]<<" ";
cout<<endl;
}

int getrand(int min,int max) {
int m;
m=(max-min);
m=min+double(rand())/RAND_MAX*m ;
return m;
}

void Exit()
{
exit(0);
}

void main ()
{
int i;
int x[ARRAY_SIZE];
char ch;
srand( (unsigned)time( NULL ) );
for (i=0;i<ARRAY_SIZE;i++)
{
x[i]=getrand(1,100);
}
cout<<"====== 1~100内随机生成的20个数: ======"<<endl;
cout<<"x[20]:";
put_array(x,ARRAY_SIZE);
cout<<endl;
print();
cout<<endl;
while(cin>>ch)
{
switch (ch)
{
case 'i':
cout<<"====== 下面将进行直接插入排序过程演示: ======"<<endl;
InsertSort(x,ARRAY_SIZE);
cout<<"x[20]:";
put_array(x,ARRAY_SIZE);
cout<<endl;
break;
case 's':
cout<<"====== 下面将进行直接选择法排序过程演示: ====== "<<endl;
SelectSort(x,ARRAY_SIZE);
cout<<"x[20]:";
put_array(x,ARRAY_SIZE);
cout<<endl;
break;
case 'b':
cout<<"====== 下面将进行起泡法排序过程演示: ====== "<<endl;
BubbleSort(x,ARRAY_SIZE);
cout<<"x[20]:";
put_array(x,ARRAY_SIZE);
cout<<endl;
break;
case 'r':
cout<<"====== 重新生成随机数: ======"<<endl;
srand( (unsigned)time( NULL ) );
for (i=0;i<ARRAY_SIZE;i++)
{
x[i]=getrand(1,100);
}
cout<<"x[20]:";
put_array(x,ARRAY_SIZE);
cout<<endl;
break;
case 'x':
Exit();
break;
default:cout<<"====== 请按上述提示输入正确指令: ====== "<<endl;
}
}
}
...全文
259 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
lchh0917 2008-06-14
  • 打赏
  • 举报
回复
试过了,但好像还是有问题,可能是我调用错误,谁能把运行通过的代码贴上来啊,快崩溃了,前辈、高手帮忙啊!!!
还是老问题:我这个程序通过产生随机数,然后进行相关排序,问题是我选择了'直接插入法排序'后,再选择其他排序时调用的是上一次排序完成后的数组,即不是原始无序随机数,也就是这里用的是地址传递,怎么修改为值传递,才保证每次进行排序时调用的数组是原始随机数。。。
cppscript 2008-06-13
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 jqx_ah 的回复:]
数组 传递 就等同于 引用传递 传递的是地址!
没有所谓的 值传递 不像一些简单的参数
个人建议你 在随机数刚产生的一开始 另外再建立一个数组 作为副本!
[/Quote]
就是啊
做一份copy
就保留下原始的了
f22fbi 2008-06-13
  • 打赏
  • 举报
回复
传数组,实际是传数组的首地址,传值是传的地址的值
如2楼所说,再建一个数组作为副本
leelittlelong 2008-06-13
  • 打赏
  • 举报
回复
图片看不了哦弟兄
tangshuiling 2008-06-13
  • 打赏
  • 举报
回复
做副本就是这样:
int array[3]={1,2,3};
int temp[3]; //temp为副本
for(int i=0;i<3;i++) temp[i]=array[i];//全部COPY
lchh0917 2008-06-13
  • 打赏
  • 举报
回复
我很菜,能说说具体代码吗,谢谢了
lchh0917 2008-06-12
  • 打赏
  • 举报
回复



就是第三张截图那样存在问题,怎么修改代码啊
jqx_ah 2008-06-12
  • 打赏
  • 举报
回复
数组 传递 就等同于 引用传递 传递的是地址!
没有所谓的 值传递 不像一些简单的参数
个人建议你 在随机数刚产生的一开始 另外再建立一个数组 作为副本!

64,642

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧