Thread: Student Looking for Help Again.

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    28

    Student Looking for Help Again.

    Hey,

    First off, I want to thank everyone for helping me so much. (I've discovered that programming really isn't my thing-- and you guys have really been helping me through it by helping me to fix and learn from my mistakes.)

    Alright, but now on to my next problem. I'm working on a program that'll estimate the area under a curve given a specific equation. I'm working out the logic, having a difficult time, but am overall getting by little by little.

    Now, we're working on learning functions. And for some reason, with or without the functions included in my program, I'm getting an error every time I try to compile:

    "parse error before numeric constant"

    I'm using DevC++ and am getting it on line 29.... The double a line is being highlighted (yes, I'm aware that the problem lies on the line before the double a)

    I also want to quickly note that THIS IS NOT THE FULL PROGRAM. I HAVEN'T COMPLETELY FILLED IN ALL OF THE LOGIC YET

    but here it is:

    Code:
    
    #include <stdio.h>
    
    
    
    #define a 0.0
    #define b 1.0
    
    double x(), f(), trapezoidal();
    
    int main(void)
    {
    	double a;
        double b;
        double x;
        double fx;
        double accuracy;
        int n;
        
        printf("TRAPEZOIDAL PROGRAM\n\nThis program calculates the area of a given function:\nf(x) = 2x^2 + 3x + 1\nIn the interval [0,1].\n\n");
        
        return 0;
    	
    }
    
    double x(int n, double x)
    {
        x = (1/n);
        
        return x;
    }
    
    double f(double x, double fx)
    {
        fx = 2 * x * x + (3 * x) + 1;
        
        return fx;
    }
    
    double trapezoidal(double a, double b, int n)
    {
        return trapezoidal;
    }

    I've been working on ths program forever, and am having a considerable amount of difficulty with it. Does anyone know what I might be doing wrong?

    Like I said, I'm aware of the incomplete functions and everything...

    Any ideas?

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Code:
    #include <stdio.h>
    
    
    
    #define a 0.0
    #define b 1.0
    
    double x(), f(), trapezoidal();
    
    int main(void)
    {
    	double a;
        double b;
    What are you using the #defines for? Do you realize this is screwing up other code? For example, following preprocessing the start of main looks like this.
    Code:
    int main(void)
    {
    	double 0.0;
        double 1.0;
    And this
    Code:
    double trapezoidal(double a, double b, int n)
    becomes this
    Code:
    double trapezoidal(double 0.0, double 1.0, int n)
    [edit]This will do integer math and then convert the result to a double, probably not what you want:
    Code:
    double x(int n, double x)
    {
       x = (1/n);
    
       return x;
    }
    You don't really need to pass the parameter x -- you could just return 1.0 / n. A similar comment for the function f.

    Don't return the name of a function:
    Code:
    double trapezoidal(double a, double b, int n)
    {
       return trapezoidal;
    }
    Last edited by Dave_Sinkula; 03-29-2005 at 01:07 PM.
    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.*

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    28
    Woo, okay.

    Thank you, first of all.

    The a and b will always equal 0.0 and 1.0... According to my problem statement, I must also use a variable for both a and b... (a and b are supposed to be the interval of this curve that I'm estimating the area of. So it's supposed to be fixed.)

    The x function is also required, but that's not the end of it. I have more to do to it. I was just getting the general skeleton of the program down... So there will be more to it than 1/n.

    Returning the name of a function-- thank you. I'd have returned the name, stupid of me, but thanks for making me realize that.


    As for the a and b, I see my stupid mistake. Because I'm already defining them, I don't need to declare them as variables. (You see the kind of stupid mistakes that I make!?) Or, I could just declare a value for them in main(). Yeah, I see my mistake. Thank you.
    Last edited by verd; 03-29-2005 at 01:29 PM.

  4. #4
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    #define A
    #define B

    ...

    double a, b;

    C is case-sensitive.....this is not very good practice though. Consider thinking of different names? Variables can be longer than one letter.

  5. #5
    Registered User
    Join Date
    Mar 2005
    Location
    Varaždin, Croatia
    Posts
    11
    Yes, be careful with the names, you have a function called x and an argument of the same function called x, watch out for globals and defined variables (a and b). You have the same names int the function trapezoidal() and main().
    and like nonpuz said, variables can be longer that one letter.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Updating in a sequential file?
    By Ronnyv1 in forum C Programming
    Replies: 1
    Last Post: 03-24-2009, 04:41 PM
  2. Database assignment is Killing me!
    By Boltrig in forum C Programming
    Replies: 2
    Last Post: 11-29-2007, 03:56 AM
  3. LinkList Sorting in C
    By simly01 in forum C Programming
    Replies: 3
    Last Post: 11-25-2002, 01:21 PM
  4. Creating a student grade book-how?
    By Hopelessly confused in forum C Programming
    Replies: 5
    Last Post: 10-03-2002, 08:43 PM
  5. A few tips please...
    By gems in forum C++ Programming
    Replies: 4
    Last Post: 04-11-2002, 02:28 PM