Thread: [Programming Pearls] Gerrymandering Problem

  1. #1
    Registered User
    Join Date
    Nov 2013
    Posts
    4

    Unhappy [Programming Pearls] Gerrymandering Problem

    I want to start the Gerrymandering problem with memory limitation to 27000, but not 16*16000, so bellow program is for that. However, problems for compilation met. I would say, well, really cannot tell what's wrong in line 22 and 61!
    Anyone who can help? Thanks in advance!
    ---- compilation error:
    $ gcc Gerrymandering.c -o Gerrymandering.out
    Gerrymandering.c:22: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘setDistrict’
    Gerrymandering.c: In function ‘readDistricts’:
    Gerrymandering.c:61: error: expected expression before ‘short’

    ---- source code:
    Code:
    #include <stdio.h>
    
    
    #define SORTMEMLIMIT     27000 /* (16*1000) */
    #define MAXDISTRICT    27000
    #define SORTTYPE    short
    #define SORTMEMSIZE    (SORTMEMLIMIT/sizeofS(SORTTYPE))
    //#define DISTRICTS     ("disricts.txt")
    char districtsFile[] = "districts.txt";
    
    
    int main()
    {
        readdistricts(districtsFile);
        return 0;
    }
    
    
    
    
    /*
    FILE: setDistrict
    input:
        district, scope: 1-MAXDISTRICT
    */
    bool setDistrict(SORTTYPE districtsBitmap[], int district, int bitmapLimit)
    {
        bool success = false;
        if(district >= bitmapLimit)
        {
            printf("ERROR: invalid distict number: %d for limit: %d!\n", district, bitmapLimit);//try __FILE__ __LINE__ later
        }
        else
        {
            int bitPosition = (district-1)%sizeof(SORTTYPE);
            int arrayPosition = (district-1)/sizeof(SORTTYPE);
            SORTTYPE move = 1;
            SORTTYPE setBit = move<<bitPosition;
            
            if(*(districtsBitmap+arrayPosition)&setBit)
            {
                printf("ERROR: district: %d was set already!", district);
            }
            else
            {
                *(districtsBitmap+arrayPosition) |= setBit;
                success = true;
            }
        }
    
    
        return success;
    }
    
    
    int readDistricts(const char* districtsFile)
    {
        FILE *fpDistricts;
        if(!(fpDistricts = fopen(districtsFile, "r+")))
        {
            return 1;
        }
    
    
        int district;
        int counter = 0;
        int failedDistrictCounter = 0;
        SORTTYPE districtsBitmap[SORTMEMSIZE];    
    
    
        while( ((++counter)< SORTMEMLIMIT)&&(1==fscanf(fpDistricts, "%d", &district)) )
        {
            if(!setDistrict(districtsBitmap, district,SORTMEMLIMIT))
            {
                failedDistrictCounter++;
            } 
        } 
        fclose(fpDistricts);
        printf("INFO: districts sorting finished. Districts number of failed operation: %d, out of %d\n", failedDistrictCounter, counter);
    
    
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Nov 2013
    Posts
    4
    pasted codes, lines are different from what showed in linux. so error line 22 is 26 here, while 61 should be 68.

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    gcc tells me that bool is not a valid type name, unless you have defined or typedef'd it somewhere.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  4. #4
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Turn up your compiler warnings.


    "bool" is not part of C89 and is not defined in C99 unless you include stdbool.h. (Same with true and false.)


    The other line demonstrates the dangers of macros. Since they work by textual replacement what you see is NOT what you get. Look at the definition of the macros used on that line. One of them has "sizeofS" in it, which should presumably be "sizeof".


    Also, you should put prototypes of your functions before main.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  5. #5
    Registered User
    Join Date
    Nov 2013
    Posts
    4
    Quote Originally Posted by Elkvis View Post
    gcc tells me that bool is not a valid type name, unless you have defined or typedef'd it somewhere.
    yes, bool is a standard prototype for c. Well, it was just so natural to use bool after working on other languages:P
    Thank you

  6. #6
    Registered User
    Join Date
    Nov 2013
    Posts
    4
    Quote Originally Posted by oogabooga View Post
    Turn up your compiler warnings.


    "bool" is not part of C89 and is not defined in C99 unless you include stdbool.h. (Same with true and false.)


    The other line demonstrates the dangers of macros. Since they work by textual replacement what you see is NOT what you get. Look at the definition of the macros used on that line. One of them has "sizeofS" in it, which should presumably be "sizeof".


    Also, you should put prototypes of your functions before main.
    Hi, problems were solved and they are exactly what you described Except the prototypes part. I also remember that, if a function is invoked before its definition, prototype is needed to be announced. However, magic thing is, with my compilation environment, there's no compilation error reported without the annoucing. Is it strange?
    Thank you

  7. #7
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by standstand View Post
    Hi, problems were solved and they are exactly what you described Except the prototypes part. I also remember that, if a function is invoked before its definition, prototype is needed to be announced. However, magic thing is, with my compilation environment, there's no compilation error reported without the annoucing. Is it strange?
    Thank you
    If you leave out the prototypes then C89 will assume a return value of int (which is what your functions return) and will assume nothing about the parameters. I believe the C99 standard does not allow you to leave them out, though.


    However, you forgo certain kinds of error checking if you leave them out, so it's better to put them in.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem programming :s
    By RA-3000 in forum C Programming
    Replies: 3
    Last Post: 05-10-2010, 01:26 PM
  2. Programming Problem
    By alokrsx in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 05-14-2007, 12:41 AM
  3. programming problem
    By Antigloss in forum C Programming
    Replies: 2
    Last Post: 06-02-2005, 08:08 PM
  4. problem with programming in C
    By yairzarka in forum C Programming
    Replies: 8
    Last Post: 03-19-2005, 03:49 PM
  5. C++ Programming (Problem One)
    By Nicknameguy in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2002, 06:38 PM

Tags for this Thread