problems with arrays

This is a discussion on problems with arrays within the C Programming forums, part of the General Programming Boards category; Hi Pals, well my new problem is that I would like the output of my code as an array,and I ...

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

    problems with arrays

    Hi Pals, well my new problem is that I would like the output of my code as an array,and I dont get it at all, I would like to have the results of u[1][1] u[1][2] u[1][3] in my output
    thanks
    Code:
    /*Heat equation explicit scheme */
    #include <stdio.h>
    #include<stdlib.h>
    #include<math.h>
    #define max 100
    #define pi 4*atan(1)
    int 
    main ()
    {
    	FILE *heat;
    	float t[max],x[max],u[max][max],k,h,r,s;
    	float tmax;
    	int n,j,m,i;
    	float xmax,nstep,alpha;
    	/*printf("enter the value of L=");scanf("%d",xmax);*/
    	/*printf(" steps of x=");scanf("%f",nstep);*/
    	/*printf("maximun value of Tmax");scanf("%d",tmax);*/
    	/*printf("enter alpha");scanf("%d",alpha);*/
    	xmax=1;
    	nstep=2;
    	tmax=0.5;
    	alpha=1;
    	heat=fopen("heat.data","w");
    	/* Estability*/
    	/*h=xmax/nstep;*/
    	h=xmax/nstep;
    	k=0.5*h*h/alpha;
    	m=3;
    	n=2;
    	
    	
    	/* Boundary Conditions*/
    	for(j=0;j<=m;j++)
    	{
    		t[j]=j*k;
    		u[0][j]=0;
    		u[n][j]=0;
    		
    	}
    	/*Initial Conditions*/
    	for(j=1;j<=n-1;j++)
    	{
    		x[j]=j*h;
    		u[j][0]=10*sin(pi*x[j]*0.5);
    	}
    	/*Aproximations*/
    	r=alpha*k/k*h;
    	s=1-2*r;
    	for(j=1;j<=m;j++)
    	{
    		for(i=1;i<=n-1;i++)
    		
    		u[i][j]=s*u[i][j-1]+r*u[i-1][j-1]+r*u[i+1][j-1];	
    		
    		
    		
    	}
    	for(j=0;j<=m;j++)
    	{
    		for(i=0;i<=n;i++)
    		
    			fprintf(heat,"%1.5f\n%1.5f\t",t[j],u[i][j]);
    			/*printf("\n");*/
    		
    	}
    	
    	fclose(heat);
    	exit (0);
    }

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    #define pi 4*atan(1)
    Do you REALLY want to call a function and then multiply by 4 every time you use PI, simply because you can't be bothered to look up/type in the value of PI? Further, since you are using float rather than double, you only need about 6 decimal places, so 3.1415926 would do fine.

    As to the other problem: What ARE you getting?

    --
    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
    what i need is my output as an array I dont know how can i get

  4. #4
    Registered User
    Join Date
    Mar 2009
    Posts
    24
    my problems is in the output of this code, I would like to have u[i][j] as an array in the output, it should be possible to do this?
    Code:
    fprintf(heat,"%1.5f\n%1.5f\t",t[j],u[i][j]);

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Sorry, but what you do mean by "as an array"? fprintf (and it's friends) can only output one item for each % format specifier.

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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  2. My Arrays!!!, My Arrays!!! Help!?
    By llcool_d2006 in forum C++ Programming
    Replies: 1
    Last Post: 12-10-2006, 12:07 PM
  3. Problems with my arrays in this program
    By shadowctr in forum C++ Programming
    Replies: 7
    Last Post: 11-20-2005, 07:27 PM
  4. Problems with Strings and Arrays.
    By SlyMaelstrom in forum C++ Programming
    Replies: 13
    Last Post: 04-15-2005, 03:13 PM
  5. Help!!! Problems with arrays.
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 05-08-2002, 09:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21