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;
}