Thread: GCC compiler giving syntax error before 'double' error

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

    Unhappy GCC compiler giving syntax error before 'double' error

    On my Linux computer, I am using the command:

    gcc programname.c -lm

    for the following code:
    Code:
    #include<time.h>
    
    #include<stdio.h>
    
    #include<math.h>
    
    #include<stdlib.h>
    
    
    
    int main(void)
    
    {
    
    
    
    int p, d, h, what, end, steps;
    
    double i, n, M, c, w, q, measure;
    
    end = 2000;
    
    steps = 1000;
    
    double average1[end][2], average2[end][2];
    
    int width[end][steps];
    
    
    
    //w is equal to the possible number of ways to choose a reactant molecule,
    
    // which in our case is equal to how many molecules exist in the system
    
    w = 0;
    
    
    
    what = 10000;
    
    measure = 0.01;
    
    
    
    //c defines the propensity of the reaction (change to 0.5, 1.0, and 10)
    
    c = 10;
    
    
    
    q = 0;
    
    //concentration
    
    int cnctrn;
    
    cnctrn = 1000;
    srand((unsigned int)time(NULL));
    
    for (d = 0; d < end; d++){
    
    for (h = 0; h < 2; h++){
    
    average1[d][h] = 0;
    
    average2[d][h] = 0;
    
    }
    
    }
    
    for (p = 0; p < end; p++){
    
    for (d = 0; d < steps; d++){
    
    width[p][d] = 0;
    
    }
    
    }
    
    
    
    
    
    for (p = 1; p < steps; p++){
    
    
    
    width[0][p] = cnctrn;
    
    }
    
    
    
    
    
    //PLotting in Matlab
    
    FILE * pFile;
    
    char name [100];
    
    pFile = fopen ("Gillespie.txt","w");
    
    
    
    n = 0;
    
    d = 0;
    
    
    
    for (h = 0; h < steps; h++){
    
    
    
    while (i < what && cnctrn > 1){
    
    
    
    q = double((rand())/(double(RAND_MAX + 1)));
    
    
    
    
    
    for (d = 0; d < end; d = d+1){
    
    if ( i < (d+1)*measure && i >= d*measure){
    
    width[d][h] = cnctrn;
    
    }
    
    }
    
    //exact data comparison
    
    w = cnctrn*c;
    
    M = -log(q)/w;
    
    cnctrn = cnctrn-1;
    
    
    
    i = i + M;
    
    n = n + 1;
    
    fprintf(pFile, "%f %i\n ", i, cnctrn);
    
    }
    
    }
    
    for (p = 1; p < end; p++){
    
    for (d = 0; d < steps; d++){
    
    if (width[p][d] == 0){
    
    width[p][d] = width[p][d-1];
    
    }
    
    }
    
    }
    
    fclose (pFile);
    
    
    
    for (d = 0; d < end; d++){
    
    average1[d][0] = d*measure;
    
    average2[d][0] = d*measure;
    
    }
    
    
    
    
    
    
    
    for (d = 0; d < end; d++){
    
    for (h = 0; h<steps; h++){
    
    average1[d][1] = average1[d][1] + double(width[d][h])/double(steps);
    
    average2[d][1]=average2[d][1]+
    
    double(width[d][h])*double(width[d][h])/double(steps);
    
    }
    
    }
    
    
    
    
    
    pFile = fopen ("average.txt","w");
    
    for (d = 0; d < end; d++){
    
    fprintf(pFile, "%f %f\n ", average1[d][0], average1[d][1]);
    
    }
    
    fclose (pFile);
    
    pFile = fopen ("averagesquared.txt","w");
    
    for (d = 0; d < end; d++){
    
    fprintf(pFile, "%f %f\n ", average2[d][0], average2[d][1]);
    
    }
    
    fclose (pFile);
    
    }
    I keep getting this error for the lines I put in bold:
    syntax error before 'double'



    Putting (double) in parenthesis gets rid of the error, but outputs the wrong text files. How do I fix this error without just putting the (double) in parenthesis to get the correct output?
    Any help with this would be very very appreciated! Thanks!

  2. #2
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    Gah! Indent, indent, indent your code!

    This line:
    Code:
    q = double((rand())/(double(RAND_MAX + 1)));
    You probably want casts... you're trying to use a type as a function. (Looks like C++, but you're compiling as C - What language do you want?) In C, this would probably be:
    Code:
    q = (double) rand() / (double) (RAND_MAX + 1);
    q will then be a random from [0,1).

    EDIT: You're also allocating over 7.5MB of data on the stack. You have either a large stack, or may very well overflow it on some system. (Like mine.) (Not sure what the defaults are...)

    Also, things like:
    Code:
    	for (d = 0; d < end; d++)
    	{
    
    		for (h = 0; h < 2; h++)
    		{
    			average1[d][h] = 0;
    			average2[d][h] = 0;
    		}
    	}
    Since you're setting the entire thing to zero, in C, you can do:
    Code:
    memset(average1, 0, sizeof(average1);
    memset(average1, 0, sizeof(average2);
    ...or simply initialize the array to zero when you declare it:
    Code:
    int myarray[50] = {0};
    Last edited by Cactus_Hugger; 06-02-2007 at 01:05 PM.
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by Cactus_Hugger View Post
    Since you're setting the entire thing to zero, in C, you can do:
    Code:
    memset(average1, 0, sizeof(average1);
    memset(average1, 0, sizeof(average2);
    Not the best choice for floating point. Zero-filling does not guarantee floating-point zeros.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  4. #4
    Registered User
    Join Date
    Jun 2007
    Posts
    2

    how to fix 'integer overflow in expression' error

    so I made those changes, putting (double) in parenthesis, but now I get the integer overflow error. how would i fix that? and the only way I am able to open my text files is by double clicking the a.out file that is created when I compile it, then the three text files appear. by the way, this is c code.

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how do you resolve this error?
    By -EquinoX- in forum C Programming
    Replies: 32
    Last Post: 11-05-2008, 04:35 PM
  2. Errors including <windows.h>
    By jw232 in forum Windows Programming
    Replies: 4
    Last Post: 07-29-2008, 01:29 PM
  3. Crazy errors caused by class, never seen before..
    By Shamino in forum C++ Programming
    Replies: 2
    Last Post: 06-10-2007, 11:54 AM
  4. using c++ in c code
    By hannibar in forum C Programming
    Replies: 17
    Last Post: 10-28-2005, 09:09 PM
  5. ras.h errors
    By Trent_Easton in forum Windows Programming
    Replies: 8
    Last Post: 07-15-2005, 10:52 PM