Member Avatar for HASHMI007

My code Not working Why ???? there is some processor fault Error

Recommended Answers

All 2 Replies

Member Avatar for HASHMI007

My code Not working Why ???? there is some processor fault Error

`//------------------------------------------------------
//  filename: huffman.cpp
//  this coding is about huffman algorithm.
//  huffman algorithm
//-------------------------------------------------------

#include<iostream.h>
#include<fstream.h>

struct tree
{   int value;
    char ch;
    tree *left;
    tree *right;
};

struct input
{   int frequency;
    char character;
    tree *address;
};

postorder(tree *root,int node,char *temp_bit)
{   node++;
    if(root!=0)
    {   temp_bit[node-1]='0';
        postorder(root->left,node,temp_bit);
        temp_bit[node-1]='\0';

        temp_bit[node-1]='1';
        postorder(root->right,node,temp_bit);
        temp_bit[node-1]='\0';

        if(root->ch!='\0')
            cout<<root->ch<<" : "<<temp_bit<<endl;
    }
    node--;
}

tree *inputTree(input num_input1,input num_input2)
{   tree *leftnode = new tree;
    tree *rightnode = new tree;
    tree *root=new tree;

    if(num_input1.address=='\0')
    {   leftnode->value = num_input1.frequency;
        leftnode->ch = num_input1.character;
        leftnode->left = '\0';
        leftnode->right = '\0';
    }
    else
    {   leftnode = num_input1.address;  }

    if(num_input2.address=='\0')
    {   rightnode->ch=num_input2.character;
        rightnode->value=num_input2.frequency;
        rightnode->left='\0';
        rightnode->right='\0';
    }
    else
    {   rightnode = num_input2.address; }

    root->value=num_input1.frequency+num_input2.frequency;
    root->ch='\0';
    root->left=leftnode;
    root->right=rightnode;

    return root;
}

void huffman(input *number_input,int counter)
{   for(int i=0;i<counter-1;i++)
        for(int j=i+1;j<counter;j++)
            if(number_input[i].frequency > number_input[j].frequency)
            {   input *temp = new input;

                temp->address = number_input[i].address;
                temp->character = number_input[i].character;
                temp->frequency = number_input[i].frequency;
                number_input[i].address = number_input[j].address;
                number_input[i].character = number_input[j].character;
                number_input[i].frequency = number_input[j].frequency;
                number_input[j].address = temp->address;
                number_input[j].character = temp->character;
                number_input[j].frequency = temp->frequency;

                delete temp;
            }
}

int main(void)

{ char ch;
    int a=0;
    int b=0;
    int c=0;
    int indx=0;
    int arr[13];
for(int i=0;i<13;i++)
{
arr[i]=0;
}
    int rzl[3];
    int number,node=0;
fstream file_op("huffman.txt",ios::in);
if(!file_op)
{
cout<<"file does not exist";
return 0;
}
while(!file_op.eof())
{
file_op.get(ch);
indx=ch;
arr[indx]=arr[indx]+1;
}
for(i=0 ;i<13 ;i++)
{
         if(arr[i]=='a')a++;
         else if(arr[i]=='b')b++;
         else if(arr[i]=='c')c++;
         else{}
}
int j=0 ;//j<13;j++){

         for(i=0 ;i<13;i++){
         if(j==0){rzl[j]=arr[i];j++;}
         else if (j!=0){
          for(int k=0 ;k<13;k++){
                if(arr[i]!=arr[k]){
                rzl[j]=arr[i];j++;
                }

          }
         }


}
    //Tree t;
    //cout<<"How many character will you input? ";
    //cin>>
    number=13;

    char *temp_bit = new char[number];
    input *num_input = new input[number];

    for(int p=0;p<number;p++)
    {   num_input[p].address='\0';
        num_input[p].frequency=0;
        num_input[p].character='\0';
    }

    for( i=0;i<j;i++)
    {
        num_input[rzl[i]].character;
        if(rzl[i]='a')num_input[a].frequency;
        else if(rzl[i]='b')num_input[b].frequency;
        else if(rzl[i]='c')num_input[c].frequency;
    }

    for( j=0;j<number-1;j++)
    {   huffman(num_input,number);

        num_input[j+1].address=inputTree(num_input[j],num_input[j+1]);
        num_input[j+1].frequency += num_input[j].frequency;
        num_input[j+1].character = '\0';
    }

    cout<<endl;
    postorder(num_input[number-1].address,node,temp_bit);
    cout<<endl;

    delete temp_bit;
    delete num_input;
    return 0;
}`

I suspect your error is a "Segmentation Fault", which typically results from trying to access an uninitialized (or already deallocated) pointer. With any luck your "some processor fault error" message actually includes a specific error code number, and maybe even the line of your program where the error occurred. That would be a big help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.