# program won't run properly, help needed asap

• 11-15-2002
jlmac2001
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; }```

• 11-16-2002
Sebastiani
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){