Nutrition & Diet Units of Alcohol per/glass/bottle

I wrote this a while back. Compile it into an executable and it can be used at the command prompt to calculate the standard (Aussie Standard) drinks of anything being consumed:


#include <stdio.h>
#include <stdlib.h>

/*Purpose: user to type 3 numbers following program name which will be, in order:
* 1) Number of drinks
* 2) Volume per container
* 3) Percentage of alcohol
*
* upon computing will return the standard drinks consumed.
* The formula for Aussie standard drinks is: Volume(in Litres) * Percentage* 0.789 = number of standard drinks*/

int main(int argc, char *argv[]){

int c; //counter
float argarr[argc]; //always remember arrays start with 0.
float *ptr[4]; // so you'll get a Segmentation fault trying to reference argarr[4] which is null
float calories;


if(argc != 4){ //in addition to program name, 3 arguments must be present and are not

printf("\n\nUsage:\n\n1)Numberofdrinks\n2)ContainerSize(in ml)\n3)Percentage\n\nAll as whole or floating point numbers ie: 6 375 4.5\n");
printf("For 6 375ml Containers @ 4.5%% alcohol each\n\n");
exit(0);
}


for(c=1;c<argc;++c){

argarr[c]=(strtof(argv[c],NULL)); //grabs argv strings and converts/returns argv string to a float
ptr[c]=&argarr[c]; //assigning 3 pointers, 1..3
}

*ptr[2]=(*ptr[2] * .001); //turns whole number into L(ml) percentage for container size, the 2nd argument


float stddr= ((*ptr[1] * (*ptr[2])) * ((*ptr[3])) * 0.789); //formula for calculating Aussie standard drinks
calories=stddr * 70;
printf("\nStandard Aussie drinks: %lf\n", stddr);
printf("\nCalories: %lf\n\n", calories);

return 0;
}
 
I wrote this a while back. Compile it into an executable and it can be used at the command prompt to calculate the standard (Aussie Standard) drinks of anything being consumed:


#include <stdio.h>
#include <stdlib.h>

/*Purpose: user to type 3 numbers following program name which will be, in order:
* 1) Number of drinks
* 2) Volume per container
* 3) Percentage of alcohol
*
* upon computing will return the standard drinks consumed.
* The formula for Aussie standard drinks is: Volume(in Litres) * Percentage* 0.789 = number of standard drinks*/

int main(int argc, char *argv[]){

int c; //counter
float argarr[argc]; //always remember arrays start with 0.
float *ptr[4]; // so you'll get a Segmentation fault trying to reference argarr[4] which is null
float calories;


if(argc != 4){ //in addition to program name, 3 arguments must be present and are not

printf("\n\nUsage:\n\n1)Numberofdrinks\n2)ContainerSize(in ml)\n3)Percentage\n\nAll as whole or floating point numbers ie: 6 375 4.5\n");
printf("For 6 375ml Containers @ 4.5%% alcohol each\n\n");
exit(0);
}


for(c=1;c<argc;++c){

argarr[c]=(strtof(argv[c],NULL)); //grabs argv strings and converts/returns argv string to a float
ptr[c]=&argarr[c]; //assigning 3 pointers, 1..3
}

*ptr[2]=(*ptr[2] * .001); //turns whole number into L(ml) percentage for container size, the 2nd argument


float stddr= ((*ptr[1] * (*ptr[2])) * ((*ptr[3])) * 0.789); //formula for calculating Aussie standard drinks
calories=stddr * 70;
printf("\nStandard Aussie drinks: %lf\n", stddr);
printf("\nCalories: %lf\n\n", calories);

return 0;
}
I would have used a modern scripting language, such as Python or even Javascript (which would allow the code to run in a browser on a web page), which is typically more human-readable, easier to write and maintain, and also easier to turn into a web app, but I do also appreciate the occasional C source code :) Once many years ago I set out to create an astrology web application (yes, astrology!) in C++ to calculate the "sidereal time" of a person's birth to aid in more accurate horoscopes. While it was technically interesting, I don't think C++ was a good choice of technology for the project.
 
  • Like
Reactions: Nekodaiden
I would have used a modern scripting language, such as Python or even Javascript (which would allow the code to run in a browser on a web page), which is typically more human-readable, easier to write and maintain, and also easier to turn into a web app, but I do also appreciate the occasional C source code :) Once many years ago I set out to create an astrology web application (yes, astrology!) in C++ to calculate the "sidereal time" of a person's birth to aid in more accurate horoscopes. While it was technically interesting, I don't think C++ was a good choice of technology for the project.

Each to it's uses. I understand the convenience of using online apps, yet at the same time I'm not a big fan of going to the internet for information that can easily be obtained without it. Or for plugging in info that may find it's way to entities I'd rather not give it to. I remember having difficulty in finding such an app that would precisely measure the standard drinks and calories so I decided to just write one myself...the output of which can easily be ported into a file with a few keystrokes at the command line if desired.

I don't consider myself much more than a enthusiast in programing, not much more than barely competent to do simple stuff. I understand the higher level languages come with more ease, but also more overhead in terms of memory as it's automatically taken care of. C is more archaic, can be a pain in the *** when tracking down bugs/memory leaks, but the same features that can make it frustrating also allow the programmer to write programs without a huge amount of overhead, which can be desired if one is writing the code for a larger undertaking like an operating system.