Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6089709
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T12:07:51+00:00 2026-05-23T12:07:51+00:00

I have a code, that was not made by me. In this complex code,

  • 0

I have a code, that was not made by me.
In this complex code, many rules are being applied to calculate a quantity, d(x). in the code is being used a pointer to calculate it.

I want to calculate an integral over this, like:
W= Int_0 ^L d(x) dx ?

I am doing this:

#define DX 0.003
void WORK(double *d, double *W)
{
    double INTE5=0.0;
    int N_X_POINTS=333;
    double h=((d[N_X_POINTS]-d[0])/N_X_POINTS); 
    W[0]=W[0]+((h/2)*(d[1]+2.0*d[0]+d[N_X_POINTS-1])); /*BC*/
    for (i=1;i<N_X_POINTS-1;i++) 
    {
        W[i]=W[i]+((h/2)*(d[0]+2*d[i]+d[N_X_POINTS]))*DX;
        INTE5+=W[i];
    }
    W[N_X_POINTS-1]=W[N_X_POINTS-1]+((h/2)*(d[0]+2.0*d[N_X_POINTS-1]+d[N_X_POINTS-2])); /*BC*/
}

And I am getting “Segmentation fault”. I was wondering to know if, I am doing right in calculate W as a pointer, or should declare it as a simple double? I guess the Segmentation fault is coming for this.

Other point, am I using correctly the trapezoidal rule?

Any help/tip, will very much appreciate.

Luiz

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T12:07:52+00:00Added an answer on May 23, 2026 at 12:07 pm

    I don’t know where that code come from, but it is a lot ugly and has some limits hard-encoded (333 points and increment by 0.003). To use it you need to “sample” properly your function and generate pairs (x, f(x))…

    A possible clearer solution to your problem is here.

    Let us consider you function and let us suppose it works (I believe it does’t, it’s a really obscure code…; e.g. when you integrate a function, you expect a number as result; where’s this number? Maybe INTE5? It is not given back… and if it is so, why the final update of the W array? It’s useless, or maybe we have something meaningful into W?). How does would you use it?

    The prototype

      void WORK(double *d, double *W);
    

    means the WORK wants two pointers. What these pointers must be depends on the code; a look at it suggests that indeed you need two arrays, with N_X_POINTS elements each. The code reads from and writes into array W, and reads only from d. The N_X_POINTS int is 333, so you need to pass to the function arrays of at least 333 doubles:

       double d[333];
       double W[333];
    

    Then you have to fill them properly. I thought you need to fill them with (x, f(x)), sampling the function with a proper step. But of course this makes no too much sense. Already said that the code is obscure (now I don’t want to try to reverse engineering the intention of the coder…).

    Anyway, if you call it with WORK(d, W), you won’t get seg fault, since the arrays are big enough. The result will be wrong, but this is harder to track (again, sorry, no “reverse engineering” for it).

    Final note (from comments too): if you have double a[N], then a has type double *.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following bit of legacy C++ code that does not compile: #include
I have some code which I did not originally create that uses _beginthreadex and
I have heard people state that Code Generators and T4 templates should not be
Following on from this question I now have code that can attach to a
Right now, I have code that looks something like this: Private Sub ShowReport(ByVal reportName
I have code that references a web service, and I'd like the address of
I have code that looks like the following, which works fine for displaying the
I have code that looks like: //System.Data.IDataRecord dr try { Consolidated = Utility.NullConvert.ToBool(dr[Constants.Data.Columns.cConsolidated], false);
Hashtables have a syncroot property but generic dictionaries don't. If I have code that
I am querying information from Active Directory . I have code that works, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.