# Thread: Jacobi iteration! Have 90% of the code done need QUICK help please!

1. ## Jacobi iteration! Have 90% of the code done need QUICK help please!

I need the "//TO DO" part near the end, im very stuck on it... thanks in advance!

Code:
```#include <stdio.h>
#include <math.h>
#include <string.h>
#include<stdlib.h>

/*
Solving a system of linear equations using the Jacobi method.
This program reads in data of the form
3
4.0  0.3  0.5  1.0
2.0  4.0  0.8  2.0
1.0  0.6  9.0  3.0
where the first number N (in this case 3) indicates that the next
N lines will be an augmented matrix N x N+1
The N x N part of the augmented matrix is the matrix of coefficients S
of vector X, and the last column of the augmented matrix is vector T,
such that SX = T
This program uses the Jacobi method to iteratively solve for X.
*/

#define N 8
#define EPSILON 0.0001

void in(double s[][N], double t[], int * n);
void out2D(char * what, double s[][N], int n);
void out1D(char * what, double t[], int n);
void jacobi(double s[][N], double x[], double t[], int n);
void mult(double a[][N], double b[], double result[], int n) ;
char * check(char * name, double sx[], double t[], int n);
main()
{
char name[25] = "Jacobi" ;
double s[N][N], x[N], t[N], sx[N] ;
int i, j, n,q;
in(s, t, &n);
printf("-----------------------------------------\n") ;
printf("SOLVING S * X = T USING THE JACOBI METHOD\n") ;
printf("-----------------------------------------\n") ;
out2D("S", s, n);
/*
jacobi(s, x, t, n);
mult(s, x, sx, n);
out1D("Jacobi S * X", sx, n);
out1D("original T", t, n);
printf("%s\n", check(name, sx, t, n));*/
printf("\n\n\n");
}

/*
Reads a value *n which is the dimension of a two dimensional
square matrix, and then reads the n x n matrix s and the vector t
where the data have a shape as shown in the top comment.
*/

void in(double s[][N], double t[],int * n)
{
int i,j,both,row,col,row2;
double *matrixs, row3,col3;
FILE *f1;

f1=fopen("matrix20.txt", "r");
fscanf (f1, "%lf", &both);
n=&both;
row3=both;
col3=both;
row2=both+1;
row=both;
col=both;

matrixs = (double *) calloc (row*col, sizeof(double));

for (i=0; i<4; i++)
{
for (j=0; j<3; j++)
{
fscanf (f1, "%lf", &matrixs[(i*both)+j]);
s[i][j]=matrixs[j+(both*i)];
}
}

/*
for (j=1; j<row; j++)
{
fscanf (f1,"%lf", &t[both]);
}
*/
}

/*
Prints a two dimensional square matrix s which is n x n
preceded by the name stored in 'what'.  For example, for the
input shown above and the matrix called 's':
s :
4.000000  0.300000  0.500000
2.000000  4.000000  0.800000
1.000000  0.600000  9.000000
*/
void out2D(char * what, double s[][N], int n)
{
int i,j;

FILE *in1,*in2;
char name [10], letter;
in1 = fopen ("what.txt", "r");
fgets (name, sizeof(name), in1);
printf ("%s", name);
fclose (in1);

for (i=0; i<4; i++)
{
for (j=0; j<3; j++)
{
printf ("%7.3lf", s[i][j]);
}
printf ("\n");
}
}
void out1D(char * what, double t[], int n)
{

int i;
FILE *f1;
f1=fopen("matrix20.txt", "r");
for (i=3;i<12; i++)
{
if (i==3)
{
fscanf (f1, "%lf", &t[i-4]);
}
else
if (i==7)
{
fscanf (f1, "%lf", &t[i-7]);
}
else
if (i==11)
{
fscanf (f1, "%lf", &t[i-10]);
}
else
{
fscanf (f1, "%*lf");
}
}
}
/*
Runs the jacobi iterative solution to S X = T.
The matrix S is given by s and is n x n.
The vector X is given by x, which has n elements.
The vector T is given by t, which has n elements.
The iteration stops if all new elements differ by no more than
EPSILON from the old elements.
*/
void jacobi(double s[][N], double x[], double t[], int n)
{
int i, j, flag = 1, k;
double new_x[N], sum;
//                               TO DO
}
/*
Multiplies an n x n matrix A by a vector X, i.e., an n x 1 matrix.
The resulting vector is called result.
*/

void mult(double a[][N], double x[], double result[], int n)
{
{
int i, j, k, sum ;
for (i = 0 ; i < n ; i++) {
for (j = 0 ; j< n ; j++) {
sum = 0 ;
sum = sum + a[i][j] * x[i] ;
result[i] = sum ;
}
}
}
}
/*
Checks if two vectors are 'close', meaning that each pair of
corresponding elements differ by no more than EPSILON.
It returns a string "Jacobi check fails" if the vectors are not close
otherwise a string "Jacobi check is OK".
*/
char * check(char * name, double sx[], double t[], int n)
{
int i;
for(i = 0 ; i < n ; i++)
if(fabs(sx[i] - t[i]) > 10 * EPSILON)
return (strcat(name, " check fails"));
return (strcat(name, " check is OK"));
}```

2. You can't just steal some code from the net spit it here and expect somebody to do your homework mate, this is not a homework service.

3. me and a friend actually posted this about 4 days ago, got multiple people to help, and this is how far we got. We had a basic skeleton to start with and i cant get this final part. Go take a look.

4. First of all one says "a friend and I posted ....".

Secondly, there is no way in hell anyone here will waste time to help you unless you explain what your code does, what you need it to do and what you have tried to make it work.

If you don't know this by now, this is a good time to read the POSTING RULES.

5. yes a friend and i, i dont know her password and she is not with me so i had to make my own account, here is our original thread: http://cboard.cprogramming.com/c-pro...dim-array.html

Everything is explained there, sorry about not following the guidelines

6. You are really not helping yourself at all, expeditious responses like "Read there and figure it out" are not really helping you at this moment.

You were offered valuable advice on that thread from quzah who is one of the most knowledgeable people here and refused to do it. You (or your friend) killed the thread not him. Secondly, there is no evidence that you are not stealing that other person's code by claiming to have worked on it with him. If you really had been working on it, you would have no problems explaining what your problem is.

7. > Have 90% of the code done need QUICK help please!
Gotta love estimates like this.

Sure, you've done all the easy I/O, but implementing jacobi() is where the rubber meets the road.

Have you actually read something like this before hand?
Jacobi method - Wikipedia, the free encyclopedia

8. Part of the reason you need to post code is to show that you have made an effort to solve the problem yourself. However, of all the code you posted, most of it is unrelated to the problem at hand. The only bit that is relevant is:
Code:
```void jacobi(double s[][N], double x[], double t[], int n)
{
int i, j, flag = 1, k;
double new_x[N], sum;
//                               TO DO
}```
So what you're unfortunately really showing is that you have made practically no attempt to solve the problem yourself.
The algorithm as listed in wikipedia looks plenty good enough to get started with.