我有一个编写maxSubArray算法的任务。它主要是工作,但我有一些返回结构的问题。
以下是相关文件(对于文字墙感到抱歉,但我不确定如何确定此错误):
main.cpp
#include <iostream>
#include "./MaxSubarray.h"
using namespace std;
#define TEST(test) { \
testNum++; \
if (!(test)) { \
cerr << "Test " << testNum << " failed" << endl; \
numFails++; \
} \
}
int runTests() {
int numFails = 0;
int testNum = 0;
{
// 0 1 2* 3 4 5
int A[] = { 1, -4, 14, -2, 3, -1 };
Result r = findMaxCrossingSubarray(A, 0, 2, 5);
Result c(2, 4, 15);
TEST(r == c);
}
{
// 0 1 2 3* 4 5 6
int A[] = { 0, 5, -4, 1, -2, -3, 6 };
Result r = findMaxCrossingSubarray(A, 0, 3, 6);
Result c(1, 6, 3);
TEST(r == c);
}
{
// 0 1 2 3* 4 5 6
int A[] = { 0, 5, -4, 1, -2, -3, 5 };
Result r = findMaxCrossingSubarray(A, 0, 3, 6);
Result c(1, 6, 2);
TEST(r == c);
}
{
int A[] = { 13, -3, 4 };
Result r = findMaxSubarray(A, 0, 2);
Result c(0, 2, 14);
TEST(r == c);
}
{
int A[] = { 13, 4, -3 };
Result r = findMaxSubarray(A, 0, 2);
Result c(0, 1, 17);
TEST(r == c);
}
{
int A[] = { -3, 4, 13 };
Result r = findMaxSubarray(A, 0, 2);
Result c(1, 2, 17);
TEST(r == c);
}
{
int A[] = { 4, -3, 13 };
Result r = findMaxSubarray(A, 0, 2);
Result c(0, 2, 14);
TEST(r == c);
}
{
int A[] = { 4, -3, -13, 5, 3 };
Result r = findMaxSubarray(A, 0, 4);
Result c(3, 4, 8);
TEST(r == c);
}
{
int A[] = { 4, 3, -13, -5, 3 };
Result r = findMaxSubarray(A, 0, 4);
Result c(0, 1, 7);
TEST(r == c);
}
{
int A[] = { -4, 4, -3, 5, -3 };
Result r = findMaxSubarray(A, 0, 4);
Result c(1, 3, 6);
TEST(r == c);
}
{
int A[] = { 13, -3, -25, 20, -3, -16, -23, 18, 20, -7, 12, -5, -22, 15, -4, 7 };
Result r = findMaxSubarray(A, 0, 15);
Result c(7, 10, 43);
TEST(r == c);
}
const int numSuccesses = testNum - numFails;
cout << numSuccesses << "/" << testNum << " tests succeeded" << endl;
return numFails;
}
int main() {
// TODO: Add test code as necessary.
// This file will NOT be submitted, though!
return runTests();
}
MaxSubarray.cpp
#include "./MaxSubarray.h"
#include <iostream>
// Provides floor, ceil, etc.
#include <cmath>
#include <climits>
using namespace std;
//Kalen Williams
//27 January 2017
Result findMaxCrossingSubarray(int* array, int low, int mid, int high){
int leftSum = INT_MIN;
int sum = 0;
int maxLeftIndex;
for(int i = mid; i >= low; i--){
sum = sum + array[i];
if(sum > leftSum){
leftSum = sum;
maxLeftIndex = i;
}
}
int rightSum = INT_MIN;
sum = 0;
int maxRightIndex;
for(int j = mid + 1; j <= high; j++){
sum = sum + array[j];
if(sum > rightSum){
rightSum = sum;
maxRightIndex = j;
}
}
int totalSum = leftSum + rightSum;
return Result(maxLeftIndex, maxRightIndex, totalSum);
}
Result findMaxSubarray(int* array, int low, int high){
if(high = low){
return Result(low, high, array[low]);
}
else{
int mid = (low + high) / 2;
//
Result leftArray = findMaxSubarray(array, low, mid);
Result rightArray = findMaxSubarray(array, mid + 1, high);
Result crossArray = findMaxCrossingSubarray(array, low, mid, high);
if(leftArray.sum >= rightArray.sum && leftArray.sum >= crossArray.sum){
return leftArray;
}
else if(rightArray.sum >= leftArray.sum && rightArray.sum >= crossArray.sum){
return rightArray;
}
else{
return crossArray;
}
//
}
}
代码按原样运行,我通过前3个测试,因为我的findMaxCrossingSubarray
有效,但是,当我取消注释findMaxSubArray
中的代码时出现错误
分段错误(核心转储)
我已经对这个问题进行过相当多的研究,并且知道这意味着我正在尝试引用尚未分配给该程序的内存,我只是不确定如何缩小问题范围。我尝试使用-Wall进行编译,但这给了我一堆各种错误,但这些错误似乎与此无关。
答案 0 :(得分:2)
我不能确定这是没有看到头文件的Seg错误的原因,但是你在findMaxSubarray
的第一行有一个错误:
if (high = low) {
你显然是high == low
。你应该有一些编译器警告。如果你是一个“const nazi”,编译器就会抓住这个...(即错误而不是警告):我的意思是,当然,将const int high
和const int low
放在函数定义中(声明中忽略const
,btw)。