Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

Simple code - best practices?

Rusty

Active Coder
Back again :D

Bit of a recap: I'm self taught- no mentor, no training. I do this to maintain or maybe even improve mental agility. Most of my coding is straight line number crunching directed toward distillation. In this instance, the input data are imperial units but equations are necessarily solved in metric and results displayed imperial again.

My specific question is whether to maintain two variables (lbs-grams, gal-liters, etc). Or convert from imperial to metric then back again. Or does it even matter? To me, my code seems clumsy and inelegant although it compiles and returns correct values.

Also, the main function is littered with single line equation/equalities. It seems they could be functions but at the expense of more code. What would be advised? Or again, does it matter? Any and all advice welcome. Constructive criticism to my code also appreciated. Thank you.

[CODE lang="c" title="Mixing program"]
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

/*This program is a mixing calculator for a wine fermentation. It includes a maximum water temperature function.
Objective is to find the minimum of hot and the maximum of cold water that results in the ideal temperature for yeast.*/


float get_float_input(void);
int get_int_input(void);
float calc_hot_water(float cold_water_temp, float water_liters, float strike_temp);
float calc_spec_grav(float sugar_grams, float water_liters);
float calc_boil(float p2);

int main()
{
printf("To determine correct boil temperature of water\nEnter local atmospheric pressure (mmHg)");
float p2 = get_float_input();

printf("Enter Desired Wash Volume (Gal)");
float wash_liters = get_float_input();
wash_liters *= 3.7854;

printf("\nEnter Target Alcohol %% ");
int target_alcohol = get_int_input();

printf("\nEnter Sugar Temp (F) ");
float sugar_temp = get_float_input();
sugar_temp = (sugar_temp - 32) * 5/9;

printf("\nEnter Cold Water Temp (F) ");
float cold_water_temp = get_float_input();
cold_water_temp = (cold_water_temp - 32) * 5/9;

printf("\nEnter Strike Temp (F) ");
float strike_temp = get_float_input();
strike_temp = (strike_temp - 32) * 5/9;

float sugar_grams = wash_liters * 17 * target_alcohol; /*Grams*/

float sugar_liters = (sugar_grams / 198 / 16 * 0.62) * 3.7854; /*198 grams per cup - 16 cups per gallon - reduction factor = 5/8 answer in gal*/

float water_liters = wash_liters - sugar_liters;

float spec_gravity = calc_spec_grav(sugar_grams, water_liters);

float hot_water_liters = calc_hot_water(cold_water_temp, water_liters, strike_temp);

float t2 = calc_boil(p2);

/*printf("\nThis recipe requires:\n %.1f gal of cold water at %0f degrees (F)", *****, *****);
printf("\n %.1f gal of hot water water at %1f degrees (F)", *****, *****);
printf("\n %.1f lbs of sugar", *****);
printf("\nWith a specific gravity of %2f", spec_gravity);*/

system("PAUSE");
return (0);
}
/************************************/
float get_float_input(void)
{
float input = 0;
scanf("%f", &input);
return input;
}

/**********************************/

int get_int_input(void)
{
int input = 0;
scanf("%d", &input);
return input;
}
/****************************************************************/

float calc_spec_grav(float x, float y)
{
/* x = sugar_grams*/
/*y = water_liters*/
x = x / 1000;
float specific_gravity = (((258.6+(87.96*x/y)) + sqrt(66874+(7736.96*x*x/(y*y))+(57947*x/y)))/517.2);/*From HD calcs page*/
printf("\nspec grav = %.3f", specific_gravity);
return specific_gravity;
}
/****************************************************************/

float calc_hot_water(float t1, float m1, float strike_temp)
{
/*Formula: T = ( m1*c1*t1 + m2*c2*t2 + m3*c3*t3 ) / ( m1c1 + m2c2 + m3c3 )*/

float t2 = 94.96;
float c1 = 4.186;
float c2 = 4.186;
float final_temp;
float m2 = 1;
do {
final_temp = ((m1-m2)*c1*t1 + m2*c2*t2) / (m1*c1 + m2*c2);
printf("\nfinal temp = %.2f hot water liters = %.2f", final_temp, m2);
m2 += 0.1;
}
while (final_temp <= strike_temp);
return m2;

}

/*******************************************************/
/*Calculate the boiling point of water at non-standard pressure using Clausius-Clapeyron Eq*/
/*Temperature units in Kelvin, converted to Celsius*/
/*ln(p1/p2) = (hvap/R) * (1/t2 - 1/t1)*/
/*Solve for t2 (new boiling point)*/

float calc_boil(float p2)
{
float t1 = 373.15; /*normal boil point water K*/
float t2_c = 0; /*to be solved for*/
float p1 = 760; /*standard pressure for normal boil point*/
float hvap = 40800; /*heat of vaporization for water J/mol*/
float R = 8.3145; /*real time gas constant*/

t2_c = ((hvap/R) / ((hvap/R) * (1/t1) + log(p1/p2)))-273.15;
float t2_f = t2_c * 9/5 +32;
return t2_f;

}



[/CODE]
Thank you.
 
This is simple code, indeed.

However, imagine that you finished this particular piece and released it to the world. Then, you forget about it. Three years later you get an email asking for support on this code. Thankfully, you still have it up on GitHub (or something). So, you go back to the code and try to read it.

Your three year older brain will be like "what is this?"

Sure, you'll eventually recall it and all will be well. But it'd be much better if you could just open it and know exactly what is going on - right?

For any equation, you want to encapsulate that into a function and then comment the function so that somebody else (perhaps you, in three years) can quickly know what the function does.

That should, at the expense (though, it's a good expense) of more code, immensely help with the simplicity and reading of your code.

You can do the same for unit conversions, or maybe find a library that already exists for it.

I hope this helps.

P.S. Don't be afraid of using multiple source and header files for organizing functions.
 
Thank you simple. Advice for a function per equation helps. Likewise with functions for conversions.

"P.S. Don't be afraid of using multiple source and header files for organizing functions."
(Can't get that quote thingy to work).

Above code is intended to be part of larger program. Learning to employ multiple source files are on the reading list. Lots of stuff for this old brain to absorb but slowly getting there. :)

Thanks again.

R
 

New Threads

Latest posts

Buy us a coffee!

Back
Top Bottom