I have to make a program that will calcuate the efficiency of an engine. The efficiency is called eta, and it uses the variables CompressionRatio and the specific heat capacity (k).
I need to have a sub program set for simply asking the user to input a value of k, one sub program to calculate eta and one program to print a table of results. I need to link all of these in main.
Here is what I have so far, and I don't know what I am doing wrong. As it stands, when executed, the program will run the sub program get_k, but then it does nothing. It will simply run the program get_k again, but not calculate anything or print any sort of a table.
I made the compression ratios an array, and I put eta as an array as well because I need to keep each result of the calculation and print it in the table. Is there a different way of doing that, and would any of you be able to show me how to do it in code?
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; }



LinkBack URL
About LinkBacks


