C ++返回数组指针不能按预期工作

时间:2014-09-29 12:34:11

标签: c++

我正在为学校作业编写代码,该作业应该向用户询问限制并调用一个函数,该函数将生成给定系列的值到数组并将其传递回调用方法以进行打印。到目前为止,这是我的代码:

部首:

/*
 * series.h
 */

#ifndef SERIES_H_
#define SERIES_H_

// Direct calls for testing - DELETE AFTER
double log(int x);
double exp(int x);
double fib(int x);
double fac(int x);

// Global
enum SERIES {LOG, EXP, FIB, FAC};
void getSeries(SERIES _s, double _limit, int& _length, double *series);
double mean(double* _values, int length);
void printSeries(double* _series, int _length);

#endif /* SERIES_H_ */

CPP:

/*
 * series.cpp
 */
#include "series.h"
#include <cmath>
#include <iostream>
using namespace std;

int factorial(int x) {
    return (x == 1 ? x : x * factorial(x - 1));
}
double log(int x) {
    return ((double)x - ((double)(pow(x,2)) / (double)(2))
            + ((double)(pow(x,3)) / (double)(3)));
}
double exp(int x) {
    return (((double) 1) + ((double) x)
            + ((pow((double) x, 2)) / ((double) factorial(2)))
            + ((pow((double) x, 3)) / ((double) factorial(3)))
            + ((pow((double) x, 4)) / ((double) factorial(4))));
}
double fib(int x) {
    return (x < 2 ? x : (fib(x - 1) + fib(x - 2)));
}
double fac(int x) {
    return (x < 2 ? 1 : x * factorial(x - 1));
}
void getSeries(SERIES _s, double _limit, int& _length, double *series) {
    _length = 0;

    switch (_s) {
    case LOG: {
        for (int i = 0; log(i) < _limit; i++) {
            series[i] = log(i);
            _length++;
        }
        break;
    }
    case EXP: {
        for (int i = 0; exp(i) < _limit; i++) {
            series[i] = exp(i);
            _length++;
        }
        break;
    }
    case FIB: {
        for (int i = 0; fib(i) < _limit; i++) {
            series[i] = fib(i);
            _length++;
        }
        break;
    }
    case FAC: {
        for (int i = 0; fac(i) < _limit; i++) {
            series[i] = fac(i);
            _length++;
        }
        break;
    }
    }
}
double mean(double* _values, int length) {
    double sum;
    for (int i = 0; i < length; i++) {
        sum += _values[i];
    }
    return (sum / length);
}
void printSeries(double* _series, int _length) {
    for (int i = 0; i < _length; i++) {
        cout << "[" << i << "]" << _series[i] << endl;
    }
}

主:

/*
 * main.cpp
 */

#include <iostream>
#include "series.h"
using namespace std;

int main() {
    double _limit;
    int _length;
    double* series = new double;

    cout << "Series Calculator Application" << endl;
    cout << "Please enter a limit:" << endl;
    cin >> _limit;
    cout << "Limit is " << _limit << endl;

    getSeries(LOG, _limit, _length, series);
    cout << "\nLOG Length is " << _length <<endl;

    cout << "------------TEST(LOG)" << endl;
    for(int i = 0; i < _length; i++){
        cout << "[" << i << "]" << log(i) << endl;
    }
    cout << "------------ACTUAL(LOG)" << endl;
    printSeries(series, _length);

    getSeries(EXP, _limit, _length, series);
        cout << "\nEXP Length is " << _length <<endl;

        cout << "------------TEST(EXP)" << endl;
        for(int i = 0; i < _length; i++){
            cout << "[" << i << "]" << exp(i) << endl;
        }
        cout << "------------ACTUAL(EXP)" << endl;
        printSeries(series, _length);
}

当我运行我的主要时,我从控制台得到这个:

Series Calculator Application
Please enter a limit:
300
Limit is 300

LOG Length is 11
------------TEST(LOG)
[0]0
[1]0.833333
[2]2.66667
[3]7.5
[4]17.3333
[5]34.1667
[6]60
[7]96.8333
[8]146.667
[9]211.5
[10]293.333
------------ACTUAL(LOG)
[0]0
[1]0.833333
[2]2.66667
[3]7.5
[4]2.24151e-048
[5]1.96774e-259
[6]1.02756e-259
[7]96.8333
[8]146.667
[9]211.5
[10]293.333

EXP Length is 9
------------TEST(EXP)
[0]1
[1]2.70833
[2]7
[3]16.375
[4]34.3333
[5]65.375
[6]115
[7]189.708
[8]297
------------ACTUAL(EXP)
[0]1
[1]2.70833
[2]7
[3]16.375
[4]8.60084e-043
[5]1.56125e-259
[6]1.02899e-259
[7]189.708
[8]297

并且存在我的问题,正如您所看到的,元素4,5和6的实际实现与测试值不匹配。我不知道这是怎么发生的。请帮忙!

1 个答案:

答案 0 :(得分:2)

分配series时:

double* series = new double;

这会分配一个double。将它用作数组并访问索引0以外的任何元素将导致未定义的行为。

您可以考虑使用std::vector<double>并根据需要添加元素。