/* */

PDA

View Full Version : Anybody good with programming?



Skywalker
03-24-2007, 10:03 AM
:sl:

I have this project to do in C, and I'm working on this one function that's driving me crazy.

If anybody here has programming knowledge in any language, I'm trying to make a function that prints all possible combinations of a set of numbers.

For example if you have, 3 5 3 7, it would print:

3
35
33
37
353
357
337
3537
5
53
57
537
3
37
7

Anybody got an idea how to do this?
Reply

Login/Register to hide ads. Scroll down for more posts
lolwatever
03-24-2007, 10:07 AM
i done C last year and i've 4gotten most of it, but basically the idea is to create an Array containing the 4 digits.

and then print Array[i] where i = rand(1-4)

thet rick is to check if previous numbers are the same as teh randomly generated combination, to do that you'd store each generated random number in a new array and compare the current combination with previous combinatons.

i'd love to translate that into actual code 4u.. but i've just4goten alot of it! maybe if i get a sec i'll look into it insh :D

tc salams!
Reply

Skywalker
03-24-2007, 10:52 PM
The thing is, random numbers are not a guaranteed way of getting those results, and even so, it could take an infinite number of tries to get it right.

I'm sure this can be done with either a recursive function or with several nested loops, but the complexity of this problem is just too much for my mind.

What I have is an array, int arr[]={3,5,3,7}, and I want to print all combinations somehow...
Reply

lolwatever
03-24-2007, 11:28 PM
salams

skywalker yeh ur right, i know its a lazy way but hey... theoretically ti works, plus when ur dealin with 2,3 or possibly 4 digits.. you could *possibly* get away with it.

but... i have done a few things b4 with numbers that may be of help?

Here's a combination program i wrote last year.. The issue is, this one tells you how many combinations you can make, but it won't spit out the combinations themselves..


Code:

#include <stdio.h>
int
n_choose_k(int n, int k);
int
recursion(int n);
int
recursionk(int k);
int
recursionnk(int n,int k);
int
recursionb(int b);
int
main(int argc, char **argv) {
int n, k;

scanf("%d %d", &n, &k);
printf("There are %d ways to choose %d items from %d distinct items", n_choose_k(n, k), k, n);
return 0;
}
 
int
n_choose_k(int n, int k) {
return (recursion(n) / (recursionnk(n,k) * recursionk(k)));
}
int
recursion(int n) {
if(n == 1) {
return n;
}
return (n * recursion(n-1));
}
int
recursionk(int k) {
int n=k;
return recursion(n);
}
int
recursionnk(int n, int k) {
int b=n-k;
return recursionb(b);
}
int
recursionb(int b) {
int n=b;
return recursion(n);
}
Also this one.. basically you insert a number and it will do an arithmetic and tell you which number generated the longest cycle... i dont think this is too helpful but... dunno.. lol

Code:

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int
main(int argc, char **argv) {
int i=0, n=0, o=0, cycle=0, maxcyc=0, maxcycn=0;
printf("Enter a number:");
scanf("%d", &n);
for(i=1; i<=n; i++) {
o = i;
cycle = 0;
while(o>1) {
if(o%2==0) {
o /= 2;
} else {
o = (o * 3) +1;
}
cycle++;
}
if(cycle>maxcyc) {
maxcyc = cycle;
maxcycn = i;
}
}
printf("Longest cycle was generated by %d which caused %d cycles to occur", maxcycn, maxcyc);
return 0;
}
Reply

Welcome, Guest!
Hey there! Looks like you're enjoying the discussion, but you're not signed up for an account.

When you create an account, you can participate in the discussions and share your thoughts. You also get notifications, here and via email, whenever new posts are made. And you can like posts and make new friends.
Sign Up
lolwatever
03-24-2007, 11:38 PM
I think i figured it!!! :D :D :D

perhaps get the numbers into an array, and then sort that array (i.e. from lowest to highest)... and then for example print it this way:

e.g. user puts in a c b

Sort it into: a b c

(i've made a function that does that..)

Code:
void
order(int A[], int usedup){
    int j, temp, didswap;
    while(didswap){
       didswap=0;
       for(j=0; j<(usedup-1); j++){
           if(A[j] >= A[j+1]){
               temp = A[j];
               A[j] = A[j+1];
               A[j+1] = temp;
               didswap=1;
           }
       }
    }
    return;
}

then decrement the position of 'c

a b c
a c b
c a b

Then the psoition fo b..

c b a
b c a

Then the position of a..
c a b (but notice that c a b is already done... so you'd make a checker against all previous entries to insure there's no repition)




I'm using letters just for clarity.. but obviously same goes for numbers. The function i made is designed for numbers... (simple bubblesort algorithm)

all the best :w:
Reply

Skywalker
03-26-2007, 11:35 AM
Great idea, I'm gonna see how I can implement it. Also, to avoid repetition, don't make the 'a' decrement (it's at the beginning anyway).

I hope I'll be able to use this function the way I need to. The funny thing is, this is probably one of the easier functions that I'll have to make in one huge program that I have to do.

Thanks for the sugg. bro!

:w:
Reply

lolwatever
03-26-2007, 12:02 PM
hehe np bro all the best :D

salamz
Reply

Hey there! Looks like you're enjoying the discussion, but you're not signed up for an account.

When you create an account, you can participate in the discussions and share your thoughts. You also get notifications, here and via email, whenever new posts are made. And you can like posts and make new friends.
Sign Up

Similar Threads

  1. Replies: 9
    Last Post: 03-03-2010, 12:40 PM
  2. Replies: 9
    Last Post: 09-15-2008, 07:13 PM
  3. Replies: 2
    Last Post: 01-24-2007, 06:51 PM
  4. Replies: 6
    Last Post: 02-17-2006, 03:32 PM
British Wholesales - Certified Wholesale Linen & Towels | Holiday in the Maldives

IslamicBoard

Experience a richer experience on our mobile app!