C/C++ C++, Laplace Equation in a 20*20 array

AI Thread Summary
The discussion focuses on solving the Laplace equation using an iterative method in a 20x20 array, where the code initially encounters an illegal operation. The user employs three arrays: one for values including boundary conditions and two for new values and differences. Key issues identified include incorrect array indexing and the need to redefine arrays V0 and diff to 20x20 to avoid out-of-bounds errors. After resolving these issues, the user inquires about formatting output for Excel, with suggestions to use CSV for better structure. The conversation highlights the importance of proper indexing and code organization for future usability.
SolStis
Messages
10
Reaction score
0
The code should solve laplace equation through an iterative technique until values change less than the specified tollerence, in this case maxdiff. I've used 3 arrays. one to store all values including initial and boundary conditions, and 2 more to store the new values and differences between the old values. The code then sums all values in the differences array, diff[j], and the calculates the average to confirm that allvalues in the array have changed by more or less than the tollerence. The code compiles ok but then performs an illegal operation, can anyone help?

Code:

#include <iostream>
#include <cmath>
#include <fstream>
using namespace std;
int main() {

//Declaration of variables
float V[20][20], V0[18][18], diff[18][18];
int i, j;


for (i=0;i<=19;i++) {
for (j=0;j<=19;j++) {
V[0][j] = 0.0;
V[19][j] = 1.0;
V[0] = (i)/19.0;
V[19] = (i)/19.0;
}
}
for (i=1; i<=18; i++) {
for (j=1; j<=18; j++) {
V[j]=1.0;
}
}

float maxdiff, sum, diffsum;
int count=0;

for(i=1;i<=18;i++) {
for(j=1; j<=18; j++) {
diff[j]=1.0;
}
}


maxdiff=0.0001;
diffsum=1.0;

while (diffsum >= maxdiff) {
sum = 0.0;
for (i=1; i<=18; i++) {
for (j=1; j<=18; j++) {
V0[j] = V[j];
V[j] = ((V[i+1][j] + V[i-1][j] + V[j+1] + V[j-1])/4.0);
diff[j] = (V0[j] - V[j]);
sum = sum + diff[j];
}
}
diffsum=sum/(18 * 18);
count++;
}
cout << count;

}
 
Technology news on Phys.org
What compiler, what operating system, are you sure problem is within your code. It compiles and runs here (Visual C++, Windows).
 
C++? Looks more like C with an element of C++
 
There's a reason why in C++ (unlike C) you are allowed & recommended to declare variables just before you need to use them, and not right at the start miles away from where your code is. There's also a reason why in loops, it is normal to count from [0, n) where your array is size n. It's easy to see you not gone over that way.

Now to the code:
Code:
    float V[20][20], V0[18][18], diff[18][18];

//...

    for(i=1;i<=18;i++) { 
        for(j=1; j<=18; j++) {
            diff[i][j]=1.0; // <--- What's wrong here?
        }
    }

//...

        for (i=1; i<=18; i++) {
            for (j=1; j<=18; j++) {
                V0[i][j] = V[i][j]; // <--- here?
                //...
                diff[i][j] = (V0[i][j] - V[i][j]); // <--- here?
                sum = sum + diff[i][j]; // <--- and here?
 
Hi, Thanks for the replies.

OS: XP SP2, Used qunicy 2005 to compile.

I managed to sort the problem out, redefiend the Arrays V0 and diff to 20 20.

Also in reply to the question about the loop counts, I counted from i=1 to 18 in calculating the V0 and diff arrays so the values were directly comparable to the original array V.

Code now works fine.

Another question though, is there a tagging format i can use to format an output file as an xls doc so i can get rows and and columns formated correctly and not just as spaces and endls within a txt doc?

Thanks again

Sol
 
Why don't you use CSV (comma separated values)?

The sooner you learn to start your indices at 0, the better for you.
 
Have managed to figure that 1 aswell, got some nice surface plots out.
Thanks for the help
 
As far as I know, all arrays in any given programming language are always assumed to start from 0. It might seem confusing at first, but it'll sort out a lot of confusion as well.
 
computerex said:
As far as I know, all arrays in any given programming language are always assumed to start from 0. It might seem confusing at first, but it'll sort out a lot of confusion as well.

No. In Pascal you may have

Code:
realtable : array[10..100] of real;

There is no such element as realtable[0].
 
  • #10
His loops run from 1 to 18 because his 20x20 array also contains the boundary conditions (those occupy all elements a[0][j], a[19][j], a[0], and a[19]). Notice that inside his loop, he accesses elements i-1 and i+1.

What the loop COULD use, is a comment explaining what it's doing and why it doesn't go from 0 to 19 like you would expect.
 
  • #11
It's also quite strange to see floats being defined. I can't recall the last time I saw C++ code using them.

By the way, since you've now seemingly sorted out what was causing you problems, it's of little harm to mention that there's nothing in the code that couldn't be accomplished just as easily with C. If you wanted to write the code properly in C++ you'd be much better off using an appropriate container, say, std::vector<std::vector<double >>, and working with the containers using typedef'd iterators. It won't add much in the way of speed, but the code will at least be reusable. As it stands, the code you're using is a bloody mess and you won't have a clue what you've done if you ever need to revisit it in the future.
 
Last edited:
  • #12
Borek said:
computerex said:
As far as I know, all arrays in any given programming language are always assumed to start from 0. It might seem confusing at first, but it'll sort out a lot of confusion as well.

No. In Pascal you may have
Or associative arrays in languages like PHP etc.

Ben Niehoff said:
His loops run from 1 to 18 because his 20x20 array also contains the boundary conditions (those occupy all elements a[0][j], a[19][j], a[0], and a[19]). Notice that inside his loop, he accesses elements i-1 and i+1.

What the loop COULD use, is a comment explaining what it's doing and why it doesn't go from 0 to 19 like you would expect.

You missed the part of his declaration where V0 & diff was both 18x18 and all the lines I highlighted where the counter went [1, 18].
 
  • #13
If he doesn't need double precision, then why declare doubles? That's just a waste of resources.

I don't see why he needs container classes, either. In fact, arrays seem more appropriate here. Semantically, he is NOT working with a vector of vectors, but with a field defined over 2-dimensional space. The best way to make this code reusable (in my opinion) would be to encapsulate the arrays in a ScalarField class.
 
  • #14
KTC said:
You missed the part of his declaration where V0 & diff was both 18x18 and all the lines I highlighted where the counter went [1, 18].

Yeah, I didn't see that he had declared his other arrays as 18x18. Whoops. :P
 
Back
Top