Thread: Trouble with a simple array

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    36

    Exclamation Trouble with a simple array

    Hey,

    This is our first assignment with arrays, and I need to make an array that can read up to 100 items from a file (even though the file only has 31 numbers).

    Not quite sure what to do.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    What are "items"? If they're all of the same type, like integers, you can declare the array like so:
    Code:
    int file[100];
    Then just read the file like you normally do and assign to each index in the array:
    Code:
    /* Gather numbers from the file */
    for ( i = 0; i < 100; i++ ) {
      if ( fscanf ( in, "%d", &file[i] ) != 1 )
        break;
    }
    
    /* Display gathered numbers */
    for ( j = 0; j < i; j++ )
      printf ( "%d\n", file[j] );
    Last edited by Prelude; 10-27-2005 at 02:30 PM.
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    36
    what does this "break" do?

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    It ends the loop prematurely.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Or, rather, if the return value of fscanf is not one, the loop ends. (fscanf returns the number of items successfully read.)
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Registered User
    Join Date
    Oct 2005
    Posts
    36
    when you are displaying the gathered numbers with array a[j], when i delclare array 'a' at the start, what is its value

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >when i delclare array 'a' at the start, what is its value
    a doesn't exist. I usually call my scratch arrays a, and habit forced me to introduce a typo. It should be file, not a. The code has been fixed.
    My best code is written with the delete key.

  8. #8
    Registered User
    Join Date
    Oct 2005
    Posts
    36
    This is what I've got....But for some reason it just returns 0's, not the numbers in the file.
    Code:
    #include <stdio.h>
    #include <math.h>
    
    int file[100];
    
    int main (void)
    {
    	int i, j;
    	FILE *inp;
    	inp = fopen("temperature.txt", "r");
    
    
    	/* Gather numbers from the file */
    	for (i = 0; i < 100; i++) 
    	{
    		fscanf (inp, "%d", &file[i]);
    	}
    
    	/* Display gathered numbers */
    	for (j = 0; j < i; j++)
    	{
    		printf ("%d\n", file[j]);
    	}
    
    	
    	fclose(inp);
    
    	return (0);
    }

  9. #9
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    Looks fine to me. (aside from missing break; and error checking) also no reason to include math.h, maybe show us your input file?

  10. #10
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    You must error check fopen() and fscanf().
    Code:
      inp = fopen("junk1.txt", "r");
      
      if (inp == NULL)
      {
        perror("fopen");
    	...
      }
    See posts above for error checking fscanf().
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  11. #11
    Registered User
    Join Date
    Oct 2005
    Posts
    36
    it looks like my program cant find the file. where on the hard drive does it have to be? it seems like ive tried to put it everywhere imaginable.

  12. #12
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    with no path (as you have it) you would need to place it inside the the same directory as the program itself.

    This is why you have to check the return of fopen (to see if it actually openned anything

  13. #13
    Registered User
    Join Date
    Oct 2005
    Posts
    36
    Here's what I have so far. My file temperature.txt is in the right place and everything.

    Code:
    #include <stdio.h>
    #include <math.h>
    #define SIZE 100
    
    int temp[SIZE];
    
    int main (void)
    {
    	int i, sum, sum_sqr;
    	double mean, st_dev;
    	FILE *inp;
    	
    	inp = fopen("C:\\Program Files\\Miracle C\\temperature.txt", "r");
    	
    	if (inp == NULL)
    	{
    		printf("ERROR OPENING THAT FILE\n");
    	}
    
    	/* Gather and prints all the data from the file */
    	for (i = 0; i < SIZE; i++) 
    	{
    		fscanf(inp, "%d", &temp[i]);
    		printf("%d\n", temp[i]);
    	}
    
    	/*Computes the sum and the sum of the squares of all the data*/
    	sum = 0;
    	sum_sqr = 0;
    	for (i = 0; i < SIZE; i++)
    	{
    		sum+=temp[i];
    		sum_sqr += temp[i] * temp[i];
    	}
    	
    	/*Computes and prints the mean and standard deviation*/
    	mean = sum / SIZE;
    	st_dev = sqrt(sum_sqr / SIZE - mean * mean);
    	printf("The mean is %.2f.\n", mean);
    	printf("The standard deviation is %.2f.\n", st_dev);
    	
    	fclose(inp);
    
    	return (0);
    }

  14. #14
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    there u go the working code.
    Code:
    #include <stdio.h>
    #include <math.h>
    #define SIZE 100
    
    int temp[SIZE];
    
    int main (void)
    {
    	int i, sum, sum_sqr,tempi;
    	double mean, st_dev;
    	FILE *inp;
    	
    	inp = fopen("C:\\Program Files\\Miracle C\\temperature.txt", "r");
    	
    	if (inp == NULL)
    	{
    		printf("ERROR OPENING THAT FILE\n");
    	}
    
    	/* Gather and prints all the data from the file */
    	for (i = 0; i<SIZE; i++) 
    	{
    		if(fscanf(inp, "%d", &temp[i])!=1)
    		break;
    		printf("%d\n", temp[i]);
    	}
    
    	/*Computes the sum and the sum of the squares of all the data*/
    	sum = 0;
    	sum_sqr = 0;
    	tempi=i;
    	for (i = 0; i < tempi; i++)
    	{
    		sum+=temp[i];
    		sum_sqr += temp[i] * temp[i];
    	}
    	/*Computes and prints the mean and standard deviation*/
    	mean = (double)sum / SIZE;
    	st_dev = sqrt(sum_sqr / SIZE - mean * mean);
    	printf("The mean is %.2f\n", mean);
    	printf("The standard deviation is %.2f.\n", st_dev);
    	
    	fclose(inp);
        getchar();
    	return (0);
    }
    /*myoutput
    1
    2
    3
    4
    5
    6
    7
    8
    9
    The mean is 0.45
    The standard deviation is 1.34.
    */
    ssharish2005

  15. #15
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Consider adopting the habit of using / instead of \\ for Windows path delimiters in C: "C:/Program Files/Miracle C/temperature.txt" instead of ""C:\\Program Files\\Miracle C\\temperature.txt". It always works, is easier to read, and is less error prone.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. base address of a simple array
    By nacho4d in forum C Programming
    Replies: 13
    Last Post: 04-07-2008, 01:28 PM
  2. Simple 2 dimensional array problem
    By AmbliKai in forum C Programming
    Replies: 4
    Last Post: 10-30-2007, 05:49 AM
  3. simple Array question.
    By Fredir in forum C++ Programming
    Replies: 6
    Last Post: 10-06-2007, 07:12 AM
  4. Template Array Class
    By hpy_gilmore8 in forum C++ Programming
    Replies: 15
    Last Post: 04-11-2004, 11:15 PM
  5. A simple question about selecting elements in an array
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 08-30-2001, 10:37 PM