Thread: Advice on using arrays for large data sets

  1. #1
    Registered User
    Join Date
    Sep 2020
    Posts
    31

    Advice on using arrays for large data sets

    This will be a calculator for proofing spirits in accordance with Federal standards. The method uses tables established by the Tax and Trade Bureau. The data set is large; 1 to 200 proof over a temperature range of 70 degrees (F) in one degree increments. Ethanol and water mixing are not trivial, hence the tables. The Europeans use an equation with over 60 coefficients!


    There are two problems.
    1. Find the true proof, standardized at 60 degrees. For example, from Table 1, if the proof is measured at 126 (abv) at a temp of 70F; the proof at 60F is 122.2


    2) Find the respective volumes of ethanol and water for that proof from Table 6. At 122 proof the volumes will be 61 parts ethanol and 42.69 parts water. Which will result in 100 parts of mix. Then simple math to determine how much water to to add to reach 80 proof, for example.

    Table 1
    61° 62° 63° 64° 65° 66° 67° 68° 69° 70°


    126 125.6 125.2 124.9 124.6 124.1 123.7 123.3 123.0 122.6 122.2
    127 126.6 126.2 125.9 125.6 125.1 124.7 124.3 124.0 123.6 123.2
    128 127.6 127.2 126.9 126.5 126.1 125.7 125.3 125.0 124.6 124.2
    129 128.6 128.2 127.9 127.5 127.1 126.7 126.4 126.0 125.6 125.2
    130 129.6 129.2 128.9 128.6 128.1 127.7 127.4 127.0 126.6 126.2
    131 130.6 130.2 129.9 129.5 129.1 128.8 128.4 128.0 127.6 127.2


    Table 6
    Proof Alc Vol Water Vol


    120 60.00 43.71
    121 60.50 43.20
    122 61.00 42.69
    123 61.50 42.18
    124 62.00 41.67
    125 62.50 41.16


    My instinct is to put the table data in arrays. It's what I know. But the way I see it is 70 arrays of 200 elements and 200 arrays of 3 elements. Is that a reasonable construct/approach? Is there a better method I should be considering? Thanks for advice.

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    1,628
    Do you know about two-dimensional arrays?
    Suppose exactly this is in a file called table1.txt:
    Code:
            61°   62°   63°   64°   65°   66°   67°   68°   69°   70°
        126 125.6 125.2 124.9 124.6 124.1 123.7 123.3 123.0 122.6 122.2
        127 126.6 126.2 125.9 125.6 125.1 124.7 124.3 124.0 123.6 123.2
        128 127.6 127.2 126.9 126.5 126.1 125.7 125.3 125.0 124.6 124.2
        129 128.6 128.2 127.9 127.5 127.1 126.7 126.4 126.0 125.6 125.2
        130 129.6 129.2 128.9 128.6 128.1 127.7 127.4 127.0 126.6 126.2
        131 130.6 130.2 129.9 129.5 129.1 128.8 128.4 128.0 127.6 127.2
    You could read it into a 2d array and access it like this:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
     
    #define T1_PROOF_LOW   126
    #define T1_PROOF_HIGH  131
    #define T1_TEMP_LOW     61
    #define T1_TEMP_HIGH    70
     
    #define T1_ROWS (T1_PROOF_HIGH - T1_PROOF_LOW + 1)
    #define T1_COLS (T1_TEMP_HIGH - T1_TEMP_LOW + 1)
     
    void read_table1(double t1[][T1_COLS])
    {
        FILE *fin = fopen("table1.txt", "r");
        if (!fin)
        {
            printf("Cannot open table1.txt\n");
            exit(EXIT_FAILURE);
        }
     
        // throw away first line
        for (int ch; (ch = fgetc(fin)) != EOF && ch != '\n'; ) ;
     
        for (int row = 0; row < T1_ROWS; ++row)
        {
            fscanf(fin, "%*d");
            for (int col = 0; col < T1_COLS; ++col)
                fscanf(fin, "%lf", &t1[row][col]);
        }
     
        fclose(fin);
    }
     
    void print_table1(double t1[][T1_COLS])
    {
        printf("    ");
        for (int col = 0; col < T1_COLS; ++col)
            printf("%2d°   ", col + T1_TEMP_LOW);
        putchar('\n');
        for (int row = 0; row < T1_ROWS; ++row)
        {
            printf("%3d ", row + T1_PROOF_LOW);
            for (int col = 0; col < T1_COLS; ++col)
                printf("%5.1f ", t1[row][col]);
            putchar('\n');
        }
    }
     
    void find_true_proof(double t1[][T1_COLS])
    {
        printf("Enter proof and temperature: ");
        int proof, temp;
        scanf("%d%d", &proof, &temp);
        proof -= T1_PROOF_LOW;
        temp -= T1_TEMP_LOW;
        if (proof < 0 || proof >= T1_ROWS)
        {
            printf("Proof is outside the allowed range of %d to %d.\n",
                T1_PROOF_LOW, T1_PROOF_HIGH);
            exit(EXIT_FAILURE);
        }
        if (temp < 0 || temp >= T1_COLS)
        {
            printf("Temperature is outside the allowed range of %d to %d.\n",
                T1_TEMP_LOW, T1_TEMP_HIGH);
            exit(EXIT_FAILURE);
        }
        printf("The true proof is %.1f\n", t1[proof][temp]);
    }
     
    int main()
    {
        double t1[T1_ROWS][T1_COLS];
        read_table1(t1);
        //print_table1(t1);
        find_true_proof(t1);
        return 0;
    }
    A little inaccuracy saves tons of explanation. - H.H. Munro

  3. #3
    Registered User
    Join Date
    Sep 2020
    Posts
    31
    Wow! Thank you for the code.

    I was aware of 2D arrays but haven't employed any yet. Time to give them some more study.
    This program will be the third module in a series I've written for hobby distillers. I can't imagine my program being where it is (if at all) without the help I've gotten on this forum. I'm so appreciative for that. Thanks again john c- especially for the code.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Advice needed, how to encode large arrays
    By baxy in forum C Programming
    Replies: 4
    Last Post: 10-17-2012, 01:21 PM
  2. What am I missing? (Arrays/Sets)
    By jacob_76505 in forum C Programming
    Replies: 3
    Last Post: 10-12-2011, 11:51 PM
  3. Reading large data sets from a file
    By DMaz in forum C Programming
    Replies: 7
    Last Post: 01-18-2011, 07:05 PM
  4. [Large file][Value too large for defined data type]
    By salsan in forum Linux Programming
    Replies: 11
    Last Post: 02-05-2008, 04:18 AM
  5. Reading SAS data sets from C (or C++)
    By dougj in forum C Programming
    Replies: 3
    Last Post: 02-15-2002, 08:50 AM

Tags for this Thread