C reference code

Discussion in 'Software' started by Wookie, Sep 11, 2004.

  1. Wookie

    Wookie Sergeant Major

    Im in intro to C right now and for the benefit of members wanting to learn the language I will post my assignments, they are made up by the teacher so im sure they will not be the same as anyone with the same class. Feel free to sticky this. Or tell me to shutup if you don't want it up :)

    Any questions about the code post them.

    Here's my first assignment. The comments should explain what it does

    Code:
    /***************************************************************************
     First assignment for intro to C By WookMaster
     Compiled with GCC under Linux using the Kdevelop IDE for project management
     For question email [email]wookmaster@techdefine.com[/email] or visit my website
     [url]www.techdefine.com[/url]
     ***************************************************************************/
    
    #ifdef HAVE_CONFIG_H
    #include <config.h>
    #endif
    
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void)
    {
      	const double gallons_per_hour = 0.3; //Gallons per hour as given by assignment
    	const double pi = 3.14159;           //The constant value of PI
    	const double cc_per_gallon = 3785.4; //CC per gallon as defined by assignment
    	double diameter = 30;		     //The diameter of the gas can
    	double radius = diameter/2;	     //Dividing by 2 give the radius of the can
    	double height = 45;	             //The height of the can	
    	double volume;			     //A variable to be used to hold the volume of the can when computed
    	double gallons;			     //A variable to hold the answer to how many gallons the can holds
    	double total_time;		     //The variable to hold our final answer
        	/*Calculates the volume*/
    	volume = ((pi) * (radius * radius)) * height;
    	/*Turns the Volume into Gallons*/
    	gallons = volume * cc_per_gallon;
    	/*Calculates the total time the machine will Run*/
    	total_time = gallons * gallons_per_hour;
    	/*Prints out the answer*/
    	printf("The total time the machine will run is: %6.2f\n", total_time);
    
      return 0;
    }
    
     
  2. Kodo

    Kodo SNATCHSQUATCH

    it's cool man.. keep posting.
     
  3. Vlad902

    Vlad902 Guest

    Why do you use autconf when you don't use any of it's features?
     
  4. Wookie

    Wookie Sergeant Major

    Kdevelop put it there and I forgot to remove it :)
     
  5. Maxwell

    Maxwell Folgers

    To improve the quality, I would suggest that all your constants are made double precision rather than derived single precision or integer constants to prevent any loss of precision on calculations. Similarly your use of %6.2f for a double precision variable may produce a machine dependent result. There exists a constant pi in <math.h> or alternatively calculate it from 4 times arc tan of 1 radian. The printf function returns a value by default, what are doing with this value or if nothing cast the result to (void). Code formatting should be consistent, e.g., indentation. What units are your calculations in, imperial or metric - I would guess imperial.

    Finally, what would you do if the constants were much larger or smaller and result in an overflow or underflow in result? etc.
     
  6. Vlad902

    Vlad902 Guest

    What exactly is the supposed purpose of doing this? Sounds like ANSI stuff (can't be pre-ANSI due to the fact that it's void) but I've never really seen a point to this and -ansi -W -Wall -pedantic never complains about it. Personally I'm not a big fan of it as it just clutters space.
     
  7. Robster12

    Robster12 The Horse Whisperer

    /***** Excuse me, but... I just wanted to say this is going to be a ***/
    /***** Great thread for me, as I am trying to learn this stuff! (ThankYou all!) */
     
  8. Wookie

    Wookie Sergeant Major


    I'd probably change some stuff, your asking theoretical questions involving the code being different than it is. It works fine like this, theres something my teacher doesnt like but I havnt gotten to talk to him yet, Im hoping my math isnt off, I don't have the math memorized I looked it up online.


    New assignment coming soon involving getting user inputted values.
     
  9. Wookie

    Wookie Sergeant Major

    whoops, math was wrong

    Heres the corrected code for the first assignment
    Code:
    /* First Assignment Intro to C WookMaster */
    
    #include <stdio.h>
    
    int main (void)
    {
    	const double gallons_per_hour = 0.3;
    	const double pi = 3.14159;
    	const double cc_per_gallon = 3785.4;
    	double diameter = 30;
    	double radius = diameter/2;
    	double height = 45;
    	double volume;
    	double gallons;
    	double total_time;
        /*Calculates the volume*/
    	volume = ((pi) * (radius * radius)) * height;
    	/*Turns the Volume into Gallons*/
    	gallons = (volume / cc_per_gallon);
    	/*Calculates the total time the machine will Run*/
    	total_time = (gallons / gallons_per_hour);
    	/*Prints out the answer*/
    	printf("The total time the machine will run is: %6.2f\n", total_time);
    return 0;
    }
    	
    
    
    
     
  10. Wookie

    Wookie Sergeant Major

    The next version of this program, it uses scanf to get the user data for the size of the gas can


    Code:
    /*Intro to C By WookMaster
    This accepts user input and calculates the amount of time in Hours a 
    generator will run on a full tank of gas depending on the gas can dimensions.*/
    
    #include <stdio.h>
    
    int main (void)
    {
    	const double gallons_per_hour = 0.3;       //Initalizes a Constant value
    	const double pi = 3.14159;			       //Initalizes a Constant value
    	const double cc_per_gallon = 3785.4;       //Initalizes a Constant value
    	double diameter = 0;                       //Initilizes a variable
    	double height = 0;						   //Initilizes a variable
    	double volume = 0;						   //Initilizes a variable
    	double gallons = 0;						   //Initilizes a variable
    	double total_time = 0;					   //Initilizes a variable
    	
     
    	printf("Please Input the height of the Can: " ); // Prints the preceding statement
    	scanf("%lf", &height);		//Accepts Input for the Height of the can
    	printf("\nPlease Input the Diameter of the can: ");
    	scanf("%lf", &diameter);  //Accepts user input for the diameter
    	double radius = diameter/2;   //Computes the Radius of the can at the same time initilizing a variable.
    	volume = ((pi) * (radius * radius)) * height;  // Computes the volume of the can
    	gallons = (volume / cc_per_gallon); //Computes the gallons that are in the can
    	total_time = (gallons / gallons_per_hour);  // Calculates the hours the machine will run in hours.
    	printf("The total time the machine will run is: %6.2f\n", total_time); //Print the answer
    return 0;
    }
    
    Ive been told by declaring radius in the middle im using habits from my C++ coding, so FYI Its not stylin in C to do that. :)
     
  11. Vlad902

    Vlad902 Guest

    It's not only "not stylin" but it will also not compile on any compiler that is pre-ansi, ansi, c89, and most likely everything else but c99.
     
  12. Wookie

    Wookie Sergeant Major

    compiled just fine under GCC.....
     
  13. rmStar-R

    rmStar-R Private E-2

    i think it's not just style. it's rather good to follow conventions in programming style. question: does that convention make the code more efficient? at least for c language... that i don't know. hehe.
     
  14. GregoryDalton

    GregoryDalton Private E-2

    Does this convention/rule stem from the fact that C is not quite as dynamic as C++. Or at least in the early days of C compilers things weren't as dynamic as modern compilers.

    I mean it makes sense from that point of view for a compiler to force a function to declare all the heap memory it is going to need for its life time right at the beginning... Makes sense to me at least... That way the compiler can allocate it its memory and then worry about where the next function is and how much memory that needs.

    Cheers,
    Greg
     
  15. Vlad902

    Vlad902 Guest

    gcc supports c99, though I'm not sure if it would by default


    By a very very very small amount on compilers that dont optimize it, yes. But not enough that it'd make a visible difference if ran a billion times.


    C has no knowledge of a heap, stack, or other "standard" data structure. That over, automatic declarations are not on the heap, that's dynamic and is not the exact same as this case. The heap usually stores static declarations and dynamic declarations usually done by an sbrk syscall to expand the heap. malloc and free depend on the heap. int foo; char bar; double baz; does not. And at that, if they did, there'd always be realloc.


    There's no reason why a compiler should think about the next functions prerequisites. If it did, then there'd be an awful mess with branch prediction, function pointers, and signals. Each function should just allocate it's own memory and deallocate (sub esp, 0xf00 and add esp, 0xf00 aren't very large speed hits) by itself.
     
  16. rmStar-R

    rmStar-R Private E-2

    Now, that's an info! Great! :cool:

    Thanks vlad.
     

MajorGeeks.Com Menu

Downloads All In One Tweaks \ Android \ Anti-Malware \ Anti-Virus \ Appearance \ Backup \ Browsers \ CD\DVD\Blu-Ray \ Covert Ops \ Drive Utilities \ Drivers \ Graphics \ Internet Tools \ Multimedia \ Networking \ Office Tools \ PC Games \ System Tools \ Mac/Apple/Ipad Downloads

Other News: Top Downloads \ News (Tech) \ Off Base (Other Websites News) \ Way Off Base (Offbeat Stories and Pics)

Social: Facebook \ YouTube \ Twitter \ Tumblr \ Pintrest \ RSS Feeds