Thread: what is wrong in this simple code

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    24

    what is wrong in this simple code

    Hey Pals, here I wrote one simple code to be more readable but something is wrong when i am compile it, the problem is print out the values of f[i][j] is wrong coz is a lot of 0's
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<math.h>
    #define max 300
    int
    main()
    {
    	float x[max],y[max],f[max][max],u[max][max],v[max][max],ui,xs,ys,dx;
    	int imax,jmax,i,j,il,it;
    	il=11;
    	it=31;
    	imax=41;
    	jmax=12;
    	xs=1.24;
    	ys=1.24;
    	ui=0.0;
    	dx=-1.0/(il-it);
    	for(i=il;i<=it;i++)
    	x[i]=(i-il)*dx;
    	for(i=it+1;i<=imax;i++)
    	x[i]=x[i-1]+(x[i-1]-x[i-2])*xs;
    	for(i=il-1;i>=1;i--)
    	x[i]=x[i+1]+(x[i+1]-x[i+2])*xs;
    
    	/*initial condition*/
    	for(i=1;i<=imax;i++)
    	{
    		for(j=1;j<=jmax;j++)
    		f[i][j]=ui*x[i];
    		
    	}
    	/*0000000000000000000000000000000000000000000000000*/
    	/*=====================Velocities===========================*/
    		for(i=2;i<=imax-1;i++)
    	{
    		for(j=2;j<=jmax-1;j++)
    		
    		
    		u[i][j]=(f[i+1][j]-f[i-1][j])/(x[i+1]-x[i-1]);		
    		
    					
    	}  
    	/* PRINT*/
    		for(j=1;j<=jmax;j++)
    	{
    		for(i=1;i<=imax;i++)
    		
    		printf("%f\n",f[i][j]);
    				
    	}
    }
    iam new in c so be patient with me :-)

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Considering that f[][] is initialized to all zeros:
    Code:
        ui=0.0;
        dx=-1.0/(il-it);
        for(i=il;i<=it;i++)
    	x[i]=(i-il)*dx;
        for(i=it+1;i<=imax;i++)
    	x[i]=x[i-1]+(x[i-1]-x[i-2])*xs;
        for(i=il-1;i>=1;i--)
    	x[i]=x[i+1]+(x[i+1]-x[i+2])*xs;
    
        /*initial condition*/
        for(i=1;i<=imax;i++)
        {
    	for(j=1;j<=jmax;j++)
    	    f[i][j]=ui*x[i];
    		
        }
    and doesn't change later on, I fail to see how it can become anything but zero from this
    Code:
        for(i=2;i<=imax-1;i++)
        {
    	for(j=2;j<=jmax-1;j++)
    		
    		
    	    u[i][j]=(f[i+1][j]-f[i-1][j])/(x[i+1]-x[i-1]);		
    		
    					
        }
    Usually, zero divided by some number gives zero.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    24
    op was a big mistake of course ui=1.0; but my question now is focus in the results of u[i][j] that probably will 0's and 1's, could you advise me concerning this value u[i][j]
    thanks in advance

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Cahnging ui to 1.0 gives
    Code:
    -1.961893
    -1.532172
    -1.185623
    -0.906147
    -0.680764
    -0.499003
    -0.352422
    -0.234211
    -0.138880
    -0.062000
    0.000000
    0.050000
    0.100000
    0.150000
    0.200000
    0.250000
    0.300000
    0.350000
    0.400000
    0.450000
    0.500000
    0.550000
    0.600000
    0.650000
    0.700000
    0.750000
    0.800000
    0.850000
    0.900000
    0.950000
    1.000000
    1.062000
    1.138880
    1.234211
    1.352422
    1.499004
    1.680765
    1.906148
    2.185624
    2.532173
    2.961895
    -1.961893
    -1.532172
    -1.185623
    -0.906147
    -0.680764
    -0.499003
    -0.352422
    -0.234211
    -0.138880
    -0.062000
    0.000000
    0.050000
    0.100000
    0.150000
    0.200000
    0.250000
    0.300000
    0.350000
    0.400000
    0.450000
    ...
    I have no idea what they are supposed to be, and to figure out what goes wrong, I suppose you need to understand what the intermediate values are supposed to be, and what they really are.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    I had been working on your code for few mins. I really see that maths behind is totally wrong. You need to really understand the formula before you apply them. Most of our solution is learing to negative values and your trying to double the negative value again by multipling it. Which dost sound good for me. Have a look at the maths. I guess you might have understood the typos good here. Since you have applied it right!

    -ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. whats wrong with this very simple code?
    By sway1 in forum C++ Programming
    Replies: 2
    Last Post: 07-06-2008, 12:18 PM
  2. What's wrong with this code?
    By Luciferek in forum C++ Programming
    Replies: 4
    Last Post: 06-21-2008, 12:02 PM
  3. Whats wrong with this simple code?
    By o14v in forum C++ Programming
    Replies: 4
    Last Post: 03-19-2008, 01:46 PM
  4. What is wrong with my code? My first program......
    By coreyt1111 in forum C++ Programming
    Replies: 11
    Last Post: 11-14-2006, 02:03 PM