Programming Fundamentals/Arrays/C
Appearance
arrays.c
[edit | edit source]// This program uses arrays to display temperature conversion tables
// and temperature as an array subscript to find a given conversion.
//
// References
// https://www.mathsisfun.com/temperature-conversion.html
// https://en.wikibooks.org/wiki/C_Programming
#include "stdio.h"
void build_c(double c[], int size);
void build_f(double f[], int size);
void display_array(char *name, double array[], int size);
void find_temperature(double c[], int c_size, double f[], int f_size);
int minimum(int value1, int value2);
int main()
{
double c[101];
double f[213];
build_c(c, sizeof(c) / sizeof(double));
build_f(f, sizeof(f) / sizeof(double));
display_array("C", c, sizeof(c) / sizeof(double));
display_array("F", f, sizeof(f) / sizeof(double));
find_temperature(c, sizeof(c) / sizeof(double),
f, sizeof(f) / sizeof(double));
return 0;
}
void build_c(double c[], int size)
{
int index;
double value;
for (index = 0; index <= size - 1; index += 1)
{
value = (double) index * 9 / 5 + 32;
c[index] = value;
}
}
void build_f(double f[], int size)
{
int index;
double value;
for (index = 0; index <= size - 1; index += 1)
{
value = (double) (index - 32) * 5 / 9;
f[index] = value;
}
}
void display_array(char *name, double array[], int size)
{
int index;
for (index = 0; index <= size - 1; index += 1)
{
printf("%s[%d] = %lf\n", name, index, array[index]);
}
}
void find_temperature(double c[], int c_size, double f[], int f_size)
{
int temp;
int size;
size = minimum(c_size, f_size);
do
{
printf("Enter a temperature between 0 and %d:\n", size - 1);
scanf("%d", &temp);
} while (temp < 0 || temp > size - 1);
printf("%d° Celsius is %lf° Fahrenheit\n", temp, c[temp]);
printf("%d° Fahrenheit is %lf° Celsius\n", temp, f[temp]);
}
int minimum(int value1, int value2)
{
int result;
if (value1 < value2)
{
result = value1;
}
else
{
result = value2;
}
return result;
}
Try It
[edit | edit source]Copy