Thread: linking sub programs to main()

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    11

    linking sub programs to main()

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

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    You can group printf() statements, it makes it easier to read, ie:

    Code:
       printf(  "Welcome to the Engine Efficiency Calculator! To begin, enter the number to \n"
                "show a list of engine efficiencies for the gas selected. If you wish to use \n"
                "a different specific heat ratio than those shown, enter 5. \n"
    
                " 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"
                "Enter your option:\n");
    Code:
        double calculate_eta(int eta[i], int i, double k);
        int print_table(double k, int eta[7]);
    Look like function prototypes (in main), if they are indeed prototypes, make them global.

    What is 'z', won't it always be 1? In that case you can write,
    Code:
    while(1)
    {
    
    /* or */
    for(;;)
    {
    Where exactly does it repeat? Try using a debugger or add some 'printfs'. You seem to be calling get_k() twice... once at initialization of 'k' in main;
    Code:
    double k = get_k();
    and again in the loop, why? Should get_k() only run once?
    Last edited by zacs7; 11-05-2007 at 12:54 AM.

  3. #3
    Registered User
    Join Date
    Nov 2007
    Posts
    11
    so you are saying that i should make these things global, i.e. outside of main?

    Code:
        double calculate_eta(int eta[i], int i, double k);
        int print_table(double k, int eta[7]);
    and which place would it be better to call get_k? in the loop im guessing?

    EDIT:

    okay, using printf statements, I found that the place where the code starts to fail is in main, in the else statement, with the line

    Code:
    int print_table(double eta[i], double k);
    its either that, or the calculation... i need to store all of the calculated values in eta...
    Last edited by cuba06; 11-05-2007 at 02:39 AM.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    A few other nigglies:
    Code:
           double SpecificHeat[4] = {1.416, 1.398, 1.314, 1.35}, k = 0;
    Don't initialize an array and a simple variable on the same line - it's terribly hard to read (it's easy to miss the single variable at the end). Assuming that the above array is constant, you should also make it "static const" so that:
    - it only needs to be initialized once [by making it static]
    - you get a compiler error if you by mistake attempt to modify the values [by making it const].
    [This obviously forces it to be a separate line too]

    Code:
                     return k = -1;
    Whilst this is technically correct - why? Either you want to return -1, or you want to set k = -1 and then return k a line or two further down.

    And you have a problem if someone enteres 0 or a number greater than 7 - k is now zero, which will lead to interesting computations in your calculation [k-1 -> -1, meaning the 1/CompressionRatio].

    Code:
    /*  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];
    }
    You probably don't want a return there - make the function void and let if "fall through the end". As it is, you are indexing out of bounds, because when the loop ends, i is 7, whcih is the eighth element of your seven element array. Where you are calling the function, it doesn't use the return value anyways.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 09-23-2008, 07:27 PM
  2. Memory leaks problem in C -- Help please
    By Amely in forum C Programming
    Replies: 14
    Last Post: 05-21-2008, 11:16 AM
  3. Need help with some C programs. (VERY Long Post)
    By McFury in forum C Programming
    Replies: 9
    Last Post: 04-30-2004, 12:33 PM
  4. void main
    By Shadow in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 05-29-2002, 07:08 PM
  5. void or int for main?:)
    By bigtamscot in forum C Programming
    Replies: 13
    Last Post: 09-27-2001, 03:11 AM