this is the function

long comb(int n,int k) { if((n==k)||(k==0)||(n==0)) return 0; if(k>0 && k<n) return (comb((n-1),k)+comb((n-1),(k-1))); }

and this is main

int m,n; cin>>m; cin>>n; cout<<comb(m,n);

Posted 03 October 2012 - 09:49 AM

i need to calculate combinations of n taken by k but i keep getting wrong results and di don't know where is the mistake

this is the function

and this is main

this is the function

long comb(int n,int k) { if((n==k)||(k==0)||(n==0)) return 0; if(k>0 && k<n) return (comb((n-1),k)+comb((n-1),(k-1))); }

and this is main

int m,n; cin>>m; cin>>n; cout<<comb(m,n);

Posted 03 October 2012 - 09:55 AM

What did you input in main()? What output do you receive? What output do you expect?

Jim

Jim

Posted 03 October 2012 - 10:13 AM

That link doesn't tell me anything.

What did you enter for the value of m?

The value of n?

What value do you expect to see with these inputs?

Jim

What did you enter for the value of m?

The value of n?

What value do you expect to see with these inputs?

Jim

Posted 03 October 2012 - 10:38 AM

thanks,it works now

sorry for double post but why did i got only 0's when i used return 1 and the corect numbers when i used return 1?

thanks,it works now,but why did i got only 0's when i used return 0 and the corect numbers when i used return 1?

sorry for double post but why did i got only 0's when i used return 1 and the corect numbers when i used return 1?

thanks,it works now,but why did i got only 0's when i used return 0 and the corect numbers when i used return 1?

Posted 03 October 2012 - 10:38 AM

Also that link is using factorials:

Where are you computing the factorials?

Jim

Quote

C(9,4) = 9! / ( 4! (9 - 4)! ) = 126

Where are you computing the factorials?

Jim

Posted 03 October 2012 - 01:40 PM

I recommend simulating what the computer is doing on paper or on the whiteboard. It becomes much clearer when you do it yourself versus somebody just showing you the callstack and the values of the variables. Gaining an understanding and good feel for recursion now will save you headaches down the road when you have to tackle data structures (like trees), or algorithms (like quicksort).

If the recursive combinations in a bit too hairy for you, start of with the recursive implementation of factorial: factorial(n) = n * factorial(n-1) where factorial(0) == 1.

If the recursive combinations in a bit too hairy for you, start of with the recursive implementation of factorial: factorial(n) = n * factorial(n-1) where factorial(0) == 1.

Page 1 of 1

- Caffeine Lounge
- Corner Cubicle
- Student Campus
- Software Development
- Industry News
- Introduce Yourself
- Nightmare.In.Code

- C and C++
- VB.NET
- Java
- C#
- ASP.NET
- .NET Framework
- VB6
- PHP
- Ruby
- Python
- ColdFusion
- Databases
- Other Languages
- Game Development
- Mobile Development
- 52 Weeks Of Code

- Web Development
- HTML & CSS
- JavaScript
- Graphic Design
- Flash & ActionScript
- Blogging
- SEO & Advertising
- Web Servers & Hosting
- Site Check

- External Sorting with C
- C++ Windows Charting Library Part 6
- Bezier Curves Part 1 [Linear Algebra Series]
- STL Algorithms ~ Tutorial 1: Using sort()
- C++ Windows Charting Library Part 5
- C++ Windows Charting Library Part 4
- C++ Windows Charting Library Part 3
- C++ Windows Charting Library Part 2
- C++ Windows Charting Library Part 1
- Win32 Image Editing Program Part3.
**338 More C++ Tutorials...**

- C Snippets
- C++ Snippets
- Java Snippets
- Visual Basic Snippets
- C# Snippets
- VB.NET Snippets
- PHP Snippets
- Python Snippets
- Ruby Snippets
- ColdFusion Snippets
- SQL Snippets
- Assembly Snippets
- Functional Programming Snippets
- Perl Snippets
- HTML/CSS Snippets
- Javascript Snippets
- Flash/ActionScript Snippets
- Other Languages Snippets

Copyright 2001-2014 **MediaGroup1 LLC**, All Rights Reserved

A**MediaGroup1 LLC** Production - Version 6.0.2.1.36

Server: secure3

A

Server: secure3