By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,665 Members | 1,251 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,665 IT Pros & Developers. It's quick & easy.

Stuck on allocating a matrix from a file and reusing that to compute another

P: 6
Hi

I have been pulling my hair out trying to figure this out. Please help!!!

Here is my project description:

By using a pointer to pointers **A and **B and the function calloc()
allocate the memory for the 4x4 matrices A[][] and B[][].
By using the pointers *a and *b and the function malloc()
allocate the memory for the 4-dimensional vectors a[] and b[].
Read the components of a and A from the given input file matrix.dat
(which is in the public/final directory; you need to copy it into your directory).
The first line of the input file contains the components of a. The rows of
matrix A are the last four lines of the input file. Print the components
of a[] and A[][]. By calling the function pmatrix(A,B,a,b,n), n=4,
which you will construct, determine the components of the matrix B=1.5*A*A, and the
vector b=(A*a)+0.5*a. Print the components of the matrix B and the vector b in the main.
Free dinamically allocated memory.
.................................................. ..............................
Output should look like:


Vector a is:
a[] = -1.200 1.200 -2.000 -0.500
Matrix A is:
2.700 1.500 3.200 2.400
-3.200 0.700 -2.600 4.300
1.400 0.600 2.100 -1.800
1.500 1.700 -2.600 -0.700
Matrix B is:
..... ...... ...... ......
..... ...... ...... ......
..... ...... ...... ......
..... ...... ...... ......
Vector b is:
b[] = ..... ..... ..... .....


Expand|Select|Wrap|Line Numbers
  1. void prob2(void)
  2. {
  3. FILE *matrix;
  4. int k, z, c;
  5. double **A, **B, *a, *b;
  6.  
  7.  
  8. matrix=fopen("matrix.dat", "r");
  9. if(matrix == NULL){
  10.         printf("Error in file opening for *matrix.\n");
  11.         exit(1);
  12.         }
  13.  
  14.  
  15. A= (double **)calloc((size_t)4, sizeof(double *));
  16. B= (double **)calloc((size_t)4, sizeof(double *));
  17. a= (double *)malloc((size_t) 4 * sizeof(double));
  18. b= (double *)malloc((size_t) 4 * sizeof(double));
  19.  
  20. for(z=0; z<4; z++){
  21.         A[z] = (double *)calloc((size_t)4, sizeof(double *));
  22. }
  23. for(z=0; z<4; z++)
  24.         for(c=0; c<4;c++) A[z][c] = (double)z * (double)c;
  25. printf("\n");
  26. printf("Matrix A is:\n\n");
  27.  
  28.         for(row=0; row<4; row++){
  29.                 for(col=0; col<4; col++){
  30.                 fscanf(matrix, "%lf", A+col);
  31.                 }
  32.         for(col=0; col<4; col++)
  33.                 printf(" %7.3f", A[col]);
  34.         printf("\n");
  35.         }
  36.  
  37. printf("\nVector a is:\n\n");
  38.         for(row=0; row<5; row++){
  39.                 for(col=0; col<4; col++){
  40.                 fscanf(matrix, "%lf", A+col);
  41.                 }
  42.         }
  43. printf("a[] = ");
  44.         for(col=0; col<4; col++)
  45.                 printf(" %7.3f", A[col]);
  46.         printf("\n");
  47.  
  48. printf("\n");
  49. printf("Matrix B is: \n\n");
  50.  
  51. }
Dec 6 '07 #1
Share this Question
Share on Google+
3 Replies


P: 6
I can get the program to scan the file and print out the correct information but from there to the next part of computing B i am lost and cant seem to find how to multiply matrices etc
Dec 6 '07 #2

P: 6
My current problem is the fact taht i keep getting Segmentation Faults and its right when i try to execute pmatrix in void prob2().
Dec 6 '07 #3

P: 6
NEWLY UPDATED PROGRAM

Expand|Select|Wrap|Line Numbers
  1. void prob2(void)
  2. {
  3.  
  4.  
  5. FILE *matrix;
  6. int k, z, c;
  7. double **A,**B, *a, *b, a_norm;
  8.  
  9.  
  10. matrix=fopen("matrix.dat", "r");
  11. if(matrix == NULL){
  12.         printf("Error in file opening for *matrix.\n");
  13.         exit(1);
  14.         }
  15.  
  16.  
  17. A= (double **)calloc((size_t)4, sizeof(double *));
  18. a= (double *)malloc((size_t) 4 * sizeof(double));
  19. b= (double *)malloc((size_t) 4 * sizeof(double));
  20.  
  21.  
  22.  
  23. printf("Matrix A is:\n\n");
  24.  
  25. A = (double **)calloc((size_t)4, sizeof(double *));
  26.         for(row=0; row<4; row++){
  27.                 for(col=0; col<4; col++)
  28.                         fscanf(matrix, "%lf", A+col);
  29.                 for(col=0; col<4; col++) printf(" %7.2f", A[col]);
  30.         printf("\n");
  31.         }
  32.  
  33.  
  34.  
  35.  
  36.  
  37.  
  38. printf("\nVector a is:\n\n");
  39.         for(row=0; row<5; row++){
  40.                 for(col=0; col<4; col++){
  41.                 fscanf(matrix, "%lf", A+col);
  42.                 }
  43.         }
  44. printf("a[] = ");
  45.         for(col=0; col<4; col++)
  46.                 printf(" %7.3f", A[col]);
  47.         printf("\n");
  48.  
  49. printf("\n");
  50. printf("Matrix B is: \n\n");
  51.  
  52.  
  53.  
  54. pmatrix(A, B, a, b, n);
  55.  
  56.  
  57. }
  58.  
  59. void pmatrix(double **A, double **B, double *a, double *b, int n)
  60. {
  61. int k, row, col;
  62. double c[ROWA][COLB], dot_p;
  63.  
  64.  
  65. for(row=0; row<ROWA; row++){
  66.         for(k=0, dot_p=0.; k<COLA; k++){
  67.                 dot_p += A[row][k] * A[k][col];}
  68.         c[row][col] = dot_p;
  69. }
  70.         printf("c[%2d][]={", col);
  71.         for(row=0; row<ROWA; row++){
  72.                 printf("  %.2f", c[row][col]);
  73.                 if((row+1)%5 == 0) printf("\n");
  74. }
  75.  
  76.         printf("}\n");
  77.         return;
  78. }
Dec 6 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.