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 6855249
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:44:01+00:00 2026-05-27T01:44:01+00:00

I was trying to get a mathematical result but I kept on getting a

  • 0

I was trying to get a mathematical result but I kept on getting a segmentation fault, the segmentation fault occurs at the “cout” line. I don’t think anything is wrong in the r[] array or the LJ[] array.

   for (k = 0; k < 15; k++)
    {
        for (i = 0; i < 15; i++)
        {
            if (i == k)
            {
                NULL;
            }
            else
            {
                //Use the 3D distance formula
                term1 = fabs(x[k] - x[i]);
                term1 = pow(term1, 2);
                term2 = fabs(y[k] - z[i]);
                term2 = pow(term2, 2);
                term3 = fabs(z[k] - z[i]);
                term3 = pow(term3, 2);
                result = term1 + term2 + term3;
                result = sqrt(result);
                r[h] = result;                      //Store the result in an array
                h++;
            }
        }
    }




    //Calculate Lennard-Jones potential of every pair
    for(itr = 0; itr < 210; itr++)
    {
        term1 = pow(r[itr], 12);
        term1 = 1/term1;
        term2 = pow(r[itr], 6);
        term2 = 2/term2;
        LJ[itr] = term1 - term2;
    }

    double Ei;

    for(itr = 0; itr < 210; itr++)
    {
        Ei = LJ[itr] + Ei;
    }
    Ei = Ei/2;
    cout << "The new Energy level " << Ei << endl;
  • 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-27T01:44:02+00:00Added an answer on May 27, 2026 at 1:44 am

    Update: you have updated the definition of LJ, so this is no longer the issue.
    Since this is c++, why are you using loop and indexing? a better (as in: a safer) solution would be to use the standard library algorithms and in particular accumulate.
    By doing this you would gain in clarity and possibly remove the source of the segfault.

    Unless other requirements are constraining you, you should follow Scott Meyer’s advice of favoring algorithms over loops.

    Second Update, with the whole code available: You must make sure that you are only accessing memory locations which you correctly allocated. So:

    1. make sure that h is initialized at zero and
    2. that you are never writing to r[210] and higher.

    Better yet, remove all those magic numbers, instead of 210 use k_max * (j_max – 1) and define

    const unsigned int k_max = 15;
    const unsigned int j_max = 15;
    

    Use a std::vector instead of an array, this will take care of memory allocation for you.

    Printing the values of h and itr (or using a debugger, which is a very useful tool) will show you where you are accessing memory which you didn’t reserve.

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

Sidebar

Related Questions

Trying to get an ASP application deployed; it worked for a while but then
I'm trying to get a deeper understanding in Python's data model and I don't
I'm currently trying to get something to run involving trigonometry but I've run across
I am trying to get emacs to properly render mathematical combining characters such the
I don't know if that's the right word for it, but I am trying
I'm trying get values from a GridView using the following code: foreach (GridViewRow row
Trying to get my css / C# functions to look like this: body {
Trying to get this example working from http://www.munna.shatkotha.com/blog/post/2008/10/26/Light-box-effect-with-WPF.aspx However, I can't seem to get
Trying to get parameters from a PUT request using HttpServlet#doPut: public void doPut(HttpServletRequest request,
Trying to get comfortable with jQuery and I have encountered some sample code that

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.