由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - Segmentation fault 11 C++
相关主题
What does C++ program return to the operating system?[合集] 和大家再讨论一道面试算法题(MS) (转载)
C++ Q 99-102 (转载)命令行模式下可以安装的完整的gcc软件包
VC++线程问题javascript function-ask for help
C++的并行架构库是如何实现内存回收?霸哥说的没错,为了几个语法糖争论没意思
这个go的吐槽很客观看了看Java的lambda,感觉还是没啥意思
python code completion (转载)Amazon S3 now supports server side encryption with customer-provided keys‏
抛砖引玉: Bill Gates的话有没有道理?各位大牛推荐一个可以auto complete的node ide吧
什么是design?谁能帮我看一下错误在哪里?
相关话题的讨论汇总
话题: int话题: mid话题: left话题: right话题: mergesort
进入Programming版参与讨论
1 (共1页)
x******a
发帖数: 6336
1
请问这个mergesort有什么问题?编译时没问题, 执行时提示Segmentation fault 11
多谢。
#include
using namespace std;
void merge(int a[], int const left, int const mid, int const right)
{
int* b=new int[right-left];
int h,i,j,k;
h=left;
i=0;
j=mid;
//merges the two array's into b[] till the first is finished
while((h {
if(a[h]<=a[j])
{
b[i]=a[h];
h++;
}
else
{
b[i]=a[j];
j++;
}
i++;
}
//completes the array filling in it the missing values
if(h==mid)
{
for(k=j;k {
b[i]=a[k];
i++;
}
}
else
{
for(k=h; k {
b[i]=a[k];
i++;
}
}
//prints into the original array
for(k=0; k {
a[k+left]=b[k];
}
delete[] b;

}
void MergeSort( int a[], int const left, int const right)
{
int mid;
if(left {
mid=(left+right)/2;
MergeSort(a,left, mid);
MergeSort(a,mid, right);
merge(a, left, mid, right);
}
}
int main()
{
int a[]={1,2,3,5,3};
MergeSort(a, 0, 5);
for(int i=0; i<5; i++)
cout<
return 0;
}
t****t
发帖数: 6806
2
C/C++ use 0-based array, so a[right] is illegal.

11

【在 x******a 的大作中提到】
: 请问这个mergesort有什么问题?编译时没问题, 执行时提示Segmentation fault 11
: 多谢。
: #include
: using namespace std;
: void merge(int a[], int const left, int const mid, int const right)
: {
: int* b=new int[right-left];
: int h,i,j,k;
: h=left;
: i=0;

x******a
发帖数: 6336
3
thrust: thank you for your reply.
I modified the code a little bit by getting ride of a[right] but I still get
the mistake. Could you please have a look if there is any other error?
Thank you.

【在 t****t 的大作中提到】
: C/C++ use 0-based array, so a[right] is illegal.
:
: 11

x******a
发帖数: 6336
4
I found if I change the following code
if(left I get the right sort.
void MergeSort( int a[], int const left, int const right)
{
int mid;
if(left {
mid=(left+right)/2;
MergeSort(a,left, mid);
MergeSort(a,mid, right);
merge(a, left, mid, right);
}
}

get

【在 x******a 的大作中提到】
: thrust: thank you for your reply.
: I modified the code a little bit by getting ride of a[right] but I still get
: the mistake. Could you please have a look if there is any other error?
: Thank you.

p*****e
发帖数: 53
5
如果用left 下就知道了

【在 x******a 的大作中提到】
: I found if I change the following code
: if(left: I get the right sort.
: void MergeSort( int a[], int const left, int const right)
: {
: int mid;
: if(left: {
: mid=(left+right)/2;
: MergeSort(a,left, mid);

x******a
发帖数: 6336
6
Thank you Pinkiee,
I have another question,
when I use the following code to get an array. if I let N=2, then each time
I got 7 and 49.
Is there anything wrong, what should I do?
{
cout<< "enter a number"< int N;
cin >>N;
int a[N];
for(int i=0; i a[i]=rand()%100;
cout< }

array跟一

【在 p*****e 的大作中提到】
: 如果用left: 下就知道了
a9
发帖数: 21638
7
int rand ( void );
Generate random number
Returns a pseudo-random integral number in the range 0 to RAND_MAX.
This number is generated by an algorithm that returns a sequence of
apparently non-related numbers each time it is called. This algorithm uses a
seed to generate the series, which should be initialized to some
distinctive value using srand.

time

【在 x******a 的大作中提到】
: Thank you Pinkiee,
: I have another question,
: when I use the following code to get an array. if I let N=2, then each time
: I got 7 and 49.
: Is there anything wrong, what should I do?
: {
: cout<< "enter a number"<: int N;
: cin >>N;
: int a[N];

p*****e
发帖数: 53
8
add those two lines in your program
#include // For time()
srand(time(0)); // Initialize random number generator.

time

【在 x******a 的大作中提到】
: Thank you Pinkiee,
: I have another question,
: when I use the following code to get an array. if I let N=2, then each time
: I got 7 and 49.
: Is there anything wrong, what should I do?
: {
: cout<< "enter a number"<: int N;
: cin >>N;
: int a[N];

x******a
发帖数: 6336
9
Thank you a9 and again, pinkiee. It works great after I add the two lines:)
1 (共1页)
进入Programming版参与讨论
相关主题
谁能帮我看一下错误在哪里?这个go的吐槽很客观
围棋18路棋盘的可能棋局数量算出来了python code completion (转载)
有人熟悉xml么?抛砖引玉: Bill Gates的话有没有道理?
如何completely change user name in windows 10 ?什么是design?
What does C++ program return to the operating system?[合集] 和大家再讨论一道面试算法题(MS) (转载)
C++ Q 99-102 (转载)命令行模式下可以安装的完整的gcc软件包
VC++线程问题javascript function-ask for help
C++的并行架构库是如何实现内存回收?霸哥说的没错,为了几个语法糖争论没意思
相关话题的讨论汇总
话题: int话题: mid话题: left话题: right话题: mergesort