Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

NO UNCOMPLETED ANSWERS! From the homework directory, copy the data file: matrix.

ID: 3633826 • Letter: N

Question

NO UNCOMPLETED ANSWERS!


From the homework directory, copy the data file:

matrix.dat
------------------------------------------------------------------
/*martix.dat*/
-1.00 1.00 -4.00 -0.50
2.00 1.50 3.00 2.10
-3.10 0.70 -2.50 4.20
1.40 0.30 2.40 -1.90
0.000 1.200 -3.000 -0.500

~
--------------------------------------------------------------------
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[]. 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. Print the components
of A[][] row by row, and the components of b[]. Using the function gauss()
given in the lecture notes week9.txt, solve the system of linear algebraic
equations A[][]*x[]=b[]. Print the components of the solution vector x[].
Calculate and print the length of this vector. Free the allocated memory.


................................................................

Your output should look like this:


a[1][]= -1.00 1.00 -4.00 -0.50
a[2][]= 2.00 1.50 3.00 2.10
a[3][]= -3.10 0.70 -2.50 4.20
a[4][]= 1.40 0.30 2.40 -1.90

b[]={ 0.000, 1.200, -3.000, -0.500 }

x[]={ ....., ......, ......, ..... }

The length of x[] is ......

Explanation / Answer

#include #include /* function prototypes */ void prob1(void); void prob2(void); void gauss(int n, double **k, double *rhs); /* add here additional function prototypes as needed */ main() { int menu; printf("Enter the function number to execute (1-2):"); scanf("%d", &menu); switch(menu){ case 1: prob1(); break; case 2: prob2(); break; default: printf("prob%d() does not exist. ", menu); } exit(0); } void prob2(void) { FILE *Inf_1; double **A, *b; int row; char text[81]; int k=0,t=0; b = (double *)malloc((size_t)3); A = (double **)calloc(16, sizeof(double *)); Inf_1 = fopen("matrix.dat", "r"); printf(" Put your solution for problem 2 here: "); //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 "); exit(1); } if(A==NULL) { printf("(main) calloc failed in double **a "); exit(2); } //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 "); exit(1); } while(fgets(text,81,Inf_1)!=NULL) { if (k < 5) { sscanf(text, "%lf %lf %lf %lf", *A[t], *(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(" a[1][]= %lf %lf %lf %lf ", *A[0],*(A[0]+1),*(A[0]+2),*(A[0]+3)); printf("a[2][]= %lf %lf %lf %lf ", *(A[1]),*(A[1]+1),*(A[1]+2),*(A[1]+3)); printf("a[3][]= %lf %lf %lf %lf ", *(A[2]),*(A[2]+1),*(A[2]+2),*(A[2]+3)); printf("a[4][]= %lf %lf %lf %lf ", *(A[3]),*(A[3]+1),*(A[3]+2),*(A[3]+3)); }