basic maths

This is a discussion on basic maths within the C Programming forums, part of the General Programming Boards category; Hi, I am trying to work out how to write an equation in C, i know it sounds stupid, but ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    190

    basic maths

    Hi, I am trying to work out how to write an equation in C, i know it sounds stupid, but it when i set a variable, a equal to an equation, it says there is bad sytax before the =

    i.e

    Code:
    int trapezium()//this part copes with calculating in value of f(x) and then outputting the estimated area under the graph
    {
    float fx[11],y, a,b,c;
    
        for(i=0;i<11;i++)
        {
         fx[i] = (1 - v[i])/U;
    
         printf("function of x at speed %5.2f, is %5.2f\n", v[i], fx[i]);
        }
    
        a =dx/2 ;
    
        y = (2*(((1-v[1])/U) + ((1-v[2])/U) + ((1-v[3])/U)+ ((1-v[4])/U) + ((1-v[5])/U)+ ((1-v[6])/U)+ ((1-v[7])/U)+ ((1-v[8])/U)+ ((1-v[9])/U))));
        printf("y = %f", y);
    }
    where it says a =dx/2 ; the error here is

    E:\xxxxxxxxxxxxxxxxx\Boundary_Layer_Displacement\m ain.c|35|error: syntax error before '=' token|

    i think there are a few brackets wrong with the y= but that can be sorted...

    cheers guys

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,794
    What is dx? It looks like you forgot to define it.
    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

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    And this:
    Code:
        y = (2*(((1-v[1])/U) + ((1-v[2])/U) + ((1-v[3])/U)+ ((1-v[4])/U) + ((1-v[5])/U)+ ((1-v[6])/U)+ ((1-v[7])/U)+ ((1-v[8])/U)+ ((1-v[9])/U))));
    Can be done in a loop, which would make it MUCH more readable.

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

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    214
    Also, where are v and U declared?

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    190
    Cheers guys that was enough to get me through that problem, and yeah have now done that bit in a loop, but it gives out RANDOM answers, the function is:

    Code:
    int trapezium()//this part copes with calculating in value of f(x) and then outputting the estimated area under the graph
    {
    float fx[11],y,a,b;
    
    U = v[10];
    
            for(i=0;i<11;i++)
            {
                fx[i] = ((i - v[i])/U);
    
                    for (n=1;n<10;n++)
                        {
                            a = a +fx[n]; //a is a section of the trapezium rule
                            printf("a = %5.2f\n", a);
                        }
            }
        float dx = 0.125;
    
        b= fx[0] + fx[10];
    
        y = (dx/2) * (b + (2*a));
    
        printf("y = %5.2f", y);
    }
    the answer comes out as y =1#R

    have i done something like multiply a foat by an integer or something stupid like that?

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    When you get "1#xxx" as the output, it usually means that you have an invalid floating point value - usually caused by using uninitialized data or doing stupid things like divide by zero, log(negative), sqrt(negative) and other "invalid" math operations.

    Divide by zero is normally caught by the processor. Other invalid math is often not caught.

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

  7. #7
    Registered User
    Join Date
    Dec 2007
    Posts
    214
    In the code you show, you still haven't declared the U and v[] variables. What type are they? Are they global variables?

  8. #8
    Registered User
    Join Date
    Apr 2008
    Posts
    190
    OK so the entire program is below:

    Code:
    #include <stdio.h>
    //#include <math.h>
    
    const float dx = 0.125; //change in x displacement, i.e measurment interval.
    
    float v[11], U;//My global floating varables
    int i,n;//My global integers, counts for loops etc
    
    int collectdata() //retrieves data from text file
    {
        int i;
    
        FILE *fp=NULL; //create a pointer, fp to point at the file the data points are stored at
        fp = fopen("data.txt", "r"); //open the data file from the saved location and read from it
    
                for (i=0; i<11; i++) //for loop scans each data point from the file and stores it in the velocity array, v.
                {
                 fscanf (fp, "%f\n", &v[i]);   //scans for integers in data file
                }
    
        fclose(fp); //close data file
    
    }
    
    int trapezium()//this part copes with calculating each value of f(x), i.e f(x1), f(x2) etc and then outputting the estimated area under the graph
    {
    float fx[11],y,a,b;
    //a and b are variables that help towards the final value for y, y being the displacement thickness
    //fx is the function value, in this case the displacement thickness
    
    
            for(i=0;i<11;i++)
            {
                fx[i] = ((1 - v[i])/U);
    
            }
            for (n=1;n<10;n++)
                        {
                            a = a +fx[n]; //a is a section of the trapezium rule
                            //printf("a = %5.2f\n", a);
                        }
    
    
        b= fx[0] + fx[10]; //more trapezium rule
    
        y = (dx/2) * (b + (2*a)); // final trapezium rule
    
        printf("y = %5.2f", y); //print the estimated area under the graph
    }
    
    
    
    int main()
    {
        collectdata();//runt he function to collect the data from the text file
    
       for(i=0;i<11;i++)//for loop to print all data points to the screen
        {
         printf("v%d= %5.2f\n", i+1, v[i]); //print the speeds retireved from the file
        }
    
        U = v[10]; //set U to the free stream veolcity - the greatest recorded speed above the wing
    
        trapezium(); // run the function to calculate the displacement thickness
    
        return 0;
    }
    Like it is it outputs an answer y=-0.72

    But it prints all the values for the speed above, from the line

    printf("v%d= %5.2f\n", i+1, v[i]); //print the speeds retireved from the file in the main function,

    if you remove that line you get y=#R

  9. #9
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    a = a +fx[n]

    a is not initialized so the result contains garbage as well
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. [ANN] New script engine (Basic sintax)
    By MKTMK in forum C++ Programming
    Replies: 1
    Last Post: 11-01-2005, 09:28 AM
  2. what are your thoughts on visual basic?
    By orion- in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 09-22-2005, 04:28 AM
  3. visual basic vs C or C++
    By FOOTOO in forum Windows Programming
    Replies: 5
    Last Post: 02-06-2005, 07:41 PM
  4. Basic Window Creation, Dev C++ 4.9.9.0 Linking Error
    By Tronic in forum Windows Programming
    Replies: 2
    Last Post: 11-27-2004, 05:03 PM

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