Some more C help...
I was instructed to create a program that computes the efficiency of an engine based on the specific heat ratio of a gas, for different compression ratios.
We were told that we need sub-functions that do a bunch of different things, and then to link them into the main function. I've got what I think is most of the code to be done, with obvious additions that I still need to put in, but the stuff I have isnt working properly. It will run, but when I enter an option to choose the specific heat ratio, it doesn't return the calculation properly. I tried putting all of my code in a separate program, and running it all in one main function, but the same things happen.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
double get_k()
{
double k;
int option;
/* 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");
printf("Enter your option:\n");
scanf("%d", &option);
if (option == 1)
{
k == 1.416;
return k;
}
else if (option == 2)
{
k == 1.398;
return k;
}
else if (option == 3)
{
k == 1.314;
return k;
}
else if (option == 4)
{
k == 1.35;
return k;
}
else if (option == 5)
{
printf("Please enter a different specific heat ratio: \n");
scanf("%f", &k);
return k;
}
}
double calculate_eta(double k)
{
double eta1;
eta1 = 1/pow(10, (k-1));
printf("%lf \n", eta1);
return eta1;
}
int main()
{
int z = 1;
double k = get_k();
double eta = calculate_eta (k);
while (z = 1);
{
get_k();
calculate_eta (k);
}
system("PAUSE");
return 0;
}
[2681 byte] By [
cuba06] at [2007-11-20 11:47:41]

# 1 Re: Some more C help...
k == 1.416;
should be
k = 1.416;
otherwise you are just comparing it, not changing the value. :)
# 2 Re: Some more C help...
int main()
{
int z = 1;
double k = get_k();
double eta = calculate_eta (k);
while (z = 1);
{
get_k();
calculate_eta (k);
}
system("PAUSE");
return 0;
}
also, the while loop will never exit since z is never changed.
and get_k is not doing anything, it should be k=get_k()
# 3 Re: Some more C help...
Okay, the calculation works. And my thought process with the z value was that I was going to add an option in the introduction, and then if that was chosen, I would put z = 0, and then it would break out of the loop. Is that the proper way of doing it? Or is there a better way?
# 4 Re: Some more C help...
If so, you want to compare with z == 1, or perhaps z != 0. z = 1 assigns 1 to z, so z always evaluates to 1 in the loop condition even if you set it to 0.
# 5 Re: Some more C help...
Okay, the calculation works. And my thought process with the z value was that I was going to add an option in the introduction, and then if that was chosen, I would put z = 0, and then it would break out of the loop. Is that the proper way of doing it? Or is there a better way?
the introduction is in the get_k() function which is sepearate from the main loop, and it can only return one value (i.e. k)
the easiest way to do it is to return a negative value for k (or some specific flag value like k= -999
then use
while (k >= 0);
{
k = get_k();
calculate_eta (k);
{
or
while (1);
{
k = get_k();
if (k >= 0)
calculate_eta (k);
else
break;
{
or do while etc..
# 6 Re: Some more C help...
Okay, that makes sense, and I think someone else suggested that to me as well.
I also need to calculate the efficiency using an array, so i've changed the calculation to
/*A funtion to calculate eta using an array.*/
double calculate_eta(double k)
{
double eta;
int CompressionRatio[7] = {1, 2, 3, 5, 10, 100, 1000}, i = 0;
for (i=0; i<=6; i++)
{
eta = 1/pow(CompressionRatio[i], (k-1));
}
return eta;
}
and now, in a separate function, I need to print a table that has the results. Our prof doesn't really like global variables, because he finds them "messy and confusing". In the print table function, i need to refer to the compression ratio and k, but they only work within their functions. Is there any way that I can insert these variables, while keeping them localised?
# 7 Re: Some more C help...
First of all, in that code every iteration of the loop is going to write over the previous eta value. Probably not what you want.
Second, you can always pass arrays around as arguments; for reasons which you'd probably find confusing just now, modifications you make to them within a function they're passed to will persist after the function returns, unlike "normal" arguments.
So I'd say just declare arrays in main for compressionRatio and eta, and pass them wherever they need to go.