सामग्री पर जाएँ

कंप्यूटर प्रोग्रामिंग/ऐरे/सी

विकिविश्वविद्यालय से
// This program uses arrays to display temperature conversion tables
// and temperature as an array subscript to find a given conversion.

#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;
    
    for (index = 0; index <= size - 1; index += 1) 
    {
        c[index] = (double) index * 9 / 5 + 32;
    }
}

void build_f(double f[], int size)
{
    int index;
    
    for (index = 0; index <= size - 1; index += 1) 
    {
        f[index] = (double) (index - 32) * 5 / 9;
    }
}

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

निम्न कोड मुफ्त ऑनलाइन विकास के वातावरण में से एक में ऊपर कॉपी और पेस्ट करो या अपने खुद के कम्पाइलर/इंटरप्रेटर/आईडीई का उपयोग करें।