首先這是一個班級任務,所以我會很感激幫助,但只是提示,因爲我想學習。我必須計算我的代碼中看到的最終數字,但是我的計算結果有些問題。我的輸出是nan,我認爲這不是一個數字。我一直在試圖弄清楚我哪裏會出錯是無濟於事的,對於如何解決這個問題有什麼建議?提前致謝。當我運行我的代碼時出現NaN錯誤
#include <iostream>
#include <cstdlib>
#include<stdio.h>
#include<cmath>
using namespace std;
int main(){
int say;
float iten_paket[100],daxil_olan_paket[100];
float P_B[100]; // Itmis paketlerin ehtimali = P(B)
float P_S[100]; // Her bir serverin secilme ehtimali = P(Si)
float sum=0,final[100],A[100];
cout<<"Please enter server number => ";
cin>>say;
for(int k = 1; k<=say; k++){
cout<<"Please enter the taken packet number for : "<<k<<" server ";
cin>>daxil_olan_paket[k];
cout<<"Please enter the lost packet number for : "<<k<<" server ";
cin>>iten_paket[k];
}
// Her serverin secilmey ehtimali tapilir, P(S 1,2,3.....n)
for(int i = 1; i<=say; i++){
P_S[i] = 1/say;
}
for(int c = 1; c<=say; c++){
P_B[c] = iten_paket[c]/daxil_olan_paket[c];
}
for(int d = 1; d<=say; d++){
A[d] = P_S[d]*P_B[d];
}
for(int f = 1; f<=say; f++){
sum=sum+A[f];
}
// Bayes dusturu P(Sn|B) = P(B|Sn)*P(Sn)/P(B)
for(int a = 1; a<=say; a++){
final[a] =(float) A[a]/sum;
cout<<"For server "<<a<<" probability is :"<<final[a]<<endl;
}
return 0;
}
你有沒有爲任何'daxil_olan_paket'元素輸入0?你除以此。 –
當你用你的調試器遍歷你的代碼時,一次一行,你的調試器說的是產生你的'NaN'的計算參數? –
請輸入服務器號碼=> 3 請輸入以下服務器的接收包號碼:1服務器20 請輸入丟失的包裹號碼:1服務器1 請輸入以下服務器的接收包號碼:2服務器30 請輸入丟失的數據包2服務器2 請輸入所拍攝的數據包數目爲:對於3號服務器50 請輸入丟失數據包數量:3服務器3 對於服務器1的概率是:楠 對於服務器2概率:楠 對於服務器3概率爲:nan -------------------------------- 過程在14.06秒後退出,返回值爲0 新聞任何k繼續。 。 。 – namdik