// Solves Jacobi Computation // // Given an array A, iteratively replace its // elements with the average of their four nearest // neighbors, until the largest change between two // consecutive iterations is less than delta. // // Variable size version // // Copyright by mpC team, 1999 // // http://www.ispras.ru/~cbr // mpc@ispras.ru #define TYPE double #define DELTA 0.0001 #define PREC 0.01 int reps; int N; void jacobi() { TYPE err; TYPE AA[N+2][N+2]; TYPE T[N][N]; reps=0; AA[]=0; AA[N+1][1:N]=1.0; do { reps++; T[]=0.25*( AA[0:N-1][1:N]+ AA[2:N+1][1:N]+ AA[1:N][0:N-1]+ AA[1:N][2:N+1] ); err=[+][+]((AA[1:N][1:N]-T[])*(AA[1:N][1:N]-T[])); AA[1:N][1:N]=T[]; } while(err>DELTA); } void report() { printf("\nJacobi scheme used %d iterations.\n", reps); } main() { printf("Enter matrix size (N): "); scanf("%d",&N); jacobi(); report(); }