Gauss-seidel and ASCII

Pages: 12
I might still need some help with the rest of the assignment. So I'm not going to set this to solved yet, but I'm taking a break. my brain hurts and my stomach is empty
hey so the code was running in virtual studio, but I need it to run with g++ I took out the #include "stdafx.h"

I keep getting the error

os@os:~$ g++ gaussian.cpp
g++: error: gaussian.cpp: No such file or directory
g++: fatal error: no input files
compilation terminated.

Its saved on the desktop of a debian OS and I've double checked the upper lower case letters. i've renamed it a few times and tried running that.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include<iostream>
#include<cmath>

int main()
{
	double C[3], cL, i;
	double a[3][4] = { { 15, -3, -1, 3800 },
	{ -3, 18, -6, 1200 },
	{ -4, -3, 12, 2350 } };
	C[0] = a[0][3] / a[0][0];
	C[1] = a[1][3] / a[1][1];
	C[2] = a[2][3] / a[2][2];
	i = 1;
	cL = 1000;
	while (true)
	{
		if (i <= cL)
		{
			C[0] = (a[0][3] - C[1] * a[0][1] - C[2] * a[0][2]) / a[0][0];
			C[1] = (a[1][3] - C[0] * a[1][0] - C[2] * a[1][2]) / a[1][1];
			C[2] = (a[2][3] - C[0] * a[2][0] - C[1] * a[2][1]) / a[2][2];
			i = i + 1;
		}
		else
		{
			std::cout << C[0];
			std::cout << C[1];
			std::cout << C[2];
			break;
		}
	}
}

lol it decided to work
This is the next step I'm working on

After the function is called it will write the data to an ASCII file name results.dat in the following format:

any suggestions are helpful :)
roughly.

ofstream ofs;
ofs.open("targetfile.txt");

ofs << what goes into file; //works exactly like cout.

ofs.close();

you can see the file on the unix console quickly with
cat filename

I got the code to print an ASCII file with the correct answers, but I'm having difficulty getting it to read an ASCII file now


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include<iostream>
#include<cmath>
#include<cstdio>
#include<stdio.h>
#include<fstream>

using namespace std;

int main()
{
	double C[3], er, er0, a[3][4];
	FILE* fp;
	fp = fopen("datafile.dat", "r+");
	fscanf(fp, " %1f ", &a[3][4]);
	
	C[0] = a[0][3] / a[0][0];
	C[1] = a[1][3] / a[1][1];
	C[2] = a[2][3] / a[2][2];
	er=5e-10;
	er0=0;
	int n;

	while (n)
	{

			C[0] = (a[0][3] - C[1] * a[0][1] - C[2] * a[0][2]) / a[0][0];
			C[1] = (a[1][3] - C[0] * a[1][0] - C[2] * a[1][2]) / a[1][1];
			C[2] = (a[2][3] - C[0] * a[2][0] - C[1] * a[2][1]) / a[2][2];
			
			if ( ((C[1]-er0)/C[1])<er)
			{
				n=0;
			}
			er0=C[1];
	}


			FILE *fp1;
			fp1 = fopen("results.dat","w+");
			fprintf(fp1,"C[1] = %8.4f \n",C[0]);
			fprintf(fp1,"C[2] = %8.4f \n",C[1]);
			fprintf(fp1,"C[3] = %8.4f \n",C[2]);
}


the program runs without any errors but the answers are completly off
Last edited on
This is the code I made to write an ASCII file names datafile.dat

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <iostream>

#include <stdio.h>

int main ()

{

double a[3][4];

int i,j;

a[0][0]=15;

a[0][1]=-3;

a[0][2]=-1;

a[0][3]=3800;

a[1][0]=-3;

a[1][1]=18;

a[1][2]=-6;

a[1][3]=1200;

a[2][0]=-4;

a[2][1]=-3;

a[2][2]=12;

a[2][3]=2350;

FILE *fp;

fp = fopen("datafile.dat","w+");

for(i=0;i<3;i++)

{

for(j=0;j<4;j++)

{ fprintf(fp,"%.0f ",a[i][j]);

}

fprintf(fp,"\n");

}

fclose(fp);

return(0);

}


This code runs fine, I'm just uploading it as a reference for you.
Last edited on
I changed it to this code and I'm getting the same wrong answer.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include<iostream>
#include<cmath>
#include<cstdio>
#include<stdio.h>
#include<fstream>

using namespace std;

int main()
{
	double C[3], er, er0, a[3][4];
	FILE* fp;
	fp = fopen("datafile.dat", "r+");
	fscanf(fp, " %1f, %1f, %1f, %1f, %1f, %1f, %1f, %1f, %1f, %1f, %1f, %1f, ", &a[0][0], &a[0][1], &a[0][2], &a[0][3], &a[1][0], &a[1][1], &a[1][2], &a[1][3], &a[2][0], &a[2][1], &a[2][2], &a[2][3]);
	
	C[0] = a[0][3] / a[0][0];
	C[1] = a[1][3] / a[1][1];
	C[2] = a[2][3] / a[2][2];
	er=5e-10;
	er0=0;
	int n;

	while (n)
	{

			C[0] = (a[0][3] - C[1] * a[0][1] - C[2] * a[0][2]) / a[0][0];
			C[1] = (a[1][3] - C[0] * a[1][0] - C[2] * a[1][2]) / a[1][1];
			C[2] = (a[2][3] - C[0] * a[2][0] - C[1] * a[2][1]) / a[2][2];
			
			if ( ((C[1]-er0)/C[1])<er)
			{
				n=0;
			}
			er0=C[1];
	}


			FILE *fp1;
			fp1 = fopen("results.dat","w+");
			fprintf(fp1,"C[1] = %8.4f \n",C[0]);
			fprintf(fp1,"C[2] = %8.4f \n",C[1]);
			fprintf(fp1,"C[3] = %8.4f \n",C[2]);
}
This is my full code now and i have no idea why its not running. I'm not getting any errors. I think its doing an infinite loop.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include<iostream>
#include<cmath>
#include<cstdio>
#include<stdio.h>
#include<fstream>

using namespace std;

void GaussSeidel(double a[3][4])
{
	double C[3], er, er0;
	C[0] = a[0][3] / a[0][0];
	C[1] = a[1][3] / a[1][1];
	C[2] = a[2][3] / a[2][2];
	er=5e-20;
	er0=0;
	int n;
	while (n)
	{

			C[0] = (a[0][3] - C[1] * a[0][1] - C[2] * a[0][2]) / a[0][0];
			C[1] = (a[1][3] - C[0] * a[1][0] - C[2] * a[1][2]) / a[1][1];
			C[2] = (a[2][3] - C[0] * a[2][0] - C[1] * a[2][1]) / a[2][2];
			
			if ( ((C[1]-er0)/C[1])<er)
		{
				n=0;
		}
			er0=C[1];
	}
}
void GaussSeidel();
int main()
{
	int i,j;
	double a[3][4], C[3];
	FILE *fu;
	fu=fopen("datafile.dat","r+");

	if (fu == NULL)
	{
		cout << "file doesn't open\n";
	}
		for (i=0;i<3;i++)
		for (j=0;j<4;j++)
	{
		fscanf(fu," %1f ",&a[i][j]);
	}
GaussSeidel(a);
FILE *fp;
fp = fopen("results.dat","w+");
fscanf(fp," %1f ", &C[3]);
fprintf(fp,"C[1] = %8.4f \n",C[0]);
fprintf(fp,"C[2] = %8.4f \n",C[1]);
fprintf(fp,"C[3] = %8.4f \n",C[2]);
fclose (fu);
return 0;
}
btw this is due at midnight please help
Dr. Lang got us all worked up now hasnt he?
lol did you finish?
are you Dr. Lang?
nah, im here for resources
are you in CEI?
lmao no, who you doe?
Topic archived. No new replies allowed.
Pages: 12