Code modification and errors

This is a discussion on Code modification and errors within the C Programming forums, part of the General Programming Boards category; I have the following code and It needs some modification sin it to compile. The cmpwrd.c and worder.c are the ...

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    2

    Code modification and errors

    I have the following code and It needs some modification sin it to compile.
    The cmpwrd.c and worder.c are the files and worder.c compiles and works fine when compeiled individually. but when i do this

    gcc cmpwrd.c worder.c

    I am getting an error that multiple mains. so I need to remove one from worder.c.
    I have to turn off the TEST_SCAFFOLD which is running forcibly.

    Can any one please post me these modification.

    Any help is appreciated.

    Code:
    cmpwords.c
    
    #include "worder.h"
    #include <stdlib.h>
    #include <math.h>
    
    struct gabble {
       char* wd;
       int count;
       int touched;
    };
    
    struct blurry {
       char* fn;
       struct gabble wds[40000];
       int firstFree;
    
       int totalCount;
       } a,b;
    
    void rdfile(char* fn, struct blurry* b){
       char* wp;
       int i;
    
       b->fn = fn;
       b->firstFree = 0;
    
       if (init_file(fn)){fprintf(stderr,"\n?giving up\n"); exit(1);}
       while (nextC != EOF){
          wp = nextword();
          b->totalCount++;
          for (i=0; i<b->firstFree; i++){
    	 if (strcmp(wp,b->wds[i].wd)==0){
    	    b->wds[i].count++;
    	    break;
    	 }
          }
          if (i == b->firstFree){
    	 b->firstFree++;
    	 b->wds[i].wd = wp;
    	 b->wds[i].count = 1;
    	 b->wds[i].touched = 0;
          }
       }
    
    }
    
    void cmpdist(struct blurry* a, struct blurry* b){
       int i,j;
       double sum = 0;
    
       for (i =0; i<a->firstFree; i++){
          double x = a->wds[i].count/(double)a->totalCount;
          for (j=0; j<b->firstFree; j++){
             if (strcmp(a->wds[i].wd,b->wds[j].wd)==0){
    	   double y = b->wds[j].count/(double)b->totalCount;
    	   sum += (x-y)*(x-y);
               a->wds[i].touched = b->wds[i].touched = 1;
             }
          }
          if (a->wds[i].touched ==0) {sum += x*x; }
    
       }
       for (j=0; j<b->firstFree; j++){
          if (b->wds[j].touched ==0) {
    	 double y = b->wds[j].count/(double)b->totalCount;
             sum += y*y; 
          }
       }
       printf("distance = &#37;g\n",  sqrt(sum));
          
    }
    int main(int argc, char** argv){
       if (argc != 3){
          fprintf(stderr, "Usage: cmpwrds file1 file2\nCompare distances between word distributions in files.\n");
          return (1);
       }
       rdfile(argv[1], &a);
       rdfile(argv[2], &b);
       cmpdist(&a,&b);
    }
    
    Worder.c
    
    /*
     * A function nextword() is defined reading an English word.  Used here
     * for reading an English text from standard input and writing
     * the individual words on separate lines on standard output.
     * The main program acts as a test scaffold for the functions, which could
     * be used alone (if the main program were replaced.)  Compiling
     * this file with -DTEST_SCAFFOLD should do just that.
     */
    
    #ifndef TEST_SCAFFOLD
    #define TEST_SCAFFOLD 1
    #endif
    
    #define EXTERN
    #include "worder.h"
    
    FILE* finput; /* file input pointer */
    
    
    
    #if TEST_SCAFFOLD
    
    char* nextword(); /* forward declaration */
    
    main(){
       init_file("CON");
       while (nextC != EOF) {
          char* t = nextword();
          puts(t);
          /* putchar('\n');  puts adds new line*/
       }
    }
    
    #endif /* TEST_SCAFFOLD */
    
    /*
     * The nextword() function returns the next English word or punctuation
     * from standard input.   It doesn't handle any abbreviations.
     */
    
    char* nextword(){
       char* retval;
       /* set up pointer to buffer to accumulate word */
       char* p = retval = currentPointer;
    
       /* skip white space */
       while (nextC == ' ' || nextC == '\n' || nextC == '\r' || nextC == '\t'){
          nextC = fgetc(finput);
       }
    
       /* check for EOF */
       if (nextC == EOF) { return "*ENDOFFILE*"; }
    
       /* accumulate letters until punctuation */
       while (nextC != EOF && nextC != ' ' && nextC != '\t' && nextC != ','
          && nextC != ';' && nextC != ':' && nextC != '.' && nextC != '?'
          && nextC != '\n' && nextC != '!' && nextC != '"'){
          *p++ = nextC;
          nextC = fgetc(finput);
       }
    
       /* check for a blank word */
       if (p == retval){
          /* first char of word was punctuation */
          *p++ = nextC;
          nextC = fgetc(finput);
       }
    
       /* add trailing null character for C strings */
       *p = 0;
       currentPointer = p+1;
    
       /* return pointer to buffer */
       return retval;
    
    } /* nextword() */
    
    int init_file(char* fn){
       finput = fopen(fn, "r");
       if (finput == 0){
          fprintf(stderr, "could not open file %s\n", fn);
          return (1);
       }
       nextC= fgetc(finput);	/* initialize global */
       return 0;
    
    }
    
    Worder.h
    
    /*
     * A function nextword() is defined reading an English word.  Used here
     * for reading an English text from standard input and writing
     * the individual words on separate lines on standard output.
    
     */
    
    
    #include <stdio.h>
    #ifndef EXTERN
    #define EXTERN extern
    EXTERN char wordspace[100000000];	/* 100 MB buffer used to build words in. */
    EXTERN char* currentPointer;
    #else
    EXTERN char wordspace[100000000];	/* 100 MB buffer used to build words in. */
    EXTERN char* currentPointer = wordspace;	/* buffer so far */
    #endif
    
    
    /* most values passed / maintained in globals */
    EXTERN char nextC;	/* next character.  Global, so that delimiter of word can be start of next word */
    
    
    
    
    
    /*
     * The nextword() function returns the next English word or punctuation
     * from standard input.   It doesn't handle any abbreviations.
     */
    
    char* nextword();
    
    /*
     * the init_file() function sets up the input file and the nextC variable.
     */
    int init_file(char* fn);
    Regards,

    G.

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,538
    Just rename the other main to something else and call the function from the code later if you want to use it.
    And all main should return int explicitly.
    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.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,604
    And all main should return int explicitly.
    To be precise: unless you know otherwise, the main function should always be declared with an int return type, and should also explicitly return an int unless this is C99 code.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    Why dont you place all your functions in one single and all your main in other file and then link the .o file to main. Perhaps that should make it more neater and solve your problem of having muiltiple main functions.

    ssharish

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    2

    any possible changes

    Thankyou very much.

    I renamed the main in worder.c and it is compiling ad giving out put.
    But the the code is spending a lot of time.

    Can any please help me with this issue with any modifications to see where it it spending time and tune it?

    Any help is appreciated.

    Regards,

    G.

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,490
    Because jumping ship just as you're getting somewhere makes SO MUCH SENSE.
    http://forums.devshed.com/c-programm...on-497497.html
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    When making performance optimization, the first step is ALWAYS to figure out WHICH part of the code is slow - then work on improving that code.

    I have an incling on where it's spending it's time, and I have an idea on how to improve it, but like so many other things, if I just tell you, then you will not learn as much as if you work on it yourself.

    Work out which function takes most of your time. There is a function called "clock()", if you add this:
    Code:
    #include <time.h>
    // at the bottom of your include files. 
    
    // then inside each basic function [e.g. readfile, not nextword], at the beginning of the function:
       clock_t t1, t2;
       
       t1 = clock();   // get starting time.
    
    // then where the function ends/returns:
       t2 = clock() - t1;
       printf("time used in X %.2f\n", (double) t2 / CLOCKS_PER_SEC);  // replace X with function name
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    If you want to get really specific, you can use a profiler like gprof to figure out what functions are called most frequently and how much execution time is spent in each function. I don't think you can get better granularity than functions, though.
    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.

  9. #9
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,538
    There are really good profilers out there. AMD's Codeanalyst is free and can perform very advanced profiling.
    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.

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Sure, both gprof and CodeAnalyst are good tools [as is Intel's VTune for their processors - I hated it when they stopped it from working on AMD processors - because for all the better things in CodeAnalyst, VTune still has some nice slick features too].

    But for something with about 6 functions, it's probably suficient to figure out which function is the main culprit, and do some thinking.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 06-04-2009, 02:03 PM

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