Reading numbers with scientific notation

This is a discussion on Reading numbers with scientific notation within the C Programming forums, part of the General Programming Boards category; Hi, I need some help with reading a set of numbers with scientific notations. Basically what my program does is ...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    6

    Reading numbers with scientific notation

    Hi, I need some help with reading a set of numbers with scientific notations. Basically what my program does is that it reads a set of values in a text file, find the maximum value and then print it out.

    My program works with values without scientific notation but crashes when I try to read text files containing scientific notations. Any ideas what I'm doing wrong?

    The program works with this sort of file:
    Code:
    0.5 304.528
    1.5 873.17
    2.5 1636.12
    3.5 1947.69
    4.5 1189.97
    5.5 1273.69
    6.5 1650.59
    7.5 1211.33
    8.5 1211.71
    9.5 1760.09
    But crashes when I try to read this file:
    Code:
    5.50E+00	4.47E-02
    6.75E+00	5.37E-02
    7.00E+01	3.35E-02
    8.25E+01	3.68E-02
    9.50E+00	3.63E-02
    1.75E+01	3.70E-02
    2.00E+01	3.23E-02
    3.03E+00	5.25E-02
    4.05E+00	5.28E-02
    5.08E+01	5.05E-02
    The crash looks like this: http://img288.imageshack.us/img288/4775/crushbg3.jpg

    My complier is quincy and I'm only allowed to use C
    Code:
    #include <stdio.h>
    #define N 100
    
    
    double max(double x[], double n);
    
    main()
    {
    
       int k=0;
       double y[N], q;
       FILE *data;
    
    
       data = fopen("textfile2.txt", "r");
       
       if (data == NULL)
       	{
            printf("Error opening file. \n");
    		exit(1);
    	}
    		
       else
       {
           while (fscanf(data, "%lf", &y[k]) == 1)
           {
              k++;
           }
           
    	   q = k;
        
           printf("Maximum value: %lf \n", max(y, q));
        
           fclose(data);
       }
       
       return 0;
       
    }
    
    
    
    /*  This function returns the maximum value. Maybe there's something wrong with the function? */
    
    double max(double x[], double n)
    {
      
       int k;
       double max_x;
    
      
       max_x = x[9];
       for (k = 1; k <= n - 1; k++)
       {
          if (x[k] > max_x) 
    	     max_x = x[k];
       }
    
      
       return max_x;
       
    }
    Any help would be much appreciated, thanks!
    Last edited by Router; 11-01-2006 at 01:08 AM.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Code:
    #include <stdio.h>
    #include <float.h>
    
    int main()
    {
       static const char filename[] = "file.txt";
       FILE *file = fopen(filename, "r");
       if ( file )
       {
          char line[BUFSIZ];
          double a, b, max = DBL_MIN;
          while ( fgets(line, sizeof line, file) &&
                  sscanf(line, "%lf %lf", &a, &b) == 2 )
          {
             printf("a = %g, b = %g\n", a, b);
             if ( max < a )
             {
                max = a;
             }
          }
          fclose(file);
          printf("max = %g\n", max);
       }
       else
       {
          perror(filename);
       }
       return 0;
    }
    
    /* file.txt
    5.50E+00	4.47E-02
    6.75E+00	5.37E-02
    7.00E+01	3.35E-02
    8.25E+01	3.68E-02
    9.50E+00	3.63E-02
    1.75E+01	3.70E-02
    2.00E+01	3.23E-02
    3.03E+00	5.25E-02
    4.05E+00	5.28E-02
    5.08E+01	5.05E-02
    */
    
    /* my output
    a = 5.5, b = 0.0447
    a = 6.75, b = 0.0537
    a = 70, b = 0.0335
    a = 82.5, b = 0.0368
    a = 9.5, b = 0.0363
    a = 17.5, b = 0.037
    a = 20, b = 0.0323
    a = 3.03, b = 0.0525
    a = 4.05, b = 0.0528
    a = 50.8, b = 0.0505
    max = 82.5
    */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 12-21-2007, 01:38 PM
  2. reading numbers from input file
    By ziga in forum C++ Programming
    Replies: 1
    Last Post: 08-17-2007, 07:23 AM
  3. Reading numbers from a text file
    By wolfindark in forum C++ Programming
    Replies: 12
    Last Post: 03-24-2007, 02:57 PM
  4. question: reading numbers into an array
    By Lince in forum C Programming
    Replies: 15
    Last Post: 11-15-2006, 03:41 AM
  5. Need help with reading numbers from the command line
    By Nterpol in forum C Programming
    Replies: 4
    Last Post: 06-01-2006, 02:40 AM

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