Thread: Creating array from data file...

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    6

    Creating array from data file...

    My program needs to read double numbers from an input file and store them in an array. I've written the code and tested several functions using an array in the main program. Now I need to adapt the program to read from a data file. Any help would be greatly appreciated. Here's the code...

    #include <stdio.h>
    #define MAXEL 10
    double find_max(double [], double);
    double find_min(double [], double);
    int get_min_range(double list[], double first, double last);
    void sel_sort(double list[], int n);


    int main(void){
    double nums[] = {5.00, 3.00, 0.00, 9.00, 8.00, 1.00, 7.00, 4.00, 10.00, 2.00};
    int i = 0;
    double sum = 0;

    sel_sort(nums, 10);

    printf("\nThe descending order of the array is:\n\n");
    while( i < 10){

    printf("%6.2f\n", nums[i]);
    i++;

    }
    printf("\nThe maximum value in the array is %6.2f. \n", find_max(nums, 10));
    printf("\nThe minimum value in the array is %6.2f. \n\n", find_min(nums, 10));


    return(0);
    }

    double find_max(double last[], double num_ele){
    int i, max = last[0];
    for(i = 1; i < num_ele; i++)
    if(max < last[i]) max = last[i];

    return (max);
    }

    double find_min(double first[], double num_ele){
    int i, min = first[0];
    for(i = 1; i < num_ele; i++)
    if(min > first[i]) min = first[i];

    return (min);
    }

    int get_min_range( double list[], double first, double last){
    int i, small_sub;
    small_sub = first;

    for(i = first + 1; i <= last; ++i)
    small_sub = i;

    return(small_sub);
    }

    void sel_sort(double *list, int n){
    int temp;
    int i;
    for(i = 0; i < n - 1; i++){
    int max = i;
    int j = 0;

    for(j = i + 1; j < n; j++){
    if(list[j] < list[max]) { max = j; }
    }
    temp = list[max];
    list[max] = list[i];
    list[i] = temp;
    }
    }

  2. #2
    First off you'll have to declare the size of your array when you declare the array itself (since you're reading data from file). Then create a temp storage location for the number being read in from file, and don't forget, the actual file.

    Something like this:
    Code:
    nums[MAXEL];   /*MAXEL WAS DECLARED AS A CONSTANT IN YOUR CODE*/
    double newInfo;
    FILE *input = fopen("data.dat", "r");  /*WHERE data.dat IS THE NAME OF YOUR DATA FILE*/
    
    if (input == NULL)
    {
       printf("Cannot open file.\n");
       exit(1);
    }
    
    for (int k = 0; fscanf(input, "%lf", &newInfo) != NULL; j++)
       nums[j] = newInfo;
    
    fclose(input);
    There's probably more than a few ways to do it, but that's what I would do.

    [EDIT] forgot to include the fclose(input) and some documentation [/EDIT]
    Last edited by DrakkenKorin; 12-06-2001 at 09:47 PM.
    DrakkenKorin

    Get off my Intarweb!!!!

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    It depends on how the numbers are stored in the file.

    If they are stored in ASCII format... that is, if you can open the file with a text editing program and see the doubles, then you'll need to use fscanf...

    Here is a sample program to add all the doubles in the file called data.txt...
    Code:
    #include <stdio.h>
    
    int main (void)
    {
     FILE * f;
     double d, total;
    
     f = fopen ("data.txt", "r");
     total = 0;
    
     for (;;)
     {
      // fscanf will return 0 if it encounters a non-double
      // will return 1 if it encounters a double
      // will return EOF if it reached the end of file
      if (fscanf (f, "%lf", &d) != 1) break;
      printf ("Adding %.2f.\n", d);
      total += d;
     }
     printf ("The total is %.2f.\n", total);
     return 0;
    }
    Modify the loop to suit your needs.
    Callou collei we'll code the way
    Of prime numbers and pings!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. xor linked list
    By adramalech in forum C Programming
    Replies: 23
    Last Post: 10-14-2008, 10:13 AM
  2. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  3. Profiler Valgrind
    By afflictedd2 in forum C++ Programming
    Replies: 4
    Last Post: 07-18-2008, 09:38 AM
  4. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  5. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 07:31 AM