c++ - 程序为 double 返回单个值 (C++)

标签 c++ return double

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

最佳答案

这一行:

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

5(五)作为它的输出 - 你想要 S(esss)

S 可能不是一个很好的变量名(l 也不是)

关于c++ - 程序为 double 返回单个值 (C++),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37402794/

相关文章:

C#:Random.NextDouble 并包括自定义间隔的边框

c# - 在 C# 中,十进制值在第一个小数点处停止

c++ - 如何限制 "using namespace ..."的范围? C++

c++ - 如何从内存分配中逐一释放数组成员

java - 从方法返回 double 值

redirect - Yii2 返回 $this->goBack() 不工作

java - C和Java之间的 double 差异

c++ - 为什么我不能将函数分配给函数指针?

c++ - vector 数组还是数组 vector ?

javascript - 让函数 "return"成为 super 函数?