gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 02:06:08 27 4
gpt4 key购买 nike

基本上我只是在一段时间后再次开始使用 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com