Jump to content

due by tomorrow, really need an answer

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
10 replies to this topic

#1
kreol

kreol

    Newbie

  • Members
  • Pip
  • 5 posts
hey.. I have a simple program which calculates permutations of any given 2 long unsigned values

however, it runs.. and my factorial function works.. however, my permutation is not giving the correct output.. could somone please help me with this?




int factorial (unsigned long);
int permutation (unsigned long, unsigned long);
#include <iostream>
using namespace std;

int main()
{
	unsigned long n,r = 0;
cout << "please enter n value " << endl;
	cin >> n;
	cout << "please enter r value " << endl;
	cin >> r;
	cout << "P(" << n << "," << r << ") is equal to.." <<
	cout << permutation(n, r);
	

	return 0;
}

int factorial(unsigned long number) 
{
	int temp;

	if(number <= 1) return 1;

	temp = number * factorial(number - 1);
	return temp;
}

int permutation( unsigned long k, unsigned long r)
{
	int result = 0;
	int temp = k - r;

	result = factorial(k)/factorial(temp);
	return result;
}

Edited by WingedPanther, 18 April 2008 - 07:58 AM.
add code blocks


#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
Without seeing what your input/output is, it's hard to say. I can say, however, that even with "moderate" numbers, you will almost certainly find that an unsigned long int will have wrapped several times as you calculate your factorials.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
Xav

Xav

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 13,118 posts
I thought there were predefined factorial methods available already?
Jordan said:

Good members, like yourself, stick around and post for ages to come!
Mr. Xav | Blog | Forums

#4
kreol

kreol

    Newbie

  • Members
  • Pip
  • 5 posts

WingedPanther said:

Without seeing what your input/output is, it's hard to say. I can say, however, that even with "moderate" numbers, you will almost certainly find that an unsigned long int will have wrapped several times as you calculate your factorials.

I tried scaling everything back down to ints and I still recieve letters as an answer.

The inputs are small, such as 4,2 or 3,1

#5
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
You're getting LETTERS as part of your output??? That doesn't make sense. Can you list the specific output you are getting?
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#6
kreol

kreol

    Newbie

  • Members
  • Pip
  • 5 posts

WingedPanther said:

You're getting LETTERS as part of your output??? That doesn't make sense. Can you list the specific output you are getting?

Input 1 = 4

Input 2 = 3



output = 6B254ACC1

#7
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
First, I would change your INTs to Unsigned Long.
Second, it appears that your output is in Hex.
Third, is there any reason you are using recursion?
Fourth, have you verified that your factorial function is producing correct results?
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#8
kreol

kreol

    Newbie

  • Members
  • Pip
  • 5 posts

WingedPanther said:

First, I would change your INTs to Unsigned Long.
Second, it appears that your output is in Hex.
Third, is there any reason you are using recursion?
Fourth, have you verified that your factorial function is producing correct results?

1. just did.

2. its in hex and not even the correct result.

3. I did it for difficulty for my proj for more points instead of doing it plainly

4. yeah, I tested the factorial function, it works.

crap, only 50 minutes left

#9
kreol

kreol

    Newbie

  • Members
  • Pip
  • 5 posts
I fixed it.. it was a spacing problem that generated the error.. thanks though for pointing me into the direction of tweaking my code.. as I hardly ever do that lol

#10
Xav

Xav

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 13,118 posts
Great - it's always good to work out a problem yourself. Think of it as a challenge.
Jordan said:

Good members, like yourself, stick around and post for ages to come!
Mr. Xav | Blog | Forums

#11
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
It's the bugs like that that'll drive you nuts. It's almost has fun as getting rid of compiler errors (I saw a student have this sequence of error counts once):
400
300
150
30
2
700
The last error she fixed allowed the compiler to hit the REST of the code and start complaining all over. I had to work hard to get her to NOT undo her last change.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog