Creating a file

Hello,

I am trying to using a matrix that is generated with the code to create a file. I am really looking for guidance on this. When I run the source code in Windows Visual C++ 2010, the file does not get generated. Any guidance would be very much appreciated. Thank you!
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
60
61
62
63
64
65
66
#include <stdio.h>
#include <math.h>
#define PI 3.14159265359
#define mu 0.03
#define dx 0.001
#define dt 0.001
#define SIZEX  4000
#define SIZET 10000
void FILEU(double mtx[SIZEX][SIZET]);

int main()     /*fxn main begins program execution*/
{
	double x[SIZEX];
	x[0]=0;
	double a=0.0;
	int b = SIZEX;
	for (int i=1; i<b; i++) /*define array for space points*/
	{
		x[i]=(1+a)/1000; a++;
	}
	
	double t[SIZET];
	t[0]=0;
	double c=0.0;
	int d = SIZET;
	for (int i=1; i<d; i++) /*define array for time points*/
	{
		t[i]=(1+c)/1000; c++;
	}
	
	double u[SIZEX][SIZET];
		for (int n=0; n<d; n++) /* define BC*/
		{
			u[0][n]=1; u[4000][n]=-1;
		}
		for (int i=1; i<b; i++)  /*define IC*/
			
		{
			u[i][0]=1-(0.5*x[i])+sin((0.25*PI*x[i]));
		}
		for(int n=0; n<d; n++)
		{
			for (int i=1; i<b;i++)
			{
				u[i][n+1] = ((sin(u[i][n])/(2*dx) + mu/(dx*dx))*u[i+1][n] + (1+(1/dt)-((2*mu)/(dx*dx)))*u[i][n] + (sin(u[i][n])/(2*dx) + mu/(dx*dx))*u[i-1][n] - cos(0.25*PI*x[i]))*(-dt);
			}
		}
	FILEU(u);		
	return 0;
}

void FILEU(double mtx[SIZEX][SIZET])
{
	int g=0; int h=0;
	FILE *file1;
	file1=fopen("Data1.txt","w");
	for (h=0; h<SIZET; h+= 1000)
	{
		for(g=0; g<SIZEX; g+= 400)
		{
			fprintf(file1, "%f\t", mtx[g][h]);
		}
		fprintf(file1, "\n");
	}
	fclose(file1);
}
Last edited on
I am not getting the file output. This is the main problem. Does anyone spot anything in the code that might prevent this? Thank you!
You have written beyond the end of the array in line 34.
u[][] has 4000 elements in its first index, so they run from 0 to 3999.


Some comments on your coding:
- don't use tab for indenting; it is different in every editor and is a right pain to sort out;
- don't write huge amounts of code without compiling and running to check the intermediate results; it is very difficult to find run-time errors in large amounts of code and this type of error would have been found if you compiled and ran the code after every loop was put in (I had to remove the lot and put them back in gradually).
- overrunning array bounds is a VERY common problem in C++ (partly because it won't stop your code compiling and partly because your code will manage to keep running, so you never realise there is a bug); be on your guard!



- don't use tab for indenting; it is different in every editor and is a right pain to sort out;

I will have to disagree with this; it is very very common therefore it is a bad idea to say that.
Your program compiles, but I get a stack overflow when trying to load your program.
Your arrays (lines 13,22,31) are too large for the stack.
Use the heap instead.
@AbstractionAnon,

I have never used heap before. How would I use this in the code above?
As AbstractionAnon points out (indirectly) you are storing a monumental size array here
(4000 x 10000 elements).

From your physical problem (1-d heat conduction or something like it) I don't see the need to store every time level. You probably only need to store time level n (as u, say) and time level n+1 (as u_next say) at most. Your two arrays would then only have 4000 x 2 elements in total (and your output file would also be a sensible size). At the end of a timestep you can update u to hold the values of u_next.

You are also:
- writing beyond the end of the array in line 34 (right boundary condition)
- using data beyond the ends of the array in line 45 (given the limits of your for loops)
- using uninitialised data from u in line 45 (though this may or may not have defaulted to 0)

Your discretisation of derivatives in line 45 also has sign errors in, but that is a maths problem, not C++.
Last edited on
Line 41
for(int n=0; n<d; n++)
also causes buffer overrun at line 45 u[i][n+1] =
element n+1 will be outside the array when n == d-1


There's a separate but very similar problem at line 43/45
 
for (int i=1; i<b;i++)

u[i+1][n] is outside the array when i == b-1

I have never used heap before. How would I use this in the code above?

In C++ there are several different options. However this appears to be written in C, hence the malloc() or calloc() functions would be used.
http://www.cplusplus.com/reference/cstdlib/malloc/

Instead of
 
    double u[SIZEX][SIZET];

you could try using an array of pointers
 
    double * u[SIZEX];

Each of those pointers needs to be allocated a corresponding block of memory to store the data.
1
2
3
4
    for (int i=0; i<SIZEX; ++i)
    {
        u[i] = malloc (SIZET * sizeof(double));
    }



When finished using the memory, it should be freed (just before return 0; in main().
1
2
3
4
    for (int i=0; i<SIZEX; ++i)
    {
        free (u[i]);
    }  



If you do this, the function header will need to be changed from
 
void FILEU(double mtx[SIZEX][SIZET]);
to
 
void FILEU(double * mtx[SIZEX])


I think that's ok - I don't use the C language much, so there may be something I got wrong.
Last edited on
Topic archived. No new replies allowed.