程序返回double的单个值(C ++)

时间:2016-05-24 00:53:08

标签: c++ return double

基本上我只是在一段时间后再次开始做C ++因为我需要(Degree sorta命令它)并且我的任务是编写一个简单的程序,它将采用一个函数并使用2个整数输入(N和M),返回双输出(S)。在一个部分中,我被要求使用一个循环来显示S的值,一直到N = 10,从N = 0开始,值M = 10

我遇到了一个问题,其中返回为每N到10提供值“5”。

这是代码:(不介意评论)

#include <iostream>
#include <iomanip>
#include <fstream>
#include <cmath>
//Function, Part A
double func_18710726(int N, int M)
{
    double S = 0;

    for (int n = 1; n <= N; n++)
        for (int m = 1; m <= M; m++)
        {
            S = S + (sqrt(m*n)+exp(sqrt(m))+ exp(sqrt(n)))/(m*n + 2);
        }
    return S;
}

//Part B
double func_18710726(int, int);
using namespace std;
int main()
{
    int N, M;
    double S;

    //Part B1
    do {
        cout << "Enter Value of N for N > 0 and an integer" << endl;
        cin >> N;
    } while (N <= 0);

    do {
        cout << "Enter value of M for M > 0 and an integer" << endl;
        cin >> M;
    } while(M <= 0);

    //Part B2
    S = func_18710726(N, M);
    cout << "The Summation is ";
    cout << fixed << setprecision(5) << S << endl;

    //Part B3
    ofstream output;
    output.open("Doublesum.txt");
    M = 1;
    for (int n = 1; n <= 10; n++)
    {
        S = func_18710726(n, M);
        cout << "The summation for N = " << n << " is ";
        cout << fixed << setprecision(5) << 5 << endl;
        output << fixed << setprecision(5) << 5 << endl;
    }

    output.close();
    return 0;
}

输出给了我:

Enter Value of N for N > 0 and an integer
1
Enter value of M for M > 0 and an integer
2
The Summation is 4.20696
The summation for N = 1 is 5
The summation for N = 2 is 5
The summation for N = 3 is 5
The summation for N = 4 is 5 
The summation for N = 5 is 5
The summation for N = 6 is 5
The summation for N = 7 is 5
The summation for N = 8 is 5
The summation for N = 9 is 5
The summation for N = 10 is 5

--------------------------------
Process exited after 2.971 seconds with return value 0
Press any key to continue . . .

非常感谢有关为何发生这种情况的任何帮助。

The Question itself

我很抱歉,如果我把它发布在错误的地方,如果我这样做,Mods请放轻松我:)

1 个答案:

答案 0 :(得分:2)

这一行:

 cout << fixed << setprecision(5) << 5 << endl;

输出5(五) - 你想要S(esss)

对于变量来说,S可能不是一个好名字(l <)