“链接.. \ SOURCE \ MERGEEXE.EXE:”错误

时间:2017-04-03 13:59:29

标签: c++ mergesort turbo-c++ linker-errors

我找到了一个用于测量执行时间的合并排序算法的代码,但是当我运行它时,程序将不会显示,并且“链接.. \ SOURCE \ MERGEEXE.EXE:”位于“消息”面板中。哪部分代码错了?

#include <iostream.h>
#include <time.h>

void mergesort(int[], int, int);
void merge(int[], int, int, int);
void mergesort(int a[], int low, int high)
{
    int mid;
    if (low < high) {
        mid = (low + high) / 2;
        mergesort(a, low, mid);
        mergesort(a, mid + 1, high);
        merge(a, low, high, mid);
    }
}

void merge(int a[], int low, int high, int mid)
{
    int i, j, k, c[50];
    i = low;
    j = mid + 1;
    k = low;
    while ((i <= mid) && (j <= high)) {
        if (a[i] < a[j]) {
            c[k] = a[i];
            k++;
            i++;
        }
        else {
            c[k] = a[j];
            k++;
            j++;
        }
    }
    while (i <= mid) {
        c[k] = a[i];
        k++;
        i++;
    }
    while (j <= high) {
        c[k] = a[j];
        k++;
        j++;
    }
    for (i = low; i < k; i++) {
        a[i] = c[i];
    }
}

int main()
{
    int randNum[100];
    clock_t sTime;
    srand((unsigned)clock());

    cout << "Start generating random numbers..." << endl;
    for (int index = 0; index < 100; index++) {
        randNum[index] = (rand() % 1000) + 1;
    }

    sTime = clock();
    mergesort(randNum, 0, 99);
    // delay the program by 0.05 second
    clock_t cWait = clock();

    cout << "Total CPU time used: " << (double)(clock() - sTime) / CLOCKS_PER_SEC << " seconds" << endl;
}

2 个答案:

答案 0 :(得分:1)

您的 c 数组的大小为50,而您将100个随机数传递给该函数,因此会出现分段错误。增加 c 数组的大小,它应该可以正常工作。

答案 1 :(得分:0)

首先确保使用什么编译器来完成此代码的来源。尝试使用DEV C ++ 5.1并调整一些代码位置是编译的答案(尽管我不保证预期的输出。)

#include <iostream>
#include <stdlib.h>
#include <ctime>
using namespace std;

void mergesort(int[], int, int);
void merge(int[], int, int, int);
void mergesort(int a[], int low, int high)
{
    int mid;
    if (low < high) {
        mid = (low + high) / 2;
        mergesort(a, low, mid);
        mergesort(a, mid + 1, high);
        merge(a, low, high, mid);
    }
}

void merge(int a[], int low, int high, int mid)
{
    int i, j, k, c[50];
    i = low;
    j = mid + 1;
    k = low;
    while ((i <= mid) && (j <= high)) {
        if (a[i] < a[j]) {
            c[k] = a[i];
            k++;
            i++;
        }
        else {
            c[k] = a[j];
            k++;
            j++;
        }
    }
    while (i <= mid) {
        c[k] = a[i];
        k++;
        i++;
    }
    while (j <= high) {
        c[k] = a[j];
        k++;
        j++;
    }
    for (i = low; i < k; i++) {
        a[i] = c[i];
    }
}

int main()
{
    int randNum[100];
    clock_t sTime;
    //srand((unsigned)clock(NULL));
    srand (time(NULL));

    cout << "Start generating random numbers..." << endl;
    for (int index = 0; index < 100; index++) {
        randNum[index] = (rand() % 1000) + 1;
    }

    sTime = clock();
    mergesort(randNum, 0, 99);
    // delay the program by 0.05 second
    clock_t cWait = clock();

    cout << "Total CPU time used: " << (double)(clock() - sTime) / CLOCKS_PER_SEC << " seconds" << endl;
}