GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 09-Dec-2005, 05:04
balusss balusss is offline
Junior Member
 
Join Date: Dec 2005
Posts: 64
balusss is on a distinguished road
Question

[c++] number combinations


hi everybody! i am trying to write a program that gives all possible combinations of the given digits. i am just not getting the idea how to start. pls help me. suppose, a number 1234 is given, then we should output all combinations such as . 4321,3214,2143,....etc.,.
  #2  
Old 09-Dec-2005, 06:03
Paramesh's Avatar
Paramesh Paramesh is offline
Regular Member
 
Join Date: Sep 2005
Location: The Milky Way
Posts: 927
Paramesh is a jewel in the roughParamesh is a jewel in the roughParamesh is a jewel in the rough

Re: [c++] number combinations


Hi balusss,
Welcome to GIDForums.

You can do this by using recursion. (I hope you know what is recursion )
Before we venture into the actual program deeply, can you write a simple recursion function for finding out the factorial of a given number?

Cheers,

Paramesh.
__________________

Don't walk in front of me, I may not follow.
Don't walk behind me, I may not lead.
Just walk beside me and be my friend.
  #3  
Old 09-Dec-2005, 06:10
balusss balusss is offline
Junior Member
 
Join Date: Dec 2005
Posts: 64
balusss is on a distinguished road
Smile

Re: [c++] number combinations


thank u parameshji,

i know recursion and did quite a lot of exercises on it. i also have read ur reply to 'netnut' on the permutations of a given word. the elaborate answer infact made me more affiliated to u. but, u see, i didn't somehow catch what i have to do with 'next' and 'place'. i have read the elaboration. pls probe me a little more so that i get to it.

thank u for your concern in replying.
  #4  
Old 09-Dec-2005, 06:13
balusss balusss is offline
Junior Member
 
Join Date: Dec 2005
Posts: 64
balusss is on a distinguished road

Re: [c++] number combinations


i am new to this site. having lil problems to browse here and there
  #5  
Old 09-Dec-2005, 07:01
Paramesh's Avatar
Paramesh Paramesh is offline
Regular Member
 
Join Date: Sep 2005
Location: The Milky Way
Posts: 927
Paramesh is a jewel in the roughParamesh is a jewel in the roughParamesh is a jewel in the rough

Re: [c++] number combinations


Hi balusss,
Call me Paramesh. Not parameshji.

Quote:
Originally Posted by balusss
i didn't somehow catch what i have to do with 'next' and 'place'. i have read the elaboration. pls probe me a little more so that i get to it.
(With some extract from Dave's reply

1. We start with an array of ints containg the values {1 2 3 ... N} (set up in the main() program before calling the recursive function).
So, let the array be: numbers[4] = {1, 2, 3, 4};

What is the place:
An argument to the recursive function that indicates the current position within the array where we are going to put the next element.

When we get into the recursive function with "place" equal to N, we know that all of the elements of the current permutation are there, ready to be printed.

So, the function looks like this:
CPP / C++ / C Code:
void print_combination( int numbers[], int place, int N)
{
  int next;
  
  if (place == N) {
    printnums(numbers, N);
    return;
  }

   /* make a loop that lets "next" go from "place" to N-1 that does the following
    * swap the "next" element with the "place" element
    * call this function recursively with "place + 1"
    * swap "next" and "place" back to their original positions */
}
We should call the function print_combination something like this in the main program:
CPP / C++ / C Code:
print_combination(arr, 0, N);
What are we passing for place? 0. Because we start with 0th place.

So, consider the case of 3, and using pencil and paper( remember --- no code yet ) check whether you get the correct results or not.
Once you get familiar with the logic, then go to the program.

Once you finish this part we can switch to your original program.

Regards,
Paramesh.
__________________

Don't walk in front of me, I may not follow.
Don't walk behind me, I may not lead.
Just walk beside me and be my friend.
  #6  
Old 09-Dec-2005, 07:28
netnut netnut is offline
Member
 
Join Date: Dec 2005
Location: India
Posts: 174
netnut will become famous soon enough

Re: [c++] number combinations


hi balusss, i think that both of us are in the same boat. The difference between u and me is that u r working with numbers and I with strings. So lets see who crosses the line first (line here means the correct code). !! All the best !!
  #7  
Old 10-Dec-2005, 00:35
netnut netnut is offline
Member
 
Join Date: Dec 2005
Location: India
Posts: 174
netnut will become famous soon enough

Re: [c++] number combinations


You're problems are over Balusss.
Yes...you read that right. My code is completed and working fine, and since I was working with strings your problem is also sorted, 'cuz you enter the digits as a string and all the possible combinations are printed.
  #8  
Old 11-Dec-2005, 22:35
balusss balusss is offline
Junior Member
 
Join Date: Dec 2005
Posts: 64
balusss is on a distinguished road

Re: [c++] number combinations


first of all,
i thank mr.paramesh for making the difference. i got it right in the saturday eve itself. and the program is working fine. i also made it a template and stored it in my libraries. everything is well. and i am happy mr.netnut too 'crossed the line' on the same day.
thanks paramesh and i owe you for that.
  #9  
Old 11-Dec-2005, 22:41
balusss balusss is offline
Junior Member
 
Join Date: Dec 2005
Posts: 64
balusss is on a distinguished road

Re: [c++] number combinations


and one more thing. how do get the 'repetitions'??????? that is if 123 is given, how can i get 122, 133, 111,222,333 etc.,?????? will any simple modification to the code i have written for 'combinations' work?
 
 

Recent GIDBlogObservations of Iraq by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Need Help with my Cards Program (C++) krisopotamus C++ Forum 2 06-Oct-2005 16:48
Knights Tour - Reloaded . kobi_hikri C Programming Language 12 03-Oct-2005 12:15
Roman to decimal to roman SpudNuts C++ Forum 2 16-Feb-2005 19:44
Anyone can write a program code for this??? chriskan76 C Programming Language 1 19-Oct-2004 20:25
Array..... chriskan76 C Programming Language 4 18-Oct-2004 14:19

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 05:23.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.