2014-09-29 42 views
-1

我正在爲學校任務編寫代碼,該任務應該要求用戶提供限制並調用一個函數,該函數將給定數組的值生成並傳遞給數組回到調用方法來打印。這裏是我到目前爲止的代碼: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的實際實現與測試值不匹配。我不知道這是怎麼發生的。請幫忙!

+0

Uugh!這麼多不必要的括號。瞭解您的運營商優先表和數據類型促銷規則! – Bathsheba 2014-09-29 12:36:38

+4

'double * series = new double;'分配一個'double',但'series'被用作一個數組。 – 2014-09-29 12:38:59

+1

@Bath:parens很好。這是過多的[C型]演員陣容,這是一個問題。 – 2014-09-29 12:39:04

回答

2

當您分配series

double* series = new double; 

這個分配單double。將它用作數組並訪問除索引0以外的任何元素將導致未定義的行爲。

您可以考慮使用std::vector<double>並根據需要添加元素。