segmentation fault when I take in the first value of matrix - c

If I run my program, and When I typed the dimensions of matrix, after I typed the first value of matrix , the console write out: Segmentation fault
For example:
4
3
Segmentation fault
Process returned 139(0x8B)
void inMatrix(int n, double **matrix)
{
int j, i;
for (i = 0; i < n; i++)
{
for (j= 0; j < n; j++)
{
scanf("%lf", &matrix[i][j]);
}
}
}
void inVector(double *vektor, int n)
{
int k;
for (k = 0; k < n; k++)
{
scanf("%lf", &vektor[k]);
}
}
int main()
{
int n;
// read dimension of matrix and value
scanf("%d", &n);
//matrix
double** matrix = (double **) calloc(n, sizeof ( double*));
//vector
double* vector = (double *) calloc(n, sizeof ( double));
// read values of matrix
inMatrix(n, matrix);
// read values of vector
inVector(vector, n);
outVector(vector, n);
printf("\n");
return 0;
}

You didn't allocate memory for the elements of the matrix, only for the pointers to the individual lines.
You need something like:
for (i = 0; i < n; i++)
matrix[i] = malloc(n * sizeof(double));
Of course, you must free stuff in the same manner when you're done with it.
for (i = 0; i < n; i++)
free(matrix[i]);
free(matrix);

Related

Error in finding the sum of diagonals of an array

I am trying to find the sum of the diagonals for the following array seen below, but whenever I call the function diagsfunc and run the program it does not give the answer,
I made an if statement to make sure the array is a square matrix, and if it is, it proceeds to call the function diagsfunc where I find the sum of the diagonal elements however whenever I run it does not print the sum.
Below is the function, main and the array. all the other functions work fine except for the diagonal one. and in case you are wondering the file contains the following numbers, where the first 2 numbers are just the rows and columns and the last 2 numbers are specific rows and columns of the array used for functions:
The file contains:
4 4
5.5 7.8 7.8 3.3
2.2 1.1 4.4 7.7
9.9 7.6 4.4 6.6
9.0 4.0 5.0 2.0
2
3
The code:
/*Diagonal function*/
double
diagsfunc (double ** arr, int diagr, int diagc)
{
int f, g;
double sum3 = 0;
for (f=0;f<diagr;++f)
{
for (g=0;f<diagc;++g)
{
if (f == g)
{
sum3 = sum3 + arr[f][g];
}
}
}
return(sum3);
}
int
main (void)
{
int cols, rows, i, j, nrows, ncols;
double **a;
double diag;
FILE * fpointer;
fpointer = fopen("projectfile.txt", "r");
fscanf(fpointer, "%d %d", &rows, &cols);
a = (double **) calloc (rows, sizeof(double *));
for (i=0; i<rows; ++i)
a[i] = (double *) calloc (cols, sizeof(double));
for (i = 0; i < rows; ++i)
for ( j = 0; j < cols; ++j)
fscanf(fpointer, "%lf", &a[i][j]);
for (i = 0; i < rows; ++i){
for ( j = 0; j < cols; ++j){
printf("%10.3lf ", a[i][j]);
}
puts("");
}
fscanf(fpointer, "%d %d", &nrows, &ncols);
if (rows == cols){
diag = diagsfunc(a, rows, cols);
printf("The sum of the diagonal elements is: %.3lf\n", diag);
} else {
printf("The array must be square to compute the sum of diagonals\n");
}
fclose(fpointer);
return(0);
}
There are few silly mistakes you did, lot more, but I am listing just required one
wrong variable used in the loop for (g=0;f<diagc;++g).
It should be
for (g = 0;g < diagc; ++g)
to find sum of diagonal, you no need 2 loops as pointed by Pablo you can do using one loop itself as
for(int i = 0; i < diagr; ++i) { /* if its a square matrix */
sum3 + = arr[i][i]
}
Complete Code
double diagsfunc (double ** arr, int diagr) {
int f, g;
double sum3 = 0;
for(int i = 0; i < diagr; ++i) {
sum3 += arr[i][i];
}
return sum3;
}
int main (void){
int cols, rows, i, j, nrows, ncols;
double **a;
double diag;
FILE * fpointer = fopen("projectfile.txt", "r");
if(fpointer == NULL ) {
/* put some message */
return 0;
}
fscanf(fpointer, "%d %d", &rows, &cols);
a = (double **) calloc (rows, sizeof(double *));
for (i=0; i<rows; ++i)
a[i] = (double *) calloc (cols, sizeof(double));
for (i = 0; i < rows; ++i)
for ( j = 0; j < cols; ++j)
fscanf(fpointer, "%lf", &a[i][j]);
for (i = 0; i < rows; ++i){
for ( j = 0; j < cols; ++j){
printf("%10.3lf ", a[i][j]);
}
puts("");
}
//rewind(fpointer); /* this you forget */
//fscanf(fpointer, "%d %d", &nrows, &ncols); /* why this statement ? */
if (rows == cols){
diag = diagsfunc(a, rows); /* no need to pass col value as its square matrix, both will be equal */
printf("The sum of the diagonal elements is: %.3lf\n", diag);
} else {
printf("The array must be square to compute the sum of diagonals\n");
}
fclose(fpointer);
return(0);
}
Also check all functions return value & learn how to debug your code.

Code doesn't read input file correctly

My code doesn't seem to be able to read the input file correctly. It somehow only reads the first line of my matrix and then it inputs the second line under "right hand side" instead of making another line for the matrix under "coefficient matrix". Additionally, it prints the third line under "Initial Guesses" rather than the third line of the matrix.
I'm assuming the error is somewhere in the code that I have posted below but let me know if you believe the code below is correct and somewhere else in my code is where this problem is originating from.
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#define MAX_DIM 100
#define MAX_ITER 500
#define TOLERANCE 1.e-6
void gauss_seidel(double **a, double *b, double *x, int n);
void print_screen_A_b_x(double **a, double *b, double *x, int n);
void main()
{
int i, j, *ptr, n;
int violation_counter, answer;
int violation_rows[MAX_DIM];
double sum;
double **a;
double *b, *x;
FILE *input_Ptr; //pointer to input file
//Open the input file
input_Ptr = fopen ( "my_input.txt", "r" );
if (input_Ptr == NULL) {
puts("\nInput file was not opened succesfully.\n");
exit(-1);
}
//read size of the problem
fscanf(input_Ptr, "%d", &n);
//dynamic memory allocation
a = (double **) malloc (n * sizeof(double *));
for (i = 0; i < n; i++) {
a[i] = (double *) malloc (n * sizeof(double));
}
b = (double *) malloc (n * sizeof(double));
x = (double *) malloc (n * sizeof(double));
/* read in data */
//n = MAX_DIM + 1;
//while (n > MAX_DIM) {
//fscanf(input_Ptr, "%d", &n);
//}
printf("\n");
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
fscanf(input_Ptr, "%lf", &a[i][j]);
}
for (i = 0; i < n; i++) {
fscanf(input_Ptr, "%lf", &b[i]);
}
for (i = 0; i < n; i++) {
fscanf(input_Ptr, "%lf", &x[i]);
}
printf("\n");
}
fclose(input_Ptr);
print_screen_A_b_x(a, b, x, n);
puts("Solution vector:");
for (i = 0; i < n; i++) {
printf("x[%d] = %10.5f \n", i, x[i]);
//free memory
for (i = 0; i < n; i++) {
free(a[i]);
}
free(a);
free(b);
free(x);
return 0;
}
/* test the convergence criterion */
violation_counter = 0;
for (i = 0; i < n; i++) {
sum = 0.0;
for (j = 0; j < n; j++)
if (i != j)
sum = sum + fabs(a[i][j]);
if (fabs(a[i][i]) < sum) {
violation_rows[violation_counter] = i;
violation_counter = violation_counter + 1;
}
if (a[i][i] == 0.0) {
printf("Found diagonal element equal to zero;
rearrange equations; exiting ...\n");
exit(0);
}
}
if (violation_counter > 0) {
printf("The Gauss-Seidel convergence criterion is violated in %d rows out of %d\n", violation_counter, n);
printf("Specifically, it was violated in rows:\n");
for (i = 0; i < violation_counter; i++)
printf("%d ", violation_rows[i]);
printf("\n");
printf("Enter 1 if you want to continue; any other number to abort : ");
scanf("%d", &answer);
if (answer != 1)
exit(1);
printf("Check results carefully\n\n");
}
/* initialize the solution vector -- initial guesses */
for (i = 0; i < n; i++) {
printf("Enter an initial guess for x[%d] of the solution vector : ", i);
scanf("%lf", &x[i]);
}
/* solve the system */
gauss_seidel(a, b, x, n);
/* output solution */
for (i = 0; i < n; i++)
printf("x[%d]=%f\n", i, x[i]);
printf("\n");
}
/* function to solve a system using Gauss-Seidel */
void gauss_seidel(double **a, double *b, double *x, int n)
{
double maxerror = 1.0e-7;
double iteration_error;
double e, sum, temp;
int i, j;
while (maxerror > 1.e-6) {
iteration_error = 0.0;
for (i = 0; i < n; i++) {
sum = 0.0;
for (j = 0; j < n; j++) {
if (i != j)
sum = sum + (a[i][j] * x[j]);
}
}
temp = (a[i][n] - sum) / a[i][i];
e = fabs((temp - x[i]) / x[i]);
x[i] = temp;
if (e > iteration_error)
iteration_error = e;
}
maxerror = iteration_error;
}
void print_screen_A_b_x(double **a, double *b, double *x, int n)
{
int i, j;
printf("\n Coefficient matrix:\n");
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
printf("%10.2f", a[i][j]);
}
printf("\n");
}
printf("\n Right hand side vector:\n");
for (i = 0; i < n; i++) {
printf("%10.2f \n", b[i]);
}
printf("\n Initial guess:\n");
for (i = 0; i < n; i++) {
printf("%10.5f \n", x[i]);
}
return;
}
Add \r\n in your post loop if statement.
You show us:
void gauss_seidel(double a[][MAX_DIM], double b[], double x[], int n)
and:
void print_screen_A_b_x(double **a, double *b, double *x, int n)
You cannot pass the same object as the first argument to both functions; they are (radically) different types, even though you use double subscripts with both. Since you've not shown how the matrix is actually defined, there isn't anything more we can do to help you.
Your compiler should be shrieking at you about one (or both) calls. Heed your compiler. It knows more about C than you do at the moment. It won't object unless there's a serious problem. If by some mischance your compiler was not complaining, then you need to either turn on compilation warnings (and work with C11, or perhaps C99, as the version of the standard — definitely not C90) or get a better compiler.
Analysis of one update
One version of the updated code in the question has input code like this:
// read size of the problem
fscanf(input_Ptr, "%d", &n);
// dynamic memory allocation
a = (double **)malloc(n * sizeof(double *));
for (i = 0; i < n; i++)
{
a[i] = (double *)malloc(n * sizeof(double));
}
b = (double *)malloc(n * sizeof(double));
x = (double *)malloc(n * sizeof(double));
printf("\n");
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
fscanf(input_Ptr, "%lf", &a[i][j]);
}
for (i = 0; i < n; i++)
{
fscanf(input_Ptr, "%lf", &b[i]);
}
for (i = 0; i < n; i++)
{
fscanf(input_Ptr, "%lf", &x[i]);
}
printf("\n");
}
The good news is that except for the absence of error checking, the memory allocation looks OK.
The bad news is that under most plausible inferences of what the data looks like, the main input loops are completely wrong. Assuming the value in n is N, the first iteration of the input loop reads N values into a[0] (which is fine), and then reads N values into b (which is fine as far as it goes), and then reads N values into x (which is also fine as far as it goes). The value of i is now n; when it is incremented by the outer loop, i is bigger than n, so the outer loop terminates, but you've only read one row of the main matrix.
Most likely, you should be using:
// read size of the problem
if (fscanf(input_Ptr, "%d", &n) != 1)
…report error and skedaddle…
// Check n for plausibility
if (n < 1 || n > 1000)
…report implausibility and skedaddle…
// dynamic memory allocation
…as before, except you should error check all the allocations…
printf("\n");
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
if (fscanf(input_Ptr, "%lf", &a[i][j]) != 1)
…report error and skedaddle…
}
}
for (i = 0; i < n; i++)
{
if (fscanf(input_Ptr, "%lf", &b[i]) != 1)
…report error and skedaddle…
}
for (i = 0; i < n; i++)
{
if (fscanf(input_Ptr, "%lf", &x[i]) != 1)
…report error and skedaddle…
}
The fact that you don't check for errors means you don't know when things go wrong. You can't afford not to know when things go wrong, so you can't afford not to check for errors.
skedaddle |skɪˈdad(ə)l|
verb [ no obj. ] informal —
depart quickly or hurriedly; run away.

While passing 2d matrix using double pointer to print function it printing last element of matrix zero

Here is simple code just reading two matrix one is 3*3 dimensional and other is 3*1 dimensional. while printing first matrix A[3][3] the last element of matrix is printing zero in void printarray(double **A, int n ) function.
Below my code:
#include <stdio.h>
#include<malloc.h>
void printarray(double **A, int n );
void main(){
double **A;
int n = 3;
int row,col;
double *b;
A = (double **) malloc(n * sizeof(double**));
for (row = 1; row<= n; row++) {
A[row] = (double *) malloc(n * sizeof(double));
}
// Initialize each element.
for (row = 1; row<= n; row++) {
for (col = 1; col<= n; col++) {
printf("A[%d][%d]= %u \t",row,col,&A[row][col]);
scanf("%lf",&A[row][col]); // or whatever value you want
}
}
//print A
printf("\n...........array in main.................\n");
for (row = 1; row<= n; row++) {
for (col = 1; col<= n; col++) {
printf("A[%d][%d]=%u \t %lf",row,col,&A[row][col],A[row][col]);
printf("\n");
}
}
b = (double *) malloc(n * sizeof(double));
printf("\n enter the elemet of b \n"); // Initialize each element.
for (row = 1; row<= n; row++){
printf("address=%u \t",&b[row]);
printf("b[%d]=",row);
scanf("%lf",&b[row]);
printf("\n");
}
printarray((double **)A, n );
}// Print it
void printarray(double **A, int n ){
int i;
int j;
printf("\n.....print a.............\n");
for( j = 1; j <= n; j++ ){
for( i = 1; i <= n; i ++){
printf("A[%d][%d]= %u \t",j,i,&A[j][i]);
printf( "%lf ", A[j][i] );
}
printf( "\n" );
}
}
One the problem comes from the indexing of your array. Array indexing start at 0.
This means that in order to loop through your array, you need your for loop to start at 0 up to n-1:
for (int row=0; row<n;++row) {/*...*/}

c - warning: pointer argument of a function may be used uninitialized

#include <stdio.h>
#include <stdlib.h>
void multiplyMatrix (int **first, int **second, int **multiply);
int m, n, p, q, i, c, d, k, sum = 0;
int main()
{
int **first, **second, **multiply;
printf("Enter the number of rows and columns of first matrix\n");
scanf("%d%d", &m, &n);
first = (int **) malloc(m * sizeof(int *));
for(i = 0 ; i < n ; i++){
first[i]=(int *)malloc(m * sizeof(int *));
}
printf("Enter the elements of first matrix\n");
for (c = 0; c < m; c++)
for (d = 0; d < n; d++)
scanf("%d", &first[c][d]);
printf("Enter the number of rows and columns of second matrix\n");
scanf("%d%d", &p, &q);
second = (int **) malloc(p * sizeof(int *));
for(i = 0 ; i < q ; i++){
second[i]=(int *) malloc(p * sizeof(int *));
}
if (n != p)
printf("Matrices with entered orders can't be multiplied with each other.\n");
else
{
printf("Enter the elements of second matrix\n");
for (c = 0; c < p; c++)
for (d = 0; d < q; d++)
scanf("%d", &second[c][d]);
/*for (c = 0; c < m; c++) {
for (d = 0; d < q; d++) {
for (k = 0; k < p; k++) {
sum = sum + first[c][k]*second[k][d];
}
multiply[c][d] = sum;
sum = 0;
}
}*/
multiplyMatrix(first, second, multiply);
printf("Product of entered matrices:-\n");
for (c = 0; c < m; c++) {
for (d = 0; d < q; d++)
printf("%d\t", multiply[c][d]);
printf("\n");
}
}
return 0;
}
void multiplyMatrix (int **first, int **second, int **multiply)
{
for (c = 0; c < m; c++) {
for (d = 0; d < q; d++) {
for (k = 0; k < p; k++) {
sum = sum + first[c][k]*second[k][d];
}
multiply[c][d] = sum;
sum = 0;
}
}
}
The program i want to write should be like this: The program asks to the user to enter both the sizes and elements of 2 matrices (or you can call it 2d arrays). Then it will multiply those matrices and print the answer.
The problem i am getting: i used pointers and malloc functions to dynamically allocate the matrices. for the multiplication, i created a function called "multiplyMatrix" which i get a warning for one of the arguments of it in the decleration. here is the warning:
warning: 'multiply' may be used uninitialized in this function.
so there is some kind of a problem with initializing this argument. i feel like the answer is simple but at the same time i can't find the solution.
You have not allocated the memory to be used by the multiply matrix - hence it is being flagged as uninitialised.
You also need to review how you use your row and column values when allocating the first and second matrices, for example:
first = (int **) malloc(m * sizeof(int *));
for(i = 0 ; i < m ; i++){
first[i]=(int *)malloc(n * sizeof(int *));
}
(Incorporates comment made by wildplasser)
This will allow first to be accessed as first[row][col]
the variable multiply was declared in main(), however it is never set to point to anything. it needs to be created the same way as first and second, however it does not need to have its' values filled in.
Suggestions to improve your code:
Create a function to allocate memory for a matrix.
Create a function to read matrix data.
Create a function to deallocate memory of a matrix.
Avoid use of global variables. Pass the necessary arguments to a function.
Use those functions instead of duplicating code in main.
#include <stdio.h>
#include <stdlib.h>
int** createMatrix(int rows, int cols)
{
int i;
int** mat = malloc(sizeof(*mat)*rows);
for ( i = 0; i < rows; ++i )
mat[i] = malloc(sizeof(*mat[i])*cols);
return mat;
}
void readMatrix(int** mat, int rows, int cols)
{
int r;
int c;
for ( r = 0; r < rows; ++r )
for ( c = 0; c < cols; ++c )
scanf("%d", &mat[c][c]);
}
void deleteMatrix(int** mat, int rows)
{
int i;
for ( i = 0; i < rows; ++i )
free(mat[i]);
free(mat);
}
void multiplyMatrix (int **first, int **second, int **multiply,
int frows, int fcols, int scols)
{
int sum = 0;
int r;
int c;
int k;
for (r = 0; r < frows; r++) {
for (c = 0; c < scols; c++) {
sum = 0;
for (k = 0; k < fcols; k++) {
sum += first[r][k]*second[k][c];
}
multiply[r][c] = sum;
}
}
}
int main()
{
int m, n, p, q;
int r, c;
int **first, **second, **multiply;
printf("Enter the number of rows and columns of first matrix\n");
scanf("%d%d", &m, &n);
first = createMatrix(m, n);
printf("Enter the elements of first matrix\n");
readMatrix(first, m, n);
printf("Enter the number of rows and columns of second matrix\n");
scanf("%d%d", &p, &q);
if (n != p)
printf("Matrices with entered orders can't be multiplied with each other.\n");
else
{
second = createMatrix(p, q);
printf("Enter the elements of second matrix\n");
readMatrix(second, p, q);
multiply = createMatrix(m, q);
multiplyMatrix(first, second, multiply, m, n, q);
printf("Product of entered matrices:-\n");
for (r = 0; r < m; r++) {
for (c = 0; c < q; c++)
printf("%d\t", multiply[r][c]);
printf("\n");
}
deleteMatrix(multiply, m);
deleteMatrix(second, p);
}
deleteMatrix(first, m);
return 0;
}

Segmentation fault when the program tries write out the results

Greeting,
Somebody can find, why write out these program segmenation fault, when I take in the all inputs?
I don't find where is the problem, or where should be modify my code
And I would like to get the results?
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
void inMatrix(int n, double **matrix)
{
int j, i;
for (i = 0; i < n; i++)
{
for (j= 0; j < n; j++)
{
scanf("%lf", &matrix[i][j]);
}
}
}
void inVector(double *vektor, int n)
{
int k;
for (k = 0; k < n; k++)
{
scanf("%lf", &vektor[k]);
}
}
void outVector(double *vektor, int n)
{
int k;
for (k = 0; k < n; k++)
{
printf("%.8lf ", vektor[k]);
}
}
void lup(int n, double **A, double **b, int v)
{
int *Permutation = (int*)malloc(sizeof(int)*n);
int i,j;
int k;
double *max = (double*) malloc (sizeof(double)*n);
int m=0, p=0;
int tmp=0, tmp2=0;
int t=0, isSingular=0;
double largestElement=0.0;
double *helpVector = (double*) malloc (sizeof(double)*n);
double *helpVectorA = (double*) malloc (sizeof(double)*n);
double *helpVectorB = (double*) malloc (sizeof(double)*n);
// for(i=0;i<n;i++)
// {
// for(j=0;j<n;j++)
// {
// A[i][j]=D[i][j];
// }
// }
for(i=0; i<n; i++)
Permutation[i]=i;
for(m=0; m<n-1; m++)
{
for(i=m; i<n; i++)
{
max[i]=fabs(A[i][m]);
}
for(i=m; i<n; i++)
{
if(max[i]>largestElement)
{
largestElement=max[i];
p=i;
}
}
for(i=0; i<n; i++)
{
helpVectorA[i]=A[m][i];
helpVectorB[i]=A[p][i];
}
for(i=0; i<n; i++)
{
A[m][i]=helpVectorB[i];
A[p][i]=helpVectorA[i];
}
tmp=Permutation[m];
tmp2=Permutation[p];
Permutation[m]=tmp2;
Permutation[p]=tmp;
if(fabs(A[m][m])>0.00000000000000001)
{
for(i=m+1; i<n; i++)
{
A[i][m]=A[i][m]/A[m][m];
for(j=m+1; j<n; j++)
{
A[i][j]=A[i][j]-A[i][m]*A[m][j];
}
}
}
if(fabs(A[m][m])<0.00000000001)
{
printf("szingularis\n");
isSingular=1;
break;
}
for(i=0; i<n; i++) max[i]=-1;
largestElement=0.0;
p=m+1;
}
if(isSingular==0)
{
if(fabs(A[n-1][n-1])<0.00000000001)
{
printf("szingularis\n");
isSingular=1;
}
}
if(isSingular==0)
{
for(k=0; k<v;k++)
{
for(i=0; i<n; i++)
{
t=Permutation[i];
helpVector[i]=b[k][t];
}
for(i=0; i<n; i++)
{
b[i][k]=helpVector[i];
}
for(i=1; i<n; i++)
{
for(j=0; j<i; j++)
{
b[k][i]-=A[i][j]*b[k][j];
}
}
for(i=n-1; i>=0; i--)
{
for(j=i+1; j<n; j++)
{
b[k][i]-=A[i][j]*b[k][j];
}
b[k][i]=b[k][i]/A[i][i];
}
}
for(i=0; i<n; i++)
{
printf("%.8lf ", b[k][i]);
}
printf("\n");
}
}
int main()
{
int k, v,n;
int j;
double **A;
double **C;
// read dimension of matrix and value
scanf("%d", &n);
//matrix
A = (double **) calloc(n, sizeof ( double*));
// matrix to store the vectors
C = (double **) calloc(n, sizeof(double *));
while(n!=0)
{
for (k = 0; k < n; k++)
{
A[k] = (double *) calloc(n, sizeof ( double));
}
inMatrix(n, A);
scanf("%d", &v);
for(k=0;k<v;k++)
{
C[k] = (double *) calloc(n, sizeof ( double));
}
for(k=0; k<v;k++)
{
for(j=0;j<n;j++)
{
scanf("%lf", &C[k][j]);
}
}
//print out result
for(k=0;k<v;k++)
{
for(j=0;j<v;j++)
{
lup(n,A,C,v);
}
}
}
return 0;
}
In main, you have
/* ... */
C = (double **) calloc(n, sizeof(double *));
n elements
while(n!=0)
{
for (k = 0; k < n; k++)
{
A[k] = (double *) calloc(n, sizeof ( double));
}
inMatrix(n, A);
scanf("%d", &v);
for(k=0;k<v;k++)
v? Where did it come from? Do you perhaps mean n
Remember C was allocated space for n elements, even before v was set.
{
C[k] = (double *) calloc(n, sizeof ( double));
}
/* ... */
Your indentation and use of whitespace could improve a bit too.
In function lup, at line 157 (in the second "if(isSingular==0)" block) you wrote
printf("%.8lf ", b[k][i]);
this line is written in a for-loop iterating on i, while k has a leftover value from the last loop. The break condition of that loop was that k = v (the size of C, A.K.A "b" from the line of code above).
So, basically, you wrote:
printf("%.8lf", b[MaxIndexOfB+1][i];
And that's probably the stinker you're looking for.
Two notes. First, I've been .Net man for too long, so I might be missing something completely trivial here, In that case, sorry.
Second, when you want people to read your code (like when posting it for help), it's imperative to use more meaningful names, or something to improve code readability, but if you're not going to do this, at least avoid confusing stuff like passing variable named "C" into parameter named "b" if it's avoidable.
Checkout this tutorial. It teaches you how to find segfaults using GDB.

Resources