Need help in debugging this code

This is a discussion on Need help in debugging this code within the C Programming forums, part of the General Programming Boards category; The question goes like this: You are given a file in which each line contains a list of space separated ...

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    1

    Need help in debugging this code

    The question goes like this:

    You are given a file in which each line contains a list of space separated words.
    Write a program to find out:
    1. List of words that are present in all the lines (words common to all lines)
    2. List of unique words that are present in the file
    3. The length of the line and file can be of any arbitrary size and your program can *not* hardcode either one of them. To reiterate, your program cannot allocate an array of arbitrary size or malloc an arbitrarily huge size in the beginning. It should dynamically allocate memory as and when required.

    Following are the requirements for the output of your program:
     The output of your program should contain two lines.
    First line will have a space separated list of the words that are present in all the lines.
    The next line will have a space separated list of all words occurring in the file. If the same word appears more than once in two different lines, your program should print it only once.
     The order of the words printed in the output should be the same as the order in which the words occur in the input file.

    For example if the input-file by name "a.txt" contains:

    good cat rat mat bat sat fat great bad
    bad good great sat fat fun
    cat bat fat mat sat good great fun find

    The output expected is:

    common words: good sat fat great
    unique words: good cat rat mat bat sat fat great bad fun find

    Notice that the order in which the words are printed by your program is important. The output

    common words: good sat fat great

    is NOT the same as

    common words: sat fat good great

    because that is not the order in which these words appear in the file.

    Your program will be invoked as
    <your_program> a.txt
    That is the file name containing lines with space separated words will be passed as a command line argument to your program.

    Your program should strictly adhere to the format of output given in the problem statement for being considered for evaluation.
    With respect to the input file you can assume that:
    There are no blank lines
    There is at least one word in each line
    A word never repeats itself in the same line



    I have written the following code:
    Code:
    #include<stdio.h>
    #define ch_limit 500
    
    struct node {
    char word[ch_limit];
    int count;
    struct node *next;
    };
    
    typedef struct node *list;
    
    int count_lines(FILE *fp)
    {
    return;
    }
    
    list populate(char *word,list first)
    {
    list temp,new=first;
    int value=0;
    temp=(list)malloc(sizeof(list));
    strcpy(temp->word,word);
    temp->count=0;
    temp->next=NULL;
    
    
    if(first==NULL){
    first=temp;
    return first;
    }
    
    while(new->next!=NULL){
    if(strcmp(new->word,word)==NULL){
    value=new->count;
    value+=1;
    new->count=value;
    free(temp);
    return(first);
    }else{
    printf("I am inside");
    new=new->next;
    }
    }
    if(strcmp(new->word,word)==NULL){
    value=new->count;
    value+=1;
    new->count=value;
    return(first);
    }
    new->next=temp;
    return(first);
    }
    
    list read_word(FILE *input_file)
    {
    char word[ch_limit];
    int ch;
    int i,count=0;
    list first=NULL;
    
    while((ch=fgetc(input_file)) !=EOF){
    
    for(i=0;(ch!=' ') || (ch!='\n');i++){
    word[i]=ch;
    ch=fgetc(input_file);
    if (ch == EOF)
    break;
    }
    word[i]='\0';
    first=populate(word,first); 
    
    }
    return(first);
    }
    
    int display(list first,int line_nos)
    {
    list unique,common;
    common=unique=first;
    if(unique==NULL){
    printf("The list of unique words is empty\n");
    return;
    }
    printf("Unique List: ");
    while(unique){
    puts(unique->word);
    printf(" ");
    unique=unique->next;
    }
    printf("\n");
    
    if(common==NULL){
    printf("The list of common words is empty\n");
    return;
    }
    
    printf("Common List: ");
    while(common){
    if(common->count!=line_nos){
    puts(common->word);
    printf(" ");
    }
    common=common->next;
    }
    return; 
    }
    
    
    int main(int argc, char *argv[])
    {
    FILE *input_file;
    int line_nos;
    list first=NULL;
    
    if(argc!=2) {
    printf("Usage: %s filename\n",argv[0]);
    exit(0);
    }
    if((input_file=fopen(argv[1],"r"))==NULL){
    printf("Error in opening the file\n");
    exit(0);
    }
    
    line_nos=count_lines(input_file);
    
    first=read_word(input_file);
    
    display(first,4);
    
    close(input_file);
    }
    However, the output obtained for the above code is:

    # ./a.out txt.txt
    List: go so go mo bo
    go inter yak lo
    jai go kim yoka
    my go bat cat

    Common List: go so go mo bo
    go inter yak lo
    jai go kim yoka
    my go bat cat


    txt.txt file contents:

    go so go mo bo
    go inter yak lo
    jai go kim yoka
    my go bat cat


    Please help in knowing whats wrong?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    I really really really really hope that you have indentation in your file and you somehow managed to lose it when pasting it in here. In fact I don't know why I even kept on trying to read the code, but I did, and I can say that the test if(common->count!=line_nos) can't possibly be right for printing common words. The fact that all your words got through means you're probably not initializing the count correctly.

  3. #3
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,648
    Cross posted (and answered) at DevShed as well...naughty, naughty.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    You don't have nearly enough header files. I see malloc() and strcmp(), while only <stdio.h> is included.

    Code:
    for(i=0;(ch!=' ') || (ch!='\n');i++){
    That's always true . . . think about it.

    BTW, I think you want fclose() instead of close.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,411
    Quote Originally Posted by tabstop View Post
    I really really really really hope that you have indentation in your file and you somehow managed to lose it when pasting it in here.
    I'm going to have to agree. The code is unreadable in this state.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. backward debugging in Visual Studio??
    By George2 in forum Tech Board
    Replies: 12
    Last Post: 11-05-2006, 01:17 AM
  2. Updated sound engine code
    By VirtualAce in forum Game Programming
    Replies: 8
    Last Post: 11-18-2004, 11:38 AM
  3. Replies: 8
    Last Post: 04-28-2003, 07:29 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Debugging leads to buggy code and longer hours?
    By no-one in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 01-28-2002, 10:14 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21