### Calculation error

Hi,

I'm trying to calculate values of array b and d.

I have checked that all the values which are assigned to arrays a and b are positive values.

But when I try to calculate using this loop the values of o is alternatively changing from positive to negative.

there is some kind of error within this part that I'm unaware.

Please guys help me to fix this error.

 ``1234567`` `````` for(int i=3;i
Last edited on
You show the code, which is your proposed solution. But you didn't state what the original problem was, so it's not possible to know whether this is correct or not.
Hi Chervil,

I have posted you entire code.

I tried to fix this bug. it works well till line 52 .

The problem is with array b[i-1]. when i try to calculate and print b[i-1].

It prints alternative negative numbers. But I didn't assign any negative values to any of arrays.

I don't understand how does it prints a negative value for b[i-1].

But if I'm taking b[i] instead of b[i-1]. It shows perfect value which has to be same for b[i-1] also.

please kindly go through the code. Let me know where is the error.

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374`` ``````#include #include #include #include #include #include using namespace std; int main() { double dt; int j=41,n=45,k; double uy[45],y[45],a[n],b[n],c[n],d[n]; double dy=0.001,mu=0.000217,f; float o; cout<<"Enter time step interval of your choice : "; cin>>dt; for(int i=1;i<=j;i++) if (i==1) { y[i]=40; } else { y[i]=0; } f=mu*dt/(dy*dy); for(int i=2;i1;i--) { y[i]=(d[i]-(c[i]*y[i+1]))/b[i]; } cout<<"CALCULATION COMPLETE !!!!"; getch(); } ``````

@Vigii,

I noticed these things:

On line 29 dt is not initialised, producing garbage values for f, and for all the values in the a array.

On lines 36, 42, 48 b[i] is always negative by assignment, but could be positive given the garbage value for f.

Btw, I think you should always use doubles, not floats - mixing the two can cause problems, and there is no need to use floats (presumably not processing billions of them).

Hope all goes well.
Hi,

You can see dt is read as user input on line 18.

and also the value is non-negative yielding 0.132 as a value for b[i].