Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program to ask user to enter number of codes. Assign a letter for each c

ID: 3683194 • Letter: W

Question

Write a program to ask user to enter number of codes. Assign a letter for each code and possibility. Create a Huffman code for each letter. Not working this code which given on chegg?

Explanation / Answer

#include #include #include #define len(x) ((int)log10(x)+1) /* This is basically the node of the huffman tree */ struct node{ int value; char letter; struct node *left,*right; }; typedef struct node Node; /* to create huffmann code for every letter */ int frequencies[27] = {79, 15, 28, 43, 91, 23, 20, 61, 71, 88, 1, 40, 24, 69, 76, 20, 1, 61, 64, 91, 28, 10, 24, 1, 20, 1, 77}; /*This is to find and return the small sub-tree */ int tofindSmaller (Node *newarray[], int differenthere){ int smaller; int i = 0; while (newarray[i]->value==-1) i++; smaller=i; if (i==differenthere){ i++; while (newarray[i]->value==-1) i++; smaller=i; } for (i=1;ivalue==-1) continue; if (i==differenthere) continue; if (newarray[i]->valuevalue) smaller = i; } return smaller; } /*builds the huffman tree and returns its address by reference*/ void tobuildHuffmanTree(Node **treehere){ Node *temp; Node *newarray[27]; int i, subTreeshere = 27; int smallOne,smallTwo; for (i=0;ivalue = englishLetterFrequencies[i]; newarray[i]->letter = i; newarray[i]->left = NULL; newarray[i]->right = NULL; } while (subTreeshere>1){ smallOne=tofindSmaller(newarray,-1); smallTwo=tofindSmaller(newarray,smallOne); temp = array[smallOne]; newarray[smallOne] = malloc(sizeof(Node)); newarray[smallOne]->value=temp->value+array[smallTwo]->value; newarray[smallOne]->letter=127; ewarray[smallOne]->left=newarray[smallTwo]; newarray[smallOne]->right=temp; ewarray[smallTwo]->value=-1; subTrees--; } *tree = array[smallOne]; return; } /* This function is used to build the table with the bits for each letter. 1 stands for binary 0 and 2 for binary 1 */ void tofillTable(int newcodeTable[], Node *treehere, int Code){ if (treehere->letterletter] = Code; else{ tofillTable(newcodeTable, treehere->left, Code*10+1); tifillTable(newcodeTable, treehere->right, Code*10+2); } return; } /*This function is used to to compress the input*/ void tocompressFile(FILE *inputhere, FILE *outputhere, int newcodeTable[]){ char bit, c, x = 0; int n,length,bitsLeft = 8; int originalBits = 0, compressedBits = 0; while ((c=fgetc(inputhere))!=10){ originalBits++; if (c==32){ length = len(newcodeTable[26]); n = newcodeTable[26]; } else{ length=len(newcodeTable[c-97]); n = newcodeTable[c-97]; } while (length>0){ compressedBits++; bit = n % 10 - 1; n /= 10; x = x | bit; bitsLeft--; length--; if (bitsLeft==0){ fputc(x,outputhere); x = 0; bitsLeft = 8; } x = x right; if (current->letter!=127){ if (current->letter==26) fputc(32, outputhere); else fputc(current->letter+97,outputhere); current = treehere; } } } } return; } /*This function is basically used to invert the codes in newcodeTable2 */ void toinvertCodes(int newcodeTable[],int newcodeTable2[]){ int i, n, copy; for (i=0;i0){ copy = copy * 10 + n %10; n /= 10; } newcodeTable2[i]=copy; } return; } int main(){ Node *treehere; int newcodeTable[27], newcodeTable2[27]; int compress; char filename[20]; FILE *inputhere, *outputhere; tobuildHuffmanTree(&treehere); tofillTable(newcodeTable, treehere, 0); toinvertCodes(newcodeTable,newcodeTable2); /*This is done to the get input details from user*/ printf("Type the name of the file to process:n"); scanf("%s",filename); printf("pleasetType 1 to compress and 2 to decompress:n"); scanf("%d",&compress); inputhere = fopen(filename, "r"); outputhere = fopen("output.txt","w"); if (compress==1) tocompressFile(inputhere,outputhere,newcodeTable2); else todecompressFile(inputhere,outputhere, treehere); return 0; } The output for every character will be: a = 0011 b = 011011 c = 11111 d = 00100 e = 101 f = 000010 g = 011001 h = 1110 i = 0101 j = 011010101 and so on
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote