|
I have been trying to debug it for two hours now, Can anyone see why I am getting these two errors. Thanks!
#include <stdio.h>
#include <stdlib.h>
#include "Huffman.h"
# define Unlinked -1
struct {
unsigned Occurences;
BinCode Code;
int Link;
} HuffTable[CharSetLength];
FILE *SampleText, *CodeFile;
void ClearTable(void){
int i;
for (i = 0; i <CharSetLength; ++i) {
HuffTable[i].Occurences = 1;
HuffTable[i].Link = Unlinked;
HuffTable[i].Code.NumberOfBits = 0;
HuffTable[i].Code.Representation = 0;
}
}
void FindLeftAndRight(int *Left, int *Right){
int i, MinIndex = 0, MinValue = Infinity, NextMinIndex, NextMinValue,
Occurences;
for(i=0;i<CharSetLength;++i){
Occurences = HuffTable[i].Occurences;
if (Occurences < MinValue){
NextMinIndex = MinIndex;
NextMinValue = MinValue;
MinIndex = i ;
MinValue = Occurences;
}
else if (Occurences < NextMinValue) {
NextMinIndex = i;
NextMinValue = Occurences;
}
}
if (MinIndex < NextMinIndex){
*Left = MinIndex;
*Right = NextMinIndex;
}
else {
*Right = MinIndex;
*Left = NextMinIndex;
}
}
void AddBitToCodesLinkedTo(int Index, int Bit, int *ChainEnd){
do {
if (HuffTable[Index].Code.NumberOfBits == BitMax) {
printf ("too long to be represented in bits %d bits \n", BitMax);
exit(0);
}
HuffTable[Index].Code.Representation |=
(Bit << HuffTable[Index].Code.NumberOfBits ++ );
*ChainEnd = Index;
Index = HuffTable[Index].Link;
}while (Index!= Unlinked);
}
void OutputCodes(void) {
int i;
for (i = 0;i <CharSetLength; ++i)
fprintf(CodeFile, CodeFormat, HuffTable[i].Code.NumberOfBits,
HuffTable[i].Code.Representation);
}
main() {
char ch;
int NodesRemaining,Left,Right,ChainEnd;
ClearTable();
printf("Sample text file name?\n");
RequestAndOpenFile(&SampleText,"r");
printf("Code file name?\n");
RequestAndOpenFile(&CodeFile,"w");
while ((ch = getc(SampleText))!=EOF)
if (ch < CharSetLength)
if (HuffTable[ch].Occurences++ == Infinity){
printf ("Too many occurences of %c", ch);
exit(0);
}
for (NodesRemaining = CharSetLength; NodesRemaining > 1; --NodesRemaining) {
FindLeftAndRight {&Left, &Right);
HuffTable[left].Occurences += HuffTable[right].Occurences;
HuffTable[right].Occurences = Infinity;
AddBitToCodesLinkedTo(Left, 0 , &ChainEnd);
HuffTable[ChainEnd].Link = Right;
AddBitToCodesLinkedTo(Right, 1, &ChainEnd);
}
OutputCodes();
}
The first error shows up in the line
for (NodesRemaining = CharSetLength; NodesRemaining > 1; --NodesRemaining) {
and the second error shows up after
OutputCodes()
Thanks in advance
|