Click here to Skip to main content
15,897,334 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Help.I m unable to get the required out put
Permutations with repetitions

C++
#include <stdio.h>

typedef unsigned char cell;
cell a[10];
int npack = sizeof(cell)*4;

void decode(cell * a, int nbsym)
{
    unsigned i;
    for (i=0; i < nbsym; i++)
    {
        printf("%c", "1234567890abcdefghijklmnopqrstuvwxyz"[a[i/npack]>>((i%npack)*2)&3]);
    }
    printf("\n");
}

void enumerate(cell * a, int nbsym)
{
    unsigned i, j;
    for (i = 0; i < 10; i++)
    {
        a = 0;
    }
    while (j <= (nbsym / npack))
    {
        j = 0;
        decode(a, nbsym);
        while (!++a[j])
        {
            j++;
        }
        if ((j == (nbsym / npack))
        && ((a[j] >> ((nbsym-1)%npack)*2)&4))
        {
            break;
        }
    }
}

int main()
{
    enumerate(a, 6);
}


When ever i execute the answer would be combination's of first four numbers up to length six it it just blinks and goes off.

------------------

I want the outputs for all the inputs given for a length 6,9.
Can any one help me please?????

Thank you
Posted
Updated 10-Jan-11 5:31am
v2
Comments
#realJSOP 10-Jan-11 11:32am    
Use <pre> tags for code snippets.

I spot two errors;

First:
for (i = 0; i < 10; i++)
{
    a = 0;
}
You repeat the same initialisation 10 times, this is probably not what you want.


Second:
The following loop;
while (j <= (nbsym / npack))

uses j without initialising it.

To fix both problems, try this:
void enumerate(cell * a, int nbsym)
{
    unsigned i, j;
    for (i = 0; i < 10; i++)
    {
        a[i] = 0;           // I added the [i]
    }
    j = 0;                  // and I added this initialisation
    while (j <= (nbsym / npack))
    {
        j = 0;
        decode(a, nbsym);
        while (!++a[j])
        {
            j++;
        }
        if ((j == (nbsym / npack))
        && ((a[j] >> ((nbsym-1)%npack)*2)&4))
        {
            break;
        }
    }
}
 
Share this answer
 
When you say it just blinks and goes off, do you mean when you try to run the app, the console window just appears and goes away?

If this is what you are referencing, keep reading, otherwise, ignore my rambling below.

If you are running from visual studio, I think there's an option to keep a debug console window open until you tell it to close, but I don't know offhand where that option is located. It's probably in a different place depending on which version of visual studio you are using.

Alternatively, you can force the window to remain open by adding some code at the end to request the user to press a key before allowing the program to continue to close.

My C is a bit rusty thanks to .NET, but I think it's something like:

printf("\nPlease press any key to quit.");<br />
getch();



Hope this helps!
 
Share this answer
 
v2
Thaddeus has isolated a couple of bugs for you - "luckily" your outer loop in main never looks like it executed, as had it done so the first time you dereferenced a you'd have probably crashed the program by writing to memory somewhere around the start of the virtual address space.

As well as the points he made (initialising the array and loop variables) are you sure your decode function is correct?

"1234567890abcdefghijklmnopqrstuvwxyz"[a[i/npack]>>((i%npack)*2)&3]


looks so complicated it has to be wrong - you're using an esoteric feature of C string literals, using modulo AND a bitwise gimmick to simulate modulo all in the same expression. If you unroll that expression for 6 passes around the loop it becomes:

const char glyphs[] = "1234567890abcdefghijklmnopqrstuvwxyz";

putc( glyphs[ a[0] ] );
putc( glyphs[ a[0] ] >> 2 );
putc( glyphs[ a[0] ] );
putc( glyphs[ a[0] ] >> 2 );
putc( glyphs[ a[1] ] );
putc( glyphs[ a[1] ] >> 2 );

so you're not actually reading 80% of the array passed in AND you'll be printing out multiple identical values.

Anyway the moral here is write code other people can understand easily, 'cause when you have to debug it or test it you're suddenly that other person. What sounds cool now ("Hey I can use array addressing on string literals!", "Did you know you can simulate modulo for one less than 2 raised to the power of n by ANDing with 2^n - 1??") becomes a real pain in the rectum to understand later.

Oh, and if the decode function is really meant to do what the code says it does, can you enlighten me as why? I'm buggered if I can see a reason for doing it but I'm always interested in learning new things.

Cheers,

Ash
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900