Dieses Programm nimmt 2 Zahlen von Benutzereingaben, fragt sie, ob sie die Permutationen oder Kombinationen herausfinden möchten, und gibt dann das Ergebnis aus. Hier ist der Code.Permutationen und Kombinationen - Laufzeitfehler
#include "std_lib_facilities.h"
int permutation(int first, int second)
{
int top_fac;
int bottom_fac;
for (int i = first-1; i >= 1; --i)
top_fac *=i;
for (int i2 = (first-second)-1; i2>=1; --i2)
bottom_fac *= i2;
return (top_fac/bottom_fac);
}
int combination(int first, int second)
{
int bottom_fac;
for (int i = second-1; i>=1; --i)
bottom_fac *= i;
return permutation(first, second)/(bottom_fac);
}
int main()
{
cout << "Enter two numbers.\n";
int first = 0;
int second = 0;
cin >> first >> second;
cout << "Now choose permutation(p) or combination(c).\n";
string choice;
cin >> choice;
if (choice == "p")
cout << "Number of permutations: " << permutation(first,second) << endl;
else if (choice == "c")
cout << "Number of combinations: " << combination(first,second) << endl;
else
cout << "p or c stupid.\n";
keep_window_open("q");
}
Wenn ich versuche, das Programm auszuführen, und ich wähle p oder c, erhalte ich eine Meldung „permutations_combinations.exe hat aufgehört zu arbeiten“. Ich habe versucht, einen Fehler zu finden, aber nichts kommt auf. Irgendwelche Ideen?
Vielen Dank im Voraus.
Ich weiß nicht, warum das mit 'Hausaufgaben' getaggt wurde. – Alex