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

I am creating a code that use the gauss-seidel method. I need help fixing my err

ID: 3862823 • Letter: I

Question

I am creating a code that use the gauss-seidel method. I need help fixing my errors. This is being done in C.

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#define DEBUG 0
#define MAX_ITER 1000

int i, j, n = 126, iter, lambda, ea;
double *bv, *xsol, *xprev, *Am, dummy, sum, sentinel;
//float *dummy, *sum, *sentinel;

double Gseid(Am, bv, n, xsol){

for(i=0; i<n ; i++) {
dummy = Am[i][i];

for(j=0; j<n ; j++){
Am[i][j] = Am[i][j]/dummy;
}
bv[i] = bv[i]/dummy;
}
for(i=0; i<n ; i++){
sum = bv[i];
for(j=0; j<n ; j++){
if (i!= j) {
sum = sum - Am[i][j]*xsol[j];
}
xsol[i] = sum;
}
}
iter = 1;
sentinel = 1;

for(i=0; i<n ; i++) {
xprev = xsol[i];
sum = bv[i];
for(j=0; j<n ; j++){
if (i!=j){
sum = sum - Am[i][j]*xsol[j];
}
xsol[i]= lambda*sum+(1.-lambda)*xprev[i];
if (sentinel = 1 && xsol[i]!=0){
ea+=((xsol[i]-xprev)/xsol[i])*100;
if (ea>es) {
sentinel = 0;
}
}
iter = iter + 1;
}
}
}

int main() {
FILE *BinInp, *TxtOut;

/* Input Code: Reads bv, Am in binary form from CGrad.bin */
if ( (BinInp = fopen("CGrad.bin","r")) == NULL ) {
fprintf(stderr, "Cannot open matrix binary file INPUT... exiting ");
exit(-1);
}

bv = malloc (n*sizeof(double));
if (!fread(bv, sizeof(double), n, BinInp)) {
fprintf(stderr, "Cannot read bv vector... exiting ");
exit(1);
} else {
if (DEBUG) {
for (i = 0; i < n; i++) {
printf ("Read bv(%03d) = %14.8f ", i+1, bv[i]);
}
}
}

Am = malloc (n*sizeof(double*));
Am[0] = malloc (n*n*sizeof(double));
for (i = 1; i < n; ++i) {
Am[i] = Am[0] + i*n;
Am[i][0] = Am[0][i];
}
for (i = 0; i < n; i++) {
if (!fread(Am[i], sizeof(double), n, BinInp)) {
fprintf(stderr, "Cannot read row Am[%03d] of matrix... exiting ", i+1);
exit(1);
} else {
if (DEBUG) {
printf ("Row %03d : ", i+1);
for (j = 0; j < n; j++) {
printf ("%8.4f ", Am[i][j]);
}
printf (" END ");
}
}
}

if (fclose(BinInp) == EOF) {
fprintf(stderr, "Cannot close matrix binary file INPUT... exiting ");
exit(-1);
}

xsol = malloc (n*sizeof(double));
xprev = malloc (n*sizeof(double));
for (i = 0; i < n; i++) {
xsol[i] = 0;
xprev[i] = 0;
}

// PERFORM ITERATIVE SOLUTION OF MATRIX SYSTEM HERE USING JACOBI OR GAUSS-SEIDEL METHODS.
// RETURN SOLUTION VECTOR IN xsol; THE CODE BELOW WILL WRITE IT TO A BINARY FILE.
// IF NEEDED, USE xprev VECTOR TO STORE PREVIOUS SOLUTION. FEEL FREE TO DEFINE OTHER VARIABLES
// AS REQUIRED FOR ITERATIVE SOULTION TO MATRIX SYSTEM.

xsol = Gseid ( Am, bv, n, xsol)


/* Output Code: Writes xsol in text form to Xsol.txt */

if( (TxtOut = fopen("Xsol.txt", "w")) == NULL ) {
fprintf(stderr, "Cannot open matrix text file OUTPUT... exiting ");
exit(-1);
}
for (i = 0; i < n; i++) {
fprintf (TxtOut, "%18.14e ", xsol[i]);
}

if (fclose(TxtOut) == EOF) {
fprintf(stderr, "Cannot close matrix text file INPUT... exiting ");
exit(-1);
}

return (0);
}

int i, j, n 126 iter lambda ea. double bv *x xpre *Arm, dummy sum, sentinel xsol float *dummy, *sum, Sentinel double Geseid (Arm bv, n, xsol) for (i o; i

Explanation / Answer

I can Am is not declared as array type. In function main es has not been declared. There is incompatible type conversin from double to void.Hence problem is that Am is not a pointer or array and hence you cannot use [] on it. There are few semicolon missing.