#program crashes before giving answer

33 messages · Page 1 of 1 (latest)

burnt hound
#

I'm creating an input and output program that can tell me the number of permutations of potential team assignments. but when i click the number of people and the number of teams it crashes before giving me the answer to potential teams

#
// 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

neat holly
#

where does it crash

burnt hound
#

before telling me total number of potential teams

neat holly
#

ok but where

burnt hound
#
// 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

ember sundial
#

Is it crashing or just exiting? The program doesn't just pause when it finishes running

neat holly
#

cause "before this print" doesn't say anything about where it's failing

ember sundial
#

C++ can stack overflow right?

burnt hound
#

so when im debugging it into visual studios ill get this message

neat holly
#

it can do all the memory errors

burnt hound
neat holly
ember sundial
burnt hound
#

no not really

neat holly
# burnt hound

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?

neat holly
# burnt hound no not really

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

burnt hound
#

okay thank you and how do i fix that?

burnt hound
#

wanting it to run the formula cpp permutations = fact(n) / (fact(r) * fact(n - r));

neat holly
#

I can't help you if you don't answer my question

ember sundial
#

The issue isn't your code

neat holly
#

well it is

ember sundial
#

How can a team be larger than the class size?

#

Your input isn't logical

#

The code could be changed to account for that

#

But entering correct values should make it work

burnt hound
#

okay thank you for the help

#

so i was just entering incorrect values