2016-05-24 5 views
0

基本的に私は(Degree sortaコマンドを使用する必要があるため)しばらくしてからC++をやり直し始めましたが、単純なプログラムを書くという簡単なタスクがありました。 2つの整数入力(NとM)を使用して、2つの出力(S)を返します。 1つの部分では、M = 10のN = 0からN = 10までのSの値をすべて表示するためにループを使用するように求められています。double(C++)の単一値を返すプログラム

Iveは戻り値が " 5" 10.

までのすべてのNこれはコードであるために:

#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

私は間違った場所にこれを掲示場合、私は私がしなければ、改造は私に簡単に移動してください:)

+0

整数を整数で割った値は常に整数を返します。あなたのループ内のintのうちの少なくとも1つをダブルに変更してください – Alfie

+0

コードを正しくインデントしてください – coyotte508

答えて

2

このライン、申し訳ありません:

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

5を持っています( 5つ)の出力として - S(esss)

おそらくSはvariabのような素晴らしい名前ではありませんle(いずれもl

+0

私はそれが間違っていることを信じることができません。夜だから... 助けてくれてありがとう、大いに感謝しています。 –

+0

@MuaminHugsy助けを求める前に自分のコードを見直す練習をしてください。午前中にそれを批判的に見ると、あなた自身に何が間違っているのかが分かるかもしれません。構文強調表示エディタを使用すると、定数5と変数Sの差異がより目立つようになります。 –

関連する問題