Can any 1 help me in understanding the prog?

Toshiba Mini nb305-n440bn 10.1" netbook...
January 15, 2011 at 06:46:52
Specs: Windows XP
The given algorithm produces combination's of GATC of length 5.When I try to replace GATC with 12345678 it produces the output of first four numbers of unit length 5.Can any one help me in understanding the program?

#include <stdio.h>

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

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

void enumerate(cell * a, int nbsym)
{
unsigned i, j;
for (i = 0; i < 1000; i++){
a[i] = 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, 5);
}


See More: Can any 1 help me in understanding the prog?

Report •


#1
January 15, 2011 at 12:22:05
unsigned i, j;
Obvious error: j isn't initialized. This gives you a (1 + nbsym / npack) in 4,294,967,296 (if int is 32-bit) chance your while loop will run.

it produces the output of first four numbers of unit length 5
This program is packing four values into a single cell, over two bits. This works because 2 raised to the second power is 4. (Side note: this implementation wastes space if the platform's byte is ever greater than 8 bits.)

If you wanted to use "12345678," the program would need to be refactored to store its values over 3 bits. And then you end up with octal, only with misleading symbols.

How To Ask Questions The Smart Way


Report •
Related Solutions


Ask Question