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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T13:14:08+00:00 2026-05-20T13:14:08+00:00

During the running of this program, i get a segmentation fault. Im trying to

  • 0

During the running of this program, i get a segmentation fault. Im trying to solve a linear equation, with numbers received from a .dat file. I can get to the part right before the gauss function, but it dumps afterwards. Any help? thanks.

    void prob2(void)
{
        FILE *matrix;

        matrix=fopen("matrix.dat", "r");
        double a1, a2, a3, a4, **A, *a, *rhs, length;
        int k, row;
        A=(double **)malloc(4*sizeof(double *));
        printf("\n");
        for(k=1;k<=4; k++) 
        {
                a=(double *)malloc(4*sizeof(double));
                fscanf(matrix, "%lf %lf %lf %lf", &a1, &a2, &a3, &a4);
                a[0]=a1;
                a[1]=a2;
                a[2]=a3;
                a[3]=a4;
                printf(" a[%d][] = %5.2f %5.2f %5.2f %5.2f\n", k, a[0], a[1], a[2], a[3]);
        }
        rhs=(double *)malloc(4*sizeof(double));
        fscanf(matrix, "%lf %lf %lf %lf", &a1, &a2, &a3, &a4);
        rhs[0]=a1;
        rhs[1]=a2;
        rhs[2]=a3;
        rhs[3]=a4;
        printf("\nb[]={  %.3f,   %.3f,  %.3f,   %.3f  }\n", rhs[0],rhs[1],rhs[2],rhs[3]);
        printf("hiii");
        gauss(4, A, rhs);
        /* print the solution x[] stored in rhs[]:*/
        printf("x[]={%7.3f, %7.3f, %7.3f, %7.3f  }\n", 
        rhs[0], rhs[1], rhs[2], rhs[3]);
        length=sqrt(pow(rhs[0],2)+pow(rhs[1],2)+pow(rhs[2],2)+pow(rhs[3],2));
        printf("The length of x[] is  %.6lf", length);
        /* free memory */
        for(row=0; row<4; row++) free(A[row]);
        free(A); 
        free(rhs);
        fclose(matrix);

}

void gauss(int n, double **A, double *rhs)
{
/* By Gauss elimination, solve a system of equations:
   A[][]*x[] = rhs[] where A[][] (n x n)
   and rhs[] (n x 1) are input */

/* x[] is stored in rhs[] */
printf("hiii");
double one = 1.0, zero=0.0;
double b, c, d;
int nm, row, col, krow;
nm = n - 1;
if(n == 1)
{
rhs[0] /= A[0][0]; A[0][0] = one;
return;
}
/* forward reduction */
for(row=0; row<nm; row++)
{
b = A[row][row]; A[row][row] = one;
for(col=row+1; col<n; col++) A[row][col] /= b;
rhs[row] /= b;
/* sweep rows of A[row+1][] to A[nm][] */
for(krow=row+1; krow<n; krow++)
{
c = A[krow][row]; A[krow][row] = zero;
for(col=row+1; col<n; col++) A[krow][col] -= c * A[row][col];
rhs[krow] -= c * rhs[row];
}
}
/* back substitution */

rhs[nm] /=A[nm][nm];
for(row=nm-1; row>=0; row--)
{
for(col=row+1; col<n; col++) rhs[row] -= A[row][col]*rhs[col];
}
return;
}
  • 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-20T13:14:09+00:00Added an answer on May 20, 2026 at 1:14 pm

    It looks like the matrix A is not assigned. I think you need this after allocating a:

    A[k-1] = a;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Eclipse Helios periodically starts running a job which displays Computing additional info. During this
If the user received a call during application running, how to automatically open the
Scenario: I have to call an external program from my Ruby script, and this
I have a program that I run and in the middle I get this
I'm using RHEL5 kernel 2.6.33 and running GCC v4.1.2. During my program compilation, I
Why is it that when running code from gdb, I get the same addresses
i have application who use specific process during running and i want to sample
We are using Team City and I noticed problem during running Watin test. Sometimes
how can i check admin-privileges for my script during running?
At a certain point, during the running of my app, I want Castle.Windsor to

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.