Binominalkoeffizient
-
hallo leute
hab letztens ma versucht diese formel hier: http://de.wikipedia.org/wiki/Binomialkoeffizient#Definitionin c++ zu programmieren. alledings gibt es da noch rpobleme iwie mit der convertierung der datentypen wäre nett wenn ihr mir da etwas helfen könntet
der gibt mit dauerdn sonen komisches erebnis aus:
#include <iostream> #include <stdlib.h> using namespace std; float fak (float x); float hoch (float x, float k); float bio (float n, float x, float k, float y); // START MAIN int main() { float x,y,n,k; float b=0; cout<<"Summe aus: \n"; cout<<"-> x= ";cin>>x; cout<<"-> y= ";cin>>y; cout<<"\n->n: ";cin>>n; cout<<"->k: ";cin>>k; // CALC THE BIONOMINAL: while(k<=20) { b+=bio(n,x,k,y); k++; } cout<<"\n\nBinominalkoeffizient lautet: "<<b; system("PAUSE"); return 0; } // END MAIN // ============================================================================ // START FKTN MAIN float fak (float x) { float r=1; for(int i=1;i<=x;i++) r*=i; return r; } float hoch (float x, float k) { float t=1; for(int h=0;h<k;h++) t*=x; return t; } float bio (float n, float x, float k, float y) { float temp; temp = fak(n) / (fak(k) * fak((n-k))); temp = temp * hoch(x,(n-k)) * hoch(y,k); return temp; }
-
Machs doch einfach mit
int, dann entfällt auch dein Problem. Wieso rechnest du überhaupt mitfloat? Beim den Funktionen für Binomialkoeffizient, Potenz und Fakultät geht es immer nur um Ganzzahlen. Passe also die Parameter- und Rückgabetypen (überhaupt die Variablen) entsprechend an.Ich würd dir ausserdem zu konsistenter Einrückung raten. Noch was:
#include <iostream> #include <stdlib.h> // veraltet -> stattdessen <cstdlib> // musst du aber sowieso nicht einbinden, <iostream> reicht
-
naja ok...
aber die werte x und y sind gleitkommazahlen:
also beispiel ist zB,
Summe aus:
-> x= 0.9
-> y= 0.1->n: 20
->k: 18Binominalkoeffizient lautet: 1.5571e-016
da sollte aber eher was mit 0.667 rauskommen
Im header bereich ist jetzt bei mir :
#include <iostream>
#include <iomanip>
#include <cstdlib>
-
Zephir schrieb:
-> x= 0.9
-> y= 0.1->n: 20
->k: 18Binominalkoeffizient lautet: 1.5571e-016
da sollte aber eher was mit 0.667 rauskommen
Mein Taschenrechner sagt mir 1,539 * 10^(-16). Stimmt also.
BTW: Du willst die Wahrscheinlichkeit bei einem Bernoulliexperiment ausrechnen. Der Binomialkoeffizient ist nur die zweite Zeile deiner bio-Funktion.
-
sooo hab jetz noch nen semenatischen fehler entdeckt...
Allerdings gibt er mir jetzt 0.676927 aus statt 0.667
wie kann cih das noch optimieren?

der neue code:
#include <iostream> #include <iomanip> #include <cstdlib> using namespace std; float fak (float x); float hoch (float x, int k); double bio (int n, float x, int k, float y); // START MAIN int main() { float x,y; int n,k; float b=0; cout<<"Summe aus: \n"; cout<<"-> x= ";cin>>x; cout<<"-> y= ";cin>>y; cout<<"\n->n: ";cin>>n; cout<<"->k: ";cin>>k; // CALC THE BIONOMINAL: while(k<=n) { b+=bio(n,x,k,y); k++; } cout<<"\n\nBinominalkoeffizient lautet: "<<b; system("PAUSE"); return 0; } // END MAIN // ============================================================================ // START FKTN MAIN float fak (float x) { float r=1; for(int i=1;i<=x;i++) r*=i; return r; } float hoch (float x, int k) { float t=1; for(int h=0;h<k;h++) t*=x; return t; } double bio (int n, float x, int k, float y) { float temp; temp = fak(n) / (fak(k) * fak((n-k))); temp = temp * hoch(x,k) * hoch(y,(n-k)); return temp; }
-
-
eine wesentlich effektivere Lösung zur Berechnung des Binominialkoeffizienten als
fak(n) / (fak(k) * fak((n-k)));ist folgendes:
int binominalkoeffizient( int n, int k ) { int erg = 1; for( int t = 1; n > k; --n, ++t ) (erg *= n) /= t; return erg; }dann kann man auch mit 'int' als Datentyp arbeiten, ohne befürchten zu müssen, mit einem Zwischenergebnis einen overflow zu erzeugen.
genau wie es auch hier beschrieben ist:
queer_homo schrieb:
http://www.c-plusplus.net/forum/viewtopic-var-t-is-209066.html
Gruß
Werner