Thread: Invalid Operands

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    10

    Invalid Operands

    Hi, I'm very new to programming, this being my second program. I've been working on this and I still can't seem to get this last section to work. I'm supposed to use a programmer defined function to solve for the area of the rectangle for an integral but I cant get past the line where it has the equation. The message it says is "invalid operands of types `double ()(double)' and `double ()(double)' to binary `operator*'. I looked it up online and couldnt find anything to really tell me what was wrong other than it's typically something simple to fix which makes me feel programming retarded lol. Any help will be appreciated!!

    Here is the part of my program that keeps messing up.\

    double y;
    double rect_area;
    double total_area;
    double area (double low_x, double high_x, double delta_x);
    {

    y = ((cos * cos) (low_x)) + ((cos * cos)(low_x * low_x));
    rect_area = (y * delta_x);

    printf("rectangle area is: %lf", y);

    system("pause");

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Hmm. Looks like you are using a function-name (cos) as a variable, and you haven't declared it either.

    What is the formula you are actually trying to apply?

    --
    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.

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    10
    y = cos^2 (x) + cos^2 (x^2)

    I thought if you included the math.h file it would recognize cos and sin as well as everything else. I used a square root function before and it worked fine. I changed around my area function and that just made my computer angry

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by chocobo59 View Post
    y = cos^2 (x) + cos^2 (x^2)

    I thought if you included the math.h file it would recognize cos and sin as well as everything else. I used a square root function before and it worked fine. I changed around my area function and that just made my computer angry
    You have to CALL the function: cos(x). The compiler doesn't psychically know that you want to apply cos to the variable x.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    And just to clarify, cos^2(x) should be written as cos(x) * cox(x).

    Since cos itself is a fairly expensive[1] operation, it may be better to use a temporary variable, as it's not guaranteed that the compiler optimizes two calls to cos with the same value:
    Code:
       cos_x = cos(x);
       result = cos_x * cos_x;
    --
    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.

  6. #6
    Registered User
    Join Date
    Oct 2007
    Posts
    10
    Thank you!! That helped me get past the stupid invalid operands issue. And also for the correction of my math equation. I can't seem to get the math equations to use my user inputed values though. To the tutorials!

    Thanks again!

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    What are the input values? Is it by any chance that you are using degrees, when the functions require radians?

    Divide by 180, multiply by pi to get radians from degrees.

    --
    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. how do you resolve this error?
    By -EquinoX- in forum C Programming
    Replies: 32
    Last Post: 11-05-2008, 04:35 PM
  2. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Need help with C program
    By ChrisH in forum C Programming
    Replies: 38
    Last Post: 11-13-2004, 01:11 AM
  5. misc.c:3210: invalid operands to binary +
    By Unregistered in forum Linux Programming
    Replies: 2
    Last Post: 07-19-2002, 07:00 AM