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!
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!
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.
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++.