Anybody good with programming?

  • Thread starter Thread starter Skywalker
  • Start date Start date
  • Replies Replies 6
  • Views Views 2K

Skywalker

Elite Member
Messages
443
Reaction score
92
Gender
Male
Religion
Islam
: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?
 
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 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!
 
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...
 
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:
[B][SIZE=2]
#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);
}
[/B][/SIZE]

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:
[B][SIZE=2]
#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;
}
[/B][/SIZE]
 
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:
 
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:
 

Similar Threads

Back
Top