Thread: I need help with my code

  1. #1
    Registered User
    Join Date
    Dec 2020
    Posts
    9

    Question I need help with my code

    This is my first post on this website.
    I'm currently doing my assignment on reading a text file and create a Huffman code for each alphabet.
    I'm still on the step to create the tree by picking every 2 smallest weight, and I'm encountering 2 problems

    here is the code:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    typedef struct node {
      char letter;
      int weight;
      int PA, left, right;
    } BT;
    typedef struct {
      char letter;
      int weight;
      char *code;
    } Huff;
    
    void find2Lowest(BT * huffTable, int size, int *leftIndex, int *rightIndex)
    {
      while (huffTable[*leftIndex].PA != -1 || huffTable[*leftIndex].weight == 0) {
        *leftIndex++;
      }
      *rightIndex = *leftIndex;
      //printf("left index=%d\n",*leftIndex);
    
      for (int i = 0; i < size; ++i) {  //finding first smallest element
        if (huffTable.weight == 0 || huffTable.PA != -1)  // skip if node weight is zero or has been used
          continue;
        if (huffTable.weight < huffTable[*leftIndex].weight)
          *leftIndex = i;
      }
    
      //if first element is still same as second smallest element, increment right index
      while (*leftIndex == *rightIndex || huffTable[*rightIndex].PA != -1
             || huffTable[*rightIndex].weight == 0)
        *rightIndex++;
    
      for (int i = 0; i < size; i++) {  //finding second smallest element
        if (huffTable.weight == 0 || huffTable.PA != -1 || i == *leftIndex) // skip if node weight is zero or has been used
          continue;
        if (huffTable.weight < huffTable[*rightIndex].weight)
          *rightIndex = i;
      }
      printf("left=%d\tright=%d\n", *leftIndex, *rightIndex);
    
    }
    
    BT *generateHuffmanTreeTable(Huff * alphabet)
    {
      BT huffTable[51];
      for (int i = 0; i < 51; i++) {  //input the weight into the table
        if (i < 26) {
          huffTable.letter = alphabet.letter;
          huffTable.weight = alphabet.weight;
          huffTable.PA = -1;        //mark non used node
          huffTable.left = -1;
          huffTable.right = -1;
        } else {
          huffTable.letter = '*';   //mark non leaf node
          huffTable.weight = 0;
          huffTable.PA = -1;        //mark non used node
          huffTable.left = -1;
          huffTable.right = -1;
        }
      }
    
      int leftIndex = 0, rightIndex = 0;
      for (int i = 0; i < 25; i++) {
        leftIndex = 0, rightIndex = 0;
        find2Lowest(huffTable, (26 + i), &leftIndex, &rightIndex);
    
        //set parent node for 2 smallest element
        huffTable[leftIndex].PA = 26 + i;
        huffTable[rightIndex].PA = 26 + i;
    
        //set weight ,left and right for new node
        huffTable[26 + i].weight =
            huffTable[leftIndex].weight + huffTable[rightIndex].weight;
        huffTable[26 + i].left = leftIndex;
        huffTable[26 + i].right = rightIndex;
    
    
        printf("/////////////////////////////////////\n");
        for (int i = 0; i < 51; i++) {
          printf("%d\t%d\t%d\t%d\n", huffTable.weight, huffTable.PA, huffTable.left,
                 huffTable.right);
        }
        printf("/////////////////////////////////////\n");
    
      }
    
    }
    
    int main()
    {
      char fileName[10];
      //scanf("%s",fileName);// prompt user to input file name.
    
      Huff alphabet[26];
      for (int i = 0; i < 26; i++) {
        alphabet.weight = 0;
        alphabet.letter = 97 + i;
      }
    
      FILE *file;
      file = fopen("data3.txt", "r");
      char check;
      if (file) {                   //Read file and count the weight for each lowercase english alphabet
        while ((check = fgetc(file)) != EOF) {
          putchar(check);
          if (check >= 'a' && check <= 'z') {
            alphabet[check - 97].weight++;
          }
        }
        fclose(file);
      }
      printf("\n\n\n");
    
      generateHuffmanTreeTable(alphabet);
    
    }
    I've set some printf to see where is my mistake but here the problem is :
    In the 41st iteration of second for loop in generateHuffmanTree
    when I supposed to assign "0" and "36" as left and right child for the 41st node, and assign 41 as 0 and 36 parents. Only 0 is working but not 36. This messed the algorithm as it always read node 36 as the smallest element and always try to assign it to the n th node but always fail.

    The second problem that i encounter is that sometimes the program got segmentation fault on the 40th iteration of the second loop.

    I've been looking carefully for the past hours to at least fix my first problem but cannot to seem find the mistake, I'm assuming because it has something to do with the second problem as well ??
    Last edited by Salem; 12-13-2020 at 11:27 AM. Reason: Removed crayola colour scheme

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,617
    Can you edit your post, and this time use "copy as text" in your IDE and/or "Paste as text" in your browser.

    My attempt to clean up your post completely mucked it up.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Dec 2020
    Posts
    9
    I couldn't find a way to edit the post. Googling it says that cboard doesn't allow editing after certain duration? by the way I forgot to input the data3.txt
    At present, most of the dynamic sign language recognition is only for sign language words,
    the continuous sign language sentence recognition research and the corresponding results
    are less, because the segmentation of such sentence is very difficult. In this paper, a sign language
    sentence recognition algorithm is proposed based on weighted key frames. Key frames can be regarded
    as the basic unit of sign word, therefore, according to the key frames we can get related vocabularies,
    and thus we can further organize these vocabularies into meaningful sentence. Such work can avoid
    the hard point of dividing sign language sentence directly. With the help of Kinect, i.e. motion-control
    device, a kind of self- adaptive algorithm of key frame extraction based on the trajectory of sign language
    is brought out in the paper. After that, the key frame is given weight according to its semantic contribution.
    Finally, the recognition algorithm is designed based on these weighted key frames and thus get the continuous
    sign language sentence. Experiments show that the algorithm designed in this paper can realize real-time
    recognition of continuous sign language sentences.

  4. #4
    Registered User
    Join Date
    Sep 2020
    Posts
    223
    Your code won't compile for me.

    Code:
    gcc -o main main.c -Wall -pedantic -lm
    main.c: In function ‘find2Lowest’:
    main.c:19:5: warning: value computed is not used [-Wunused-value]
         *leftIndex++;
         ^~~~~~~~~~~~
    main.c:25:18: error: ‘huffTable’ is a pointer; did you mean to use ‘->’?
         if (huffTable.weight == 0 || huffTable.PA != -1)  // skip if node weight is zero or has been used
                      ^
                      ->
    main.c:25:43: error: ‘huffTable’ is a pointer; did you mean to use ‘->’?
         if (huffTable.weight == 0 || huffTable.PA != -1)  // skip if node weight is zero or has been used
                                               ^
                                               ->
    main.c:27:18: error: ‘huffTable’ is a pointer; did you mean to use ‘->’?
         if (huffTable.weight < huffTable[*leftIndex].weight)
                      ^
                      ->
    main.c:34:5: warning: value computed is not used [-Wunused-value]
         *rightIndex++;
         ^~~~~~~~~~~~~
    main.c:37:18: error: ‘huffTable’ is a pointer; did you mean to use ‘->’?
         if (huffTable.weight == 0 || huffTable.PA != -1 || i == *leftIndex) // skip if node weight is zero or has been used
                      ^
                      ->
    main.c:37:43: error: ‘huffTable’ is a pointer; did you mean to use ‘->’?
         if (huffTable.weight == 0 || huffTable.PA != -1 || i == *leftIndex) // skip if node weight is zero or has been used
                                               ^
                                               ->
    main.c:39:18: error: ‘huffTable’ is a pointer; did you mean to use ‘->’?
         if (huffTable.weight < huffTable[*rightIndex].weight)
                      ^
                      ->
    main.c: In function ‘generateHuffmanTreeTable’:
    main.c:51:16: error: ‘(BT *)&huffTable’ is a pointer; did you mean to use ‘->’?
           huffTable.letter = alphabet.letter;
                    ^
                    ->
    main.c:51:34: error: ‘alphabet’ is a pointer; did you mean to use ‘->’?
           huffTable.letter = alphabet.letter;
                                      ^
                                      ->
    main.c:52:16: error: ‘(BT *)&huffTable’ is a pointer; did you mean to use ‘->’?
           huffTable.weight = alphabet.weight;
                    ^
                    ->
    main.c:52:34: error: ‘alphabet’ is a pointer; did you mean to use ‘->’?
           huffTable.weight = alphabet.weight;
                                      ^
                                      ->
    main.c:53:16: error: ‘(BT *)&huffTable’ is a pointer; did you mean to use ‘->’?
           huffTable.PA = -1;        //mark non used node
                    ^
                    ->
    main.c:54:16: error: ‘(BT *)&huffTable’ is a pointer; did you mean to use ‘->’?
           huffTable.left = -1;
                    ^
                    ->
    main.c:55:16: error: ‘(BT *)&huffTable’ is a pointer; did you mean to use ‘->’?
           huffTable.right = -1;
                    ^
                    ->
    main.c:57:16: error: ‘(BT *)&huffTable’ is a pointer; did you mean to use ‘->’?
           huffTable.letter = '*';   //mark non leaf node
                    ^
                    ->
    main.c:58:16: error: ‘(BT *)&huffTable’ is a pointer; did you mean to use ‘->’?
           huffTable.weight = 0;
                    ^
                    ->
    main.c:59:16: error: ‘(BT *)&huffTable’ is a pointer; did you mean to use ‘->’?
           huffTable.PA = -1;        //mark non used node
                    ^
                    ->
    main.c:60:16: error: ‘(BT *)&huffTable’ is a pointer; did you mean to use ‘->’?
           huffTable.left = -1;
                    ^
                    ->
    main.c:61:16: error: ‘(BT *)&huffTable’ is a pointer; did you mean to use ‘->’?
           huffTable.right = -1;
                    ^
                    ->
    main.c:83:43: error: ‘(BT *)&huffTable’ is a pointer; did you mean to use ‘->’?
           printf("%d\t%d\t%d\t%d\n", huffTable.weight, huffTable.PA, huffTable.left,
                                               ^
                                               ->
    main.c:83:61: error: ‘(BT *)&huffTable’ is a pointer; did you mean to use ‘->’?
           printf("%d\t%d\t%d\t%d\n", huffTable.weight, huffTable.PA, huffTable.left,
                                                                 ^
                                                                 ->
    main.c:83:75: error: ‘(BT *)&huffTable’ is a pointer; did you mean to use ‘->’?
           printf("%d\t%d\t%d\t%d\n", huffTable.weight, huffTable.PA, huffTable.left,
                                                                               ^
                                                                               ->
    main.c:84:23: error: ‘(BT *)&huffTable’ is a pointer; did you mean to use ‘->’?
                  huffTable.right);
                           ^
                           ->
    main.c: In function ‘main’:
    main.c:99:13: error: ‘(Huff *)&alphabet’ is a pointer; did you mean to use ‘->’?
         alphabet.weight = 0;
                 ^
                 ->
    main.c:100:13: error: ‘(Huff *)&alphabet’ is a pointer; did you mean to use ‘->’?
         alphabet.letter = 97 + i;
                 ^
                 ->
    main.c:94:8: warning: unused variable ‘fileName’ [-Wunused-variable]
       char fileName[10];
            ^~~~~~~~
    main.c: In function ‘generateHuffmanTreeTable’:
    main.c:90:1: warning: control reaches end of non-void function [-Wreturn-type]
     }
     ^

  5. #5
    Registered User
    Join Date
    Sep 2020
    Posts
    223
    Line 19. You most likely want

    Code:
        (*leftIndex)++;
    Likewise on line 34:
    Code:
        (*rightIndex)++;

  6. #6
    Registered User
    Join Date
    Dec 2020
    Posts
    9
    looks like the guy who edit it messed it up. for almost every huffTable need index first than the propreties like huffTable[i].weight. I'll paste new one here
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    
    typedef struct node{
        char letter;
        int weight;
        int PA,left,right;
    }BT;
    typedef struct{
        char letter;
        int weight;
        char *code;
    }Huff;
    
    
    void find2Lowest(BT *huffTable,int size, int *leftIndex, int *rightIndex){
        while(huffTable[*leftIndex].PA!=-1||huffTable[*leftIndex].weight==0){
            *leftIndex++;
        }
        *rightIndex=*leftIndex;
        //printf("left index=%d\n",*leftIndex);
    
    
        for(int i=0;i<size;++i){//finding first smallest element
            if(huffTable[i].weight==0||huffTable[i].PA!=-1)// skip if node weight is zero or has been used
                continue;
            if(huffTable[i].weight<huffTable[*leftIndex].weight)
                *leftIndex=i;
        }
    
    
        //if first element is still same as second smallest element, increment right index
        while(*leftIndex==*rightIndex||huffTable[*rightIndex].PA!=-1||huffTable[*rightIndex].weight==0)
            *rightIndex++;
    
    
        for(int i=0;i<size;i++){//finding second smallest element
            if(huffTable[i].weight==0||huffTable[i].PA!=-1||i==*leftIndex)// skip if node weight is zero or has been used
                continue;
            if(huffTable[i].weight<huffTable[*rightIndex].weight)
                *rightIndex=i;
        }
        printf("left=%d\tright=%d\n",*leftIndex,*rightIndex);
        
    }
    
    
    BT* generateHuffmanTreeTable(Huff *alphabet){
        BT huffTable[51];
        for(int i=0;i<51;i++){//input the weight into the table
            if(i<26){
                huffTable[i].letter=alphabet[i].letter;
                huffTable[i].weight=alphabet[i].weight;
                huffTable[i].PA=-1;//mark non used node
                huffTable[i].left=-1;
                huffTable[i].right=-1;
            }
            else{
                huffTable[i].letter='*';//mark non leaf node
                huffTable[i].weight=0;
                huffTable[i].PA=-1;//mark non used node
                huffTable[i].left=-1;
                huffTable[i].right=-1;
            }
        }
        
        int leftIndex=0,rightIndex=0;
        for(int i=0;i<25;i++){
            leftIndex=0,rightIndex=0;
            find2Lowest(huffTable,(26+i),&leftIndex,&rightIndex);
    
    
            //set parent node for 2 smallest element
            huffTable[leftIndex].PA=26+i;
            huffTable[rightIndex].PA=26+i;
    
    
            //set weight ,left and right for new node
            huffTable[26+i].weight=huffTable[leftIndex].weight+huffTable[rightIndex].weight;
            huffTable[26+i].left=leftIndex;
            huffTable[26+i].right=rightIndex;
    
    
            
            printf("/////////////////////////////////////\n");
            for(int i=0;i<51;i++){
                printf("%d\t%d\t%d\t%d\n",huffTable[i].weight,huffTable[i].PA,huffTable[i].left,huffTable[i].right);
            }
            printf("/////////////////////////////////////\n");
            
        }
        
    }
    
    
    
    
    
    
    int main(){
        char fileName[10];
        //scanf("%s",fileName);// prompt user to input file name.
    
    
        Huff alphabet[26];
        for(int i=0;i<26;i++){
            alphabet[i].weight=0;
            alphabet[i].letter=97+i;
        }
    
    
        FILE *file;
        file = fopen("data3.txt","r");
        char check;
        if(file){//Read file and count the weight for each lowercase english alphabet
            while((check=fgetc(file))!=EOF){
                putchar(check);
                if(check>='a' && check<='z'){
                    alphabet[check-97].weight++;
                }
            }
            fclose(file);
        }
        printf("\n\n\n");
    
    
        generateHuffmanTreeTable(alphabet);
    
    
    }

  7. #7
    Registered User
    Join Date
    Dec 2020
    Posts
    9
    Yes it is consistent now, it finished the table but it still got segmentation fault.

  8. #8
    Registered User
    Join Date
    Sep 2020
    Posts
    223
    In this loop:
    Code:
      while (huffTable[*leftIndex].PA != -1 || huffTable[*leftIndex].weight == 0) {
        (*leftIndex)++;
      }
    There is nothing to ensure that *leftIndex does not exceed the size of huffTable[].

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,617
    It still has some compilation issues.
    Code:
    $ gcc -Wall -Wextra -g foo.c
    foo.c: In function ‘find2Lowest’:
    foo.c:20:9: warning: value computed is not used [-Wunused-value]
             *leftIndex++;
             ^
    foo.c:36:9: warning: value computed is not used [-Wunused-value]
             *rightIndex++;
             ^
    foo.c: In function ‘main’:
    foo.c:103:10: warning: unused variable ‘fileName’ [-Wunused-variable]
         char fileName[10];
              ^
    foo.c: In function ‘generateHuffmanTreeTable’:
    foo.c:95:1: warning: control reaches end of non-void function [-Wreturn-type]
     }
     ^
    $ gdb -q ./a.out
    Reading symbols from ./a.out...done.
    (gdb) run 
    Starting program: ./a.out 
    At present, most of the dynamic sign language recognition is only for sign language words,
    the continuous sign language sentence recognition research and the corresponding results
    are less, because the segmentation of such sentence is very difficult. In this paper, a sign language
    sentence recognition algorithm is proposed based on weighted key frames. Key frames can be regarded
    as the basic unit of sign word, therefore, according to the key frames we can get related vocabularies,
    and thus we can further organize these vocabularies into meaningful sentence. Such work can avoid
    the hard point of dividing sign language sentence directly. With the help of Kinect, i.e. motion-control
    device, a kind of self- adaptive algorithm of key frame extraction based on the trajectory of sign language
    is brought out in the paper. After that, the key frame is given weight according to its semantic contribution.
    Finally, the recognition algorithm is designed based on these weighted key frames and thus get the continuous
    sign language sentence. Experiments show that the algorithm designed in this paper can realize real-time
    recognition of continuous sign language sentences.
    
    
    
    left=9	right=23
    << snipped >>
    Program received signal SIGSEGV, Segmentation fault.
    0x0000000000400873 in find2Lowest (huffTable=0x7fffffffd870, size=41, leftIndex=0x7fffffffd85c, rightIndex=0x7fffffffd884) at foo.c:35
    35	    while(*leftIndex==*rightIndex||huffTable[*rightIndex].PA!=-1||huffTable[*rightIndex].weight==0)
    (gdb) bt
    #0  0x0000000000400873 in find2Lowest (huffTable=0x7fffffffd870, size=41, leftIndex=0x7fffffffd85c, rightIndex=0x7fffffffd884) at foo.c:35
    #1  0x0000000000400bea in generateHuffmanTreeTable (alphabet=0x7fffffffdca0) at foo.c:72
    #2  0x0000000000400f31 in main () at foo.c:129
    (gdb) p *leftIndex
    $1 = 0
    (gdb) p *rightIndex
    $2 = 32610
    Your rightIndex has wandered way off the end of the table, until the OS decides it's had enough of that and just kills the program.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  10. #10
    Registered User
    Join Date
    Dec 2020
    Posts
    9
    Thank you guys for the helps. I think it's fixed now. I have a question for Salem. How do you get those error message in line 38-48 ?

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,617
    It's there in the command line I posted
    -Wall -Wextra

    There's a whole raft of -Wfoo flags for GCC/Clang to tell you all sorts of things about your code before you even get to run it.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 09-19-2012, 01:58 PM
  2. Replies: 1
    Last Post: 03-10-2010, 11:28 AM
  3. Replies: 14
    Last Post: 04-01-2008, 02:23 AM
  4. producing c/c++ code from flowcharts,pseudo code , algorithims
    By rohit83.ken in forum C++ Programming
    Replies: 3
    Last Post: 02-20-2008, 07:09 AM
  5. Having trouble translating psudeo-code to real-code.
    By Lithorien in forum C++ Programming
    Replies: 13
    Last Post: 10-05-2004, 07:51 PM

Tags for this Thread