473,405 Members | 2,349 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

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

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
3 2192
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
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
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

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

Similar topics

11
by: fivelitermustang | last post by:
Actually, how would I go about allocating a four-dimensional dynamic array? I only know how to make two dimensional dynamic arrays: double **v; v = new double*; for (int i=0; i<C; i++) { v =...
4
by: kak3012 | last post by:
Hi, After my question today, I am not sure if this is the right group to ask but I have a simple question I guess. I have multi dimensional array called cover what I do is this iT GIVES AN...
14
by: Gattaca | last post by:
I would like to create a matrix of integers by allocating memory dynamically (malloc or calloc) because i and j are defined during execution of the program. I have got not problem to do this in...
7
by: check.checkta | last post by:
Hi, I'd like to implement a simple matrix class. I'd like to overload operator so that it returns as a vector (either the stl vector or some other Vector class of my own). The reason I want...
20
by: Frank-O | last post by:
Hi , Recently I have been commited to the task of "translating" some complex statistical algorithms from Matlab to C++. The goal is to be three times as fast as matlab ( the latest) . I've...
14
by: Paul McGuire | last post by:
I've posted a simple Matrix class on my website as a small-footprint package for doing basic calculations on matrices up to about 10x10 in size (no theoretical limit, but performance on inverse is...
3
by: tariq103 | last post by:
How would i compute the mean value of a matrix from a txt. file? help
20
by: Neclepsio | last post by:
Hi everyone. I've made a class Matrix, which contains a pointer to the data and some methods which all return a copy of the matrix modified in some way. The program works quite well for small...
1
by: xHolyWrath | last post by:
Hi everyone! I'm new to this forum so any advise will be very much appreciated. I'm currently creating a C program that would read a text file full of integer values that represents pixel spectrum...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.