Thread: array printing help

  1. #1
    Unregistered
    Guest

    Cool array printing help

    If I have an array: array[0] that has a value stored in it and the value changes. how do I print the values to a file formatted i.e.?

    fprintf(ofp,"%f",array[0]);


    output is:

    1.234
    2.456
    3.456
    2.367

    I want it formatted like this

    1.234 2.456
    3.456 2.367

    Thanks.

  2. #2
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,490
    Code:
    array=0;
    while (.....) {
    fprintf(ofp,"%f %f\n",array[x],array[x+1]);
    x+=2;
    }

  3. #3
    Unregistered
    Guest
    Well actually the number stored at that index {0} changes.

    ie.

    array[0] ----> array:[2.3456]
    array:[3.2456]

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Do this help you understand?
    Code:
    #include <stdio.h>
    
    int main(void)
    {
    	FILE *fp;
    	float a[1];
    	int i;
    	
    	if ((fp = fopen("test.txt", "w")) == NULL)
    	{
    		perror("File");
    		return 1;
    	}
    	
    	a[0] = 10.123;
    	
    	for (i = 1; i < 11; i++)
    	{
    		fprintf (fp, "%15.2f ", a[0]);
    		a[0] = a[0] * i;
    		if (i % 2 == 0) fputc ('\n', fp);
    	}
    	
    	fclose(fp);
    	return 0;
    	
    }
    
    /*
     Output from the program:
              10.12           10.12 
              20.25           60.74 
             242.95         1214.76 
            7288.56        51019.92 
          408159.38      3673434.50 
    */
    Last edited by Hammer; 07-10-2002 at 01:12 PM.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. printing half a char array
    By theoneandonly in forum C Programming
    Replies: 19
    Last Post: 11-11-2006, 07:27 AM
  2. Replies: 3
    Last Post: 11-03-2003, 08:55 PM
  3. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  4. Array Program
    By emmx in forum C Programming
    Replies: 3
    Last Post: 08-31-2003, 12:44 AM
  5. Printing an integer stored in an array
    By Freez3L in forum C Programming
    Replies: 4
    Last Post: 11-18-2002, 02:11 AM