#program crashes before giving answer
33 messages · Page 1 of 1 (latest)
// librarys to be included in the code
#include<iostream>
#include<cmath>
#include<string>
// using standard for using the Input / Output methods
using namespace std;
// Function to calculate the factorial of a number - also known as the Function Prototype
double fact(double n)
{
// Checks if this code is 0, returns code to 1
if (n == 0)
{
return 1;
}
// otherwise call function recursively, passing less than 1, n-1 and return
else
{
return n * fact(n - 1);
}
}
// The main function for code
int main()
{
cout << "void -- 1" << endl << endl;
// Defines variables named n and r - also known as varable declaration
double n, r; // n is total number of students in class and r is team size in the class
// variable permutations for storing result
double permutations;
// What you are inserting in our case number of students
cout << "Enter total number of students : ";
cin >> n;
// what you are inserting in our case the number of teams
cout << "Enter number of team size to be selected : ";
cin >> r;
// using the formula given, calculates the number of permutations
permutations = fact(n) / (fact(r) * fact(n - r));
// Displays the output of the code
cout << "Total number of permutations : " << permutations << endl;
//system("pause");
return 0;
}
// Function definitions
here is the code
where does it crash
before telling me total number of potential teams
ok but where
// Displays the output of the code
cout << "Total number of permutations : " << permutations << endl;
it never says this mesage it crashes right after entering the value for cpp // what you are inserting in our case the number of teams cout << "Enter number of team size to be selected : "; cin >> r;
i cant seem to find out why it is crashing either
Is it crashing or just exiting? The program doesn't just pause when it finishes running
have you run it through a debugger? it'll tell you where it's actually crashing
cause "before this print" doesn't say anything about where it's failing
C++ can stack overflow right?
so when im debugging it into visual studios ill get this message
it can do all the memory errors
oh look, you were right
You know what a stack overflow is?
no not really
you're calling fact(n - r) with an n of 3 and an r of 18, so you're doing fact(-15). How do you expect this to work with your recursive base case?
it's when you fill up the stack and go past the end, corrupting other memory. In this case, it's happening because your recursive function never stops recursing, so you fill up the stack with function calls as it nests deeper
okay thank you and how do i fix that?
.
wanting it to run the formula cpp permutations = fact(n) / (fact(r) * fact(n - r));
I can't help you if you don't answer my question
The issue isn't your code
well it is