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.
Related
I am trying to make an arrangement with dynamic memory, 3-dimensional, my code is as follows:
typedef unsigned char matrix;
matrix ***mat(int n, int b)
{
matrix ***temp = (matrix ***)malloc(n*sizeof(matrix**));
for(int i=0; i<n; i++)
{
temp[i] = (matrix **)malloc(b*sizeof(matrix *));
for(int j = 0; j < b; j++)
temp[i][j]= (matrix *)malloc(b*sizeof(matrix));
}
return temp;
}
int main()
{
matrix ***M2 = mat(3,2);
for(int i=0; i<3; i++)
{
for(int j=0; j<2; j++)
{
for(int k=0; k<2; k++)
{
printf(" %d", M2[i][j][k]);
}
printf("\n");
}
printf("\n");
}
return 0;
}
when I run the program I have a segment violation, someone can tell me what the error is, since I can not visualize
I guess in the most nested for loop (the j one) the variables are messed in declaration for(int j = 0; i < b; i++). Try j for all
Usually I will do this kind of thing in FORTRAN, personally I like write the algorithm involving high dimensional array in FORTRAN as a library, and do the flow control staff in C. While 3D is still easily manageable in C, you need really careful with the pointers, here is a working example, it's valgrind clean.
#include <stdio.h>
#include <stdlib.h>
float ***myarray(int l, int m, int n)
{
float **ptr = malloc(sizeof(float*)*(l+l*m));
float *data = malloc(sizeof(float)*l*m*n);
float **p1 = ptr, **p2 = ptr+l;
for(int i=0; i<l; i++) {
p1[i] = (float*)(p2+i*m);
for(int j=0; j<m; j++)
p2[i*m+j] = data+(i*m+j)*n;
}
return (float***)ptr;
}
void myfree(float ***a)
{
free(a[0][0]);
free(a);
}
int main()
{
float ***array = myarray(4,3,2);
for(int i=0; i<4; i++)
for(int j=0; j<3; j++)
for(int k=0; k<2; k++)
array[i][j][k] = i+j+k;
myfree(array);
return 0;
}
you must correct the j counter inside the nested for loop in mat function, this is the correct one:
for(int j = 0; j < b; j++)
Currently I am working on a code that computes the following equation with two matrices, X and Y, to return the value of matrix W.
W = (XT * X)^-1 * XT * Y
Input Matrix train:
4
10
3.000000,1.000000,1180.000000,1955.000000,221900.000000
3.000000,2.250000,2570.000000,1951.000000,538000.000000
2.000000,1.000000,770.000000,1933.000000,180000.000000
4.000000,3.000000,1960.000000,1965.000000,604000.000000
3.000000,2.000000,1680.000000,1987.000000,510000.000000
4.000000,4.500000,5420.000000,2001.000000,1230000.000000
3.000000,2.250000,1715.000000,1995.000000,257500.000000
3.000000,1.500000,1060.000000,1963.000000,291850.000000
3.000000,1.000000,1780.000000,1960.000000,229500.000000
3.000000,2.500000,1890.000000,2003.000000,323000.000000
Input Matrix test:
3
3.000000,2.500000,3560.000000,1965.000000
2.000000,1.000000,1160.000000,1942.000000
3.000000,1.000000,1430.000000,1927.000000
Result Matrix:
716559
194430
323391
My code returns the proper values for the testcases with the exception of matrices over the size of 1000. I know this is because the size is not dynamically allocated, but I am not sure what the best approach to doing this in my code would be:
#include <stdlib.h>
#include <stdio.h>
int main(int argc, char* argv[]){
if(argc < 3){
printf("error.");
return 0;
}
FILE *fptrain = fopen(argv[1], "r");
if(fptrain == NULL)
{
printf("error.");
return 0;
}
int row, col, i, j;
fscanf(fptrain, "%d", &col);
col = col+1;
fscanf(fptrain, "%d", &row);
char ch;
//creates the original X and Y matrix
double trainX[row][col];
double trainY[row][1];
for(i=0; i<row; i++)
{
trainX[i][0] = 1.000000;
for(j=1; j<col; j++)
{
fscanf(fptrain, "%lf%c", &trainX[i][j], &ch);
}
fscanf(fptrain, "%lf%c", &trainY[i][0], &ch);
}
//creates the X transposed matrix
double trainXtrans[col][row];
for(i=0; i<row; i++)
{
for(j=0; j<col; j++)
{
trainXtrans[j][i] = trainX[i][j];
}
}
//multiplies X and X transposed
double trainXtemp[row][row];
int s;
double num=0;
for(i=0; i<col; i++)
{
for(j=0; j<col; j++)
{
for(s=0; s<row; s++)
{
num = num + trainX[s][j]*trainXtrans[i][s];
}
trainXtemp[i][j] = num;
num = 0;
}
}
//finds the identity matrix of X times X transposed
double trainXinden[col][col*2];
for(i=0; i<col; i++)
{
for(j=0; j<col; j++)
{
trainXinden[i][j] = trainXtemp[i][j];
}
for(j=col; j<col*2; j++)
{
if(j==i+col)
{
trainXinden[i][j] = 1.000000;
}
else{
trainXinden[i][j] = 0.000000;
}
}
}
//finds the inverse of X times X transposed through Gauss Jordan Elimination
int k;
double divscalar;
for(i=0; i<col; i++)
{
divscalar = trainXinden[i][i];
for(j=0; j<col*2; j++)
{
if(trainXinden[i][j] != 0)
{
trainXinden[i][j] = trainXinden[i][j]/divscalar;
}
}
for(k=0; k<col; k++)
{
if(i!=k)
{
double subscalar = trainXinden[k][i];
for(j=0; j<col*2; j++)
{
trainXinden[k][j] = trainXinden[k][j] - subscalar*trainXinden[i][j];
}
}
}
}
double trainXinverse[row][row];
for(i=0; i<row; i++)
{
for(j=0; j<col; j++)
{
trainXinverse[i][j] = trainXinden[i][j+col];
}
}
double trainXinvXt[col][row];
for(i=0; i<col; i++)
{
for(j=0; j<row; j++)
{
for(s=0; s<col; s++)
{
num = num + trainXinverse[i][s]*trainXtrans[s][j];
}
trainXinvXt[i][j] = num;
num = 0;
}
}
//multiples (trainXinvXt) by Y
double weight[row][1];
for(i=0; i<col; i++)
{
for(s=0; s<row; s++)
{
weight[i][0] += trainXinvXt[i][s]*trainY[s][0];
}
}
FILE *fptest = fopen(argv[2], "r");
if(fptest == NULL)
{
printf("error.");
return 0;
}
int testrows;
fscanf(fptest, "%d", &testrows);
//creates the test file matrix
double testM[testrows][col];
for(i=0; i<testrows; i++)
{
testM[i][0] = 1.000000;
for(j=1; j<col; j++)
{
fscanf(fptest, "%lf%c", &testM[i][j], &ch);
}
}
double prices[testrows][1];
for(i=0; i<testrows; i++)
{
for(s=0; s<col; s++)
{
num = num + testM[i][s]*weight[s][0];
}
prices[i][0] = num;
num = 0;
}
for(i=0; i<testrows; i++)
{
printf("%0.0lf", prices[i][0]);
printf("\n");
}
return 0;
}
When I use malloc on each matrix, for some reason it appears that it is not allowing me to create the augmented matrix or perform my gauss-jordan reduction, which is ruining my final answer.
The best approach to allocate memory to a multi-dimensional array (taking e.g. of 2D array, since you are using matrix in your program):
int(*matrix)[col] = malloc (sizeof(int[row][col]));
If you want to write a function for it then:
void* allocMatrix (int row, int col)
{
return malloc (sizeof(int[row][col]));
}
If you are not familiar with this approach of allocating memory dynamically to a multi-dimensional array then read this (very nicely explained by Lundin).
In your program, you are having the matrix of type double, so the allocMatrix() will be -
void* allocMatrix (int row, int col)
{
return malloc (sizeof(double[row][col]));
}
and in main(), you need to make following changes to create matrix dynamically -
double (*trainX)[col] = allocMatrix (row, col);
Do same changes for other matrices also and make sure to free() the dynamically allocated memory to matrices at appropriate places in your program.
Just for the knowledge purpose, this practice of allocating memory to the multi-dimensional array has been followed commonly, though it's not the best.
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.
with first function i fill 5x5 array with 0 (means that users aren't friends, or 1 that mean they are friends.
second function calculate friends of each user.
third function calculate and return common friends between users.
and i have problem with last fucntion, it have to calculate friends of each user and save it in 1D array, and then bubble short the array.
can anyone help me to find out what is wrong with the last function
friendship array is
01100
10111
11010
01100
01000
Code
#include <stdio.h>
#include <stdlib.h>
/* function declaration */
void loadMatrix (int **F, int size);
int findFriends (int **F, int size, int user);
int commonFriends (int **F, int size, int user1, int user2);
void sortUsers (int **F, int size, int *S);
int main()
{
int **matrixF; /* friendship array */
int *matrixS; /* sum of each user array */
int num_users; /* users number */
int i, j;
printf("Give number of users: ");
scanf("%d", &num_users);
/* allocate memory for array */
matrixF = (int **) malloc(num_users * sizeof(int *));
if (!matrixF)
{
printf("Memory allocation error!\n");
exit(1);
}
for (i = 0; i < num_users; ++i)
{
matrixF[i] = (int *) malloc(num_users * sizeof(int));
if (!matrixF[i])
{
printf("Memory allocation error!\n");
exit(1);
}
}
loadMatrix(matrixF, num_users);
for (i=0; i<num_users; ++i)
{
printf("Number of friends of user %d: %d\n", i, findFriends(matrixF, num_users, i));
}
/* common friends */
for (i=0; i<num_users; ++i)
{
for (j=0; j<i; ++j)
{
printf("Number of common friends of %d and %d: %d\n", i, j, commonFriends(matrixF, num_users, i, j));
}
}
matrixS = (int *)malloc(num_users * sizeof(int));
if (!matrixS)
{
printf("Memory allocation error!\n");
exit(1);
}
sortUsers(matrixF, num_users, matrixS);
/* bubble short */
for (i=0; i<num_users; ++i)
{
printf("%d friends.\n", matrixS[i]);
}
return 0;
}
void loadMatrix (int **F, int size)
{
int i, j;
for (i=0; i<size; i++)
for (j=0; j<size; j++)
{
do
{
printf(" user%d is friend with user%d: ", i, j);
scanf("%d", &F[i][j]);
if ((F[i][j]<0) || (F[i][j]>1 )) printf("H timi prepei einai 0 i 1 . ");
}
while ((F[i][j]<0) || (F[i][j]>1)); /*please enter 0 or 1*/
}
}
int findFriends (int **F, int size, int user) {
int j;
int sum=0;
for(j=0; j<size; j++)
{
sum+=F[user][j];
}
return (sum);
}
int commonFriends (int **F, int size, int user1, int user2) {
int j;
int counter=0;
for (j=0; j<size; j++)
{
if ((F[user1][j]==1)&&(F[user2][j]==1))
counter++;
}
return (counter);
system("pause");
}
void sortUsers (int **F, int size, int *S)
{
int i, j, temp;
int sum=0;
for(i=0; i<size; i++)
{
for(j=0; j<size; j++)
sum+=F[i][j];
}
S[i]=sum;
printf("%d\t", S[i]);
for(i=0; i<size; i++)
{
S[i]=sum;
}
for (i=1; i<size; i++)
{
for (j=0; j<size-1; j++)
{
if (S[j]>S[j+1])
{
temp = S[j];
S[j] = S[j+1];
S[j+1] = temp;
}
}
}
}
i just find out the answer
void sortUsers (int **F, int size, int *S) {
int i, j, temp, user;
int sum;
for (i=0; i<size; i++){
sum=0;
for (j=0; j<size; j++)
S[i]=(sum+=F[i][j]);
}
for (i=0; i<size; i++)
printf("%d\t", S[i]);
for (i=1; i<size; i++) {
for (j=0; j<size-1; j++) {
if (S[j]>S[j+1]) {
temp = S[j];
S[j] = S[j+1];
S[j+1] = temp;
}
}
}
system("pause");
}
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);