Thread: add an array

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    19

    add an array

    How make this array to read 50 integers numbers

    I appreciate your help

    thanks
    Code:
    #include <stdio.h>
    
    int
    main (void)
    {
        int numbers[10], i;
        FILE *input;
        
        input = fopen("numbers.txt", "r");
        
        for (i=0; i<10; ++i)
            fscanf(input, "%d", &numbers[i]);
        
        
        printf("The numbers read are: ");
        for (i=0; i<10; ++i)
            printf("%4d", numbers[i]);
        
        printf ("\n");
        
        fclose (input);
        
        return (0);
    }

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    You want to store 50 integers , so you will need an array of size 50, not 10.

    I suggest you to use fread to read data from file

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Change all the "10"s to "50"s.

    Or better yet, "#define MAX_INTS 50" and change all "10"s to "MAX_INTS"

  4. #4
    Registered User
    Join Date
    Oct 2012
    Posts
    19
    where should i place the file to read or open

  5. #5
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    In the same folder that you are working all.This is not mandatory, but it is good for a beginner

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 10
    Last Post: 09-09-2012, 02:29 PM
  2. Replies: 2
    Last Post: 03-20-2012, 08:41 AM
  3. Replies: 9
    Last Post: 08-23-2010, 02:31 PM
  4. Replies: 9
    Last Post: 04-07-2010, 10:03 PM
  5. Replies: 6
    Last Post: 11-09-2006, 03:28 AM

Tags for this Thread