I think my program is crapping out because of faulty memory allocation.
I want my code to read matrix.dat and store the numbers in a 4x4 matrix using pointer ........... Then I would like to print the matrix line by line but instead of the correct numbers, I get a long string of random numbers.
This is my overall code:
Code:
void prob2(void)
{
FILE *Inf_1;
double **A=0, *b=0;
int row=0;
char text[81];
int k=0,t=0;
b = (double *)malloc((size_t)32);
A = (double **)calloc(16, sizeof(double *));
Inf_1 = fopen("matrix.dat", "r");
printf(" Put your solution for problem 2 here:\n");
//By using a pointer to pointer **A and the function calloc()
//allocate the memory for the 4x4 matrix A[][]
//By using a pointer *b and the function malloc() allocate the memory
//for the 4-dimensional vector b[]
if(b==NULL)
{
printf("(main) malloc failed in double *b\n");
exit(1);
}
if(A==NULL)
{
printf("(main) calloc failed in double **a\n");
exit(2);
}
for(row=0;row<16;row++)
{
A[row] = (double *)calloc(16, sizeof(double *));
if(A[row]==NULL)
{
printf("(main) calloc failed in double *A[]\n");
exit(4);
}
}
//Read the components of A and b from the given input file matrix.dat.
//The last line of the input file contains the components of b. The rows of
//matrix A are the first four lines of the input file.
if ((Inf_1 == NULL))
{
printf("Error opening the file\n");
exit(1);
}
while(fgets(text,81,Inf_1)!=NULL)
{
if (k < 5) {
sscanf(text, "%lf %lf %lf %lf", &A[t][0], &A[t][1], &A[t][2], &A[t][3]);
k++;
t++;
}
else
sscanf(text, "%lf %lf %lf %lf", b, b+1, b+2, b+3);
}
//Print the components of A[][] row by row, and the components of b[].
printf("\n");
for (k=0;k<4;k++) {
printf("a[%d][] = %lf %lf %lf %lf\n",k+1,&A[k][0],&A[k][1],&A[k][2],&A[k][3]);
}
}
I'm trying to isolate my problem and I would like others to check over some key components.
1. True or false: This block of code reads file matrix.dat line by line (there are 5 lines total; every line has five numbers) and stores the numbers in a 4x4 matrix in A.
The else statement reads the last line on matrix.dat and stores the 4 numbers using pointer b.
Code:
while(fgets(text,81,Inf_1)!=NULL)
{
if (k < 5) {
sscanf(text, "%lf %lf %lf %lf", &A[t][0], &A[t][1], &A[t][2], &A[t][3]);
k++;
t++;
}
else
sscanf(text, "%lf %lf %lf %lf", b, b+1, b+2, b+3);
}
2. True/false: This loop of 16 allocates some memory for each row in my 4x4 matrix.
Code:
for(row=0;row<16;row++)
{
A[row] = (double *)calloc(16, sizeof(double *));
if(A[row]==NULL)
{
printf("(main) calloc failed in double *A[]\n");
exit(4);
}
}
3. True/false: This prints the numbers on matrix.dat.
Code:
for (k=0;k<4;k++) {
printf("a[%d][] = %lf %lf %lf %lf\n",k+1,&A[k][0],&A[k][1],&A[k][2],&A[k][3]);
}
This is matrix.dat by the way:
Code:
-1.0 1.0 -4.0 -0.5
2.0 1.5 3.0 2.1
-3.1 0.7 -2.5 4.2
1.4 0.3 2.4 -1.9
0.0 1.2 -3.0 -0.5
Anyone know why my output is:
a[1][] = 0.000000 0.000000 -51371471092907161178215517680210201070883690439141 41531451094331347592107749648920245017535748877081 62688667630716613389078816118684482410273456360890 67293804625029028204093572680075524848238599093026 66019186939209269159188445012222149148388435131742 64331967577566243348855100186528081647614005590664 15104.000000 0.000000
a[2][] = 0.000000 0.000000 -51371471092907161178215517680210201070883690439141 41531451094331347592107749648920245017535748877081 62688667630716613389078816118684482410273456360890 67293804625029028204093572680075524848238599093026 66019186939209269159188445012222149148388435131742 64331967577566243348855100186528081647614005590664 15104.000000 0.000000
a[3][] = 0.000000 0.000000 -51371471092907161178215517680210201070883690439141 41531451094331347592107749648920245017535748877081 62688667630716613389078816118684482410273456360890 67293804625029028204093572680075524848238599093026 66019186939209269159188445012222149148388435131742 64331967577566243348855100186528081647614005590664 15104.000000 0.000000
a[4][] = 0.000000 0.000000 -51371471092907161178215517680210201070883690439141 41531451094331347592107749648920245017535748877081 62688667630716613389078816118684482410273456360890 67293804625029028204093572680075524848238599093026 66019186939209269159188445012222149148388435131742 64331967577566243348855100186528081647614005590664 15104.000000 0.000000