tags:

views:

159

answers:

5

This program takes 2 numbers from user input, asks them whether they'd like to find out the permutations or combinations, and then outputs the result. Here's the code.

#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");
}

When I try to run the program, and I choose p or c, I get a "permutations_combinations.exe has stopped working" message. I tried to catch an error, but nothing is coming up. Any ideas?

Thanks in advance.

+1  A: 

Make sure you initialize top_fac and bottom_fac.

John Kugelman
Gah of course! Thank you very much.
Alex
Well, actually have to initialize top_fac and bottom_fac to first and second, since it's calculating a factorial.
Alex
Ah! I was then going to follow up with "your loops are off by 1". ;-)
John Kugelman
+4  A: 

You're not initializing the local variables top_fac and bottom_fac inside of your functions. Unlike other languages, local variables are NOT initialized to anything in particular in C or C++. The values that they receive are whatever garbage happens to be on the stack when you call the function. You should explicitly initialize top_fac and bottom_fac to 1 at the beginning of the permutation() and combination() functions.

I'm guessing that bottom_fac is accidentally getting initialized to 0, and then you're dividing by 0 at the end of the function, which is causing the runtime failure you're seeing.

Adam Rosenfield
A: 

Uninitialized variables.

Shing Yip
A: 

I'm guessing your inputs are causing one of your for-loops to infinitely loop. Check those out carefully.

Sev
A: 

A suggestion - learn how to use your debugger. Even if you're developing on Linux you've got an IDE that will help you step through your code - http://monodevelop.com/ (among others). Setting a break point at the top of your functions would have shown you your variables are not being initialized.

xanadont
You don't even need to set a breakpoint -- any debugger worth its salt should be able to give you a stack trace from the exact point that something like a segfault/access violation occurred.
Adam Rosenfield
Good point, though dunno how MonoDevelop handles unhandled exceptions. Full debugger support has only just made it in. And there's a good chance this question is coming from a student who's potentially developing on Linux.
xanadont
Based on the "permutations_combinations.exe" comment in the question, I'd strongly suspect the OP is working on Windows. And mono is irrelevant here, since the question is about C++.
Adam Rosenfield
xanadont