Code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
/* A function to introduce the program and return a specific heat ratio. */
double get_k()
{
int option;
double SpecificHeat[4] = {1.416, 1.398, 1.314, 1.35}, k = 0;
/* Introduction */
printf("Welcome to the Engine Efficiency Calculator! To begin, enter the number to \n");
printf("show a list of engine efficiencies for the gas selected. If you wish to use \n");
printf("a different specific heat ratio than those shown, enter 5. \n");
/* List options. */
printf(" 1. Hydrogen (k = 1.416)\n 2. Oxygen (k = 1.398)\n 3. Carbon Dioxide (k = 1.314)\n 4. Air/Fuel Mixture (k = 1.35)\n 5. Other\n 6. Terminate Program\n");
printf("Enter your option:\n");
scanf("%d", &option);
if (option >=1 && option <= 4)
{
k = SpecificHeat[option - 1];
}
else if (option == 5)
{
printf("Please enter a different specific heat ratio between 1.0 and 2.0: \n");
scanf("%f", &k);
while (k<1 || k>2)
{
printf("Error! You must enter a specific heat ratio between 1.0 and 2.0!\n");
printf("Please enter a different specific heat ratio:\n");
scanf("%lf", &k);
}
}
else if (option == 6)
{
printf("GOODBYE! (press any key to quit)\n");
return k = -1;
}
return k;
}
/* A funtion to calculate eta using an array. */
double calculate_eta(double eta[], int i, double k)
{
int CompressionRatio[7] = {1, 2, 3, 5, 10, 100, 1000};
for (i=0; i<7; i++)
{
eta[i] = 1 - (1/pow(CompressionRatio[i], (k-1)));
}
return eta[i];
}
/* A function to print a table with the results */
int print_table(double eta[7], double k)
{
printf("-----------------------------------------------------\n");
printf("Compression Ratio Specific Heat Capacity Efficiency\n");
printf("1 | %lf | %lf\n", k, eta[0]);
printf("2 | %lf | %lf\n", k, eta[1]);
printf("3 | %lf | %lf\n", k, eta[2]);
printf("5 | %lf | %lf\n", k, eta[3]);
printf("10 | %lf | %lf\n", k, eta[4]);
printf("100 | %lf | %lf\n", k, eta[5]);
printf("1000 | %lf | %lf\n", k, eta[6]);
printf("-----------------------------------------------------\n");
}
int main()
{
/* Declare Variables used in main */
int i, eta[7], z = 1;
double k = get_k();
double calculate_eta(int eta[i], int i, double k);
int print_table(double k, int eta[7]);
while (z == 1)
{
k = get_k();
if (k == -1)
{
break;
}
else
{
calculate_eta(eta[i], i, k);
printf("eta is %lf", eta[i]);
print_table(k, eta[i]);
}
}
system("PAUSE");
return 0;
}