Thread: program won't run properly, help needed asap

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    41

    Exclamation program won't run properly, help needed asap

    Would someone please run my program to see what I'm doing wrong? My program is at the bottom of the page.


    Refrigerator cycle: cop = Tc / Th - Tc



    Heat pump: cop = Th / Th - Tc

    where Tc and Th are the absolute temperatures (K) of cold and hot reservoirs.



    Implementation Details:

    Write a function to enter and return the temperatures of the cold and hot reservoirs in degrees Celsius; return them through pointer parameters. Make sure that both are greater than absolute 0(allow the user to correct any errors) and that they have the proper relationship to one another; that is Th > Tc (if not, swap them).
    Write a function to convert a Celsius temperature to an absolute temperature in degrees Kelvin. Absolute 0 is -273.15 degrees Celsius.
    Write a function, name cop(), that will compute both values of cop, given the Th and Tc, and return the results using call by address.
    Write a main program that will calculate and print the values of cop for either a refrigerator or a heat pump. Use a query loop to repeat the process, asking the user to select one or the other computation or "quit". Do the input and calculations using functions described above. Although the cop() function returns two results, print only the result that the user requested.


    Hint:

    Kelvin = Celsius + 273.15;





    Select your computation, 1 for heat pump or 2 for refrigerator :1

    Enter temperatures for hot and cold reservoirs.

    Temperatures must be greater than -273.15 and

    hot reservoir temperature must be greater

    than cold reservoir temperature.

    56 78

    Temp = 56.00 (cold) and 78.00 (hot) in Celsius

    Temp = 329.15 (cold) and 351.15 (hot) in Kelvin

    The value of cop for heat pump = 15.96

    Continue ?? < type 1 to continue. 1



    Select your computation, 1 for heat pump or 2 for refrigerator :1

    Enter temperatures for hot and cold reservoirs.

    Temperatures must be greater than -273.15 and

    hot reservoir temperature must be greater

    than cold reservoir temperature.

    56 90

    Temp = 56.00 (cold) and 90.00 (hot) in Celsius

    Temp = 329.15 (cold) and 363.15 (hot) in Kelvin

    The value of cop for heat pump = 10.68



    Continue ?? < type 1 to continue. 1





    Code:
    #include <stdio.h>
    void enter_temp(float*, float*);
    void convert(float*, float*);
    void cop(float*, float*,float*, float*);
    
    int main(void){
    
    float h, c, hcop, ccop;
    int again=1;
    int selection;
    float kelvin;
    printf("Select your computation, 1 for heat pump or 2 for refrigerator:");
    scanf("%d", &selection);
    
    while (again==1){
    printf("Continue?? <type 1 to continue>.");
    scanf("%d", &again);
    }
    while(selection !=1 &&  selection !=2){
    printf("Select your computation, 1 for heat pump or 2 for refrigerator:");
    scanf("%d", &selection);}
    
    
    enter_temp(&h, &c);
    printf("Enter the temperatures for hot and cold reservoirs.\n");
    printf("%f%f", &h, &c);
    printf("Temperatures must be greater than -273.15 and hot reservoir");
    printf("temperatures must be greater than cold reservoir temperature.");
    
    convert(&h, &c);
    printf("Temp = %f, &c (cold) and %f, &h (hot) in Celsius\n");
    
    printf("Temp = %f, (cold) and %f (hot) in Kelvin\n", c+273.15, h+273.15);
     scanf("%.2f%.2f", &kelvin);
    
    cop(&h, &c, &hcop, &ccop);
    
    printf ("The value of cop for refrigerator cycle = %f\n", ccop);
    scanf("%.2f", &ccop);
    printf("The value of cop for heat pump = %f\n", hcop);
    scanf("%.2f", &hcop);
    
    printf("Continue?? <type 1 to continue.");
    scanf("%d", &again);
    
    return 0;
    }
    void enter_temp(float *hPtr, float *cPtr){
    float h, c;
    
    printf("Enter the temperatures for hot and cold reservoirs.\n");
    printf("%f%f", &h, &c);
    printf("Temperatures must be greater than -273.15 and hot reservoir");
    printf("temperatures must be greater than cold reservoir temperature.");
    
    while (h > -273.15 && c > -273.15){
    printf("Enter the temperatures for hot and cold reservoirs.\n");
    printf("%f%f", &h, &c);
    printf("Temperatures must be greater than -273.15 and hot reservoir");
    printf("temperatures must be greater than cold reservoir temperature.");
    }
    while (h>c){
    printf("Enter the temperatures for hot and cold reservoirs.");
    printf("%f%f", &h, &c);
    printf("Temperatures must be greater than -273.15 and hot reservoir");
    printf("temperatures must be greater than cold reservoir temperature.");}
    
    
    return;
    
    }
    
    void convert(float *hptr, float *cptr){
    
    float h, c, kelvin;
    
    kelvin = h + 273.15;
    kelvin = c + 273.15;
    printf("Temp = %f, &c (cold) and %f, &h (hot) in Celsius\n");
    
    printf("Temp = %f, (cold) and %f (hot) in Kelvin\n", c+273.15, h+273.15);
     scanf("%.2f%.2f", &kelvin);
    
    return;
    }
    
    void cop(float *hPtr,float *cPtr,float *hcopPtr,float *ccopPtr){
    
    float h, c, hcop, ccop;
    
    ccop= c/ h-c;
    hcop= h/ h-c;
    
    printf ("The value of cop for refrigerator cycle = %f\n", ccop);
    scanf("%.2f", &ccop);
    printf("The value of cop for heat pump = %f\n", hcop);
    scanf("%.2f", &hcop);
    
    return;
    
    }

    Code tags added by Hammer.

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    The problem is, the program does nothing! There are too many logic errors. Why are you writing a program when you should be getting down the basics first?

    You have all of these pointers passed to functions but never touch assignment.

    You try to print more or less variables than you specify.

    The flow of your loops is screwed up.

    Must I go on? I don't mean to be hard on you but you have to do things one step at a time.

    First, printf(). This function does not take the address of variables, so drop the amperstand (&). Both printf and scanf rely on you to make sure that the control string you pass contains the same amount of arguments passed and the correct types.

    Next assignment. What do you think this does?

    int num = 25;
    int * ptr_to_num;
    printf("Num = %i", *ptr_to_num);

    Nothing! Because you haven't assigned the pointer to the variable. Likewise, in functions, if you want to change the value of a parameter passed to it, you must use the parameter!!

    void change(int * num){
    *num = *num + 10;
    }

    void no_change(int num){
    num = num + 10;
    }

    Finally, loop logic can follow this basic pattern:

    int flag = TRUE;
    while(flag == TRUE){
    //...ask for input.
    //...if user wants out, set flag to 0 or if you need to, use 'break'.
    //...process input.
    //...print input, pause screen...this continues "infinitely".
    }

    Anyway, I really think you should get all that squared away before you start on more programs. Also, you should always have a "tester" project open and ready at all times to test functions, ideas, etc...

    Ok, I'll leave you alone now
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Please use code tags when posting code, and read the rules regarding "urgent" help.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Releasing a program - what is needed? A few Q's
    By ulillillia in forum Tech Board
    Replies: 9
    Last Post: 04-28-2007, 12:18 AM
  2. Replies: 2
    Last Post: 12-22-2006, 08:45 PM
  3. Program does not run outside of the IDE
    By darksaidin in forum C++ Programming
    Replies: 3
    Last Post: 08-09-2003, 01:31 AM
  4. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM
  5. Program to run in Dos only
    By ihsir in forum C++ Programming
    Replies: 2
    Last Post: 01-19-2002, 06:16 PM