Like Tree1Likes

Grade my first lab? (and program :D)

This is a discussion on Grade my first lab? (and program :D) within the C Programming forums, part of the General Programming Boards category; So here is what I plan to hand in to my professor, he requires comments so many of those comments ...

  1. #16
    Registered User
    Join Date
    Feb 2012
    Location
    Winnetka, California, United States
    Posts
    15
    So here is what I plan to hand in to my professor, he requires comments so many of those comments are "duh" comments but I felt i had to include them.

    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <math.h>
    
    
    int main ()
    {
        int side1, side2, side3;                
        double semiperimeter; 
        double    area;                                                                                                                
            do{
               printf("Please enter three sides to a triangle. \n"); 
               scanf("%d %d %d", &side1, &side2, &side3);
               semiperimeter = (double)(side1 + side2 + side3) / 2;      /* Using (side1 + side2 + side3)/2.0 can be subsituted for the word double */
               area = sqrt(semiperimeter * (semiperimeter - side1) * (semiperimeter - side2) * (semiperimeter - side3));
             
               if (side1 + side2 > side3 && side2 + side3 > side1 && side1 + side3 > side2)    /* Using Heron's law to determine whether inputed numbers are a triangle */
                {    
                    if ((side1 == side2) && (side2 == side3))
                        printf("\nThis triangles sides are %d,%d,%d and is an equalateral triangles.\nIts area is %.05f.\n\n", side1, side2, side3, area);
                    else if ((side1 == side2) || (side2 == side3) || (side1 == side3))
                        printf("\nThis triangles sides are %d,%d,%d and is an Isosceles triangle.\nIts area is %.05f.\n\n", side1, side2, side3, area);   /* If heron's law is passed, each statement is checked to give correct triangle */
                    else 
                           printf("\nThis triangles sides are %d,%d,%d and is a Scalene triangle.\nIts area is %.05f.\n\n", side1, side2, side3, area);
                }
               else
                   printf("\n%d,%d,%d are not valid sides to a triangle.  This program will now terminate.\n", side1, side2, side3); /* Will end the program once user inputs an incorrect combination of numbers. */
            }while ((side1 + side2 >side3) && (side2 + side3 >side1) && (side1 + side3 > side2));   /* If it passes Heron's law it will keep looping, if not program terminates. */
        
            getch(); /* Allows user to press a character before program exits. */                                                        
            return 0;
    }

  2. #17
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,425

    you would probably earn a brownie point or two if you zeroed your ints and doubles before requesting user input, that way you have a known value to check against, either in your head, or through further input validation.. and writing a simple comment to say //init values.
    Regarding your 'duh' comments you might just get rid of the ones that explain the opposite of what has already been said on the line before, or make that a very simple one. Its well accepted that outlining the reason for a condition a programmer can read into a subsequent 'reverse' statement occuring soon after..., i think you see that already though, but it will help clean up the code.
    Last edited by rogster001; 02-21-2012 at 03:17 PM.
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  3. #18
    Registered User
    Join Date
    Feb 2012
    Location
    Winnetka, California, United States
    Posts
    15
    Quote Originally Posted by rogster001 View Post
    you would probably earn a brownie point or two if you zeroed your ints and doubles before requesting user input, that way you have a known value to check against, either in your head, or through further input validation.. and writing a simple comment to say //init values.
    Regarding your 'duh' comments you might just get rid of the ones that explain the opposite of what has already been said on the line before, or make that a very simple one. Its well accepted that outlining the reason for a condition a programmer can read into a subsequent 'reverse' statement occuring soon after..., i think you see that already though, but it will help clean up the code.
    my knowledge is still very minimal in programming so far, so my question is wouldnt it already be assumed that that ints and doubles are zeroed because they have no value yet? Or I guess a better question would be what does it mean to zero a int or double before they input the value? Thank you for the advice .

  4. #19
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    Quote Originally Posted by Cowmoogun View Post
    my knowledge is still very minimal in programming so far, so my question is wouldnt it already be assumed that that ints and doubles are zeroed because they have no value yet? Or I guess a better question would be what does it mean to zero a int or double before they input the value? Thank you for the advice .
    They can contain absolutely anything before initialized. So, any assumption would likely be wrong. (..except for global variables.)
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  5. #20
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    One thing I've noticed about triangles over the years is that they don't always have integral sides. Are you sure you want to represent the sides as ints?

    Also, I would put the comments on a line by themselves so you don't have lines that are 200+ chars long!
    MK27 likes this.

  6. #21
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,425
    Or I guess a better question would be what does it mean to zero a int or double before they input the value?
    It means to assign a value of 0 as a default, it does not only apply when user input is involved, it is a general good coding rule.

    Of course in some cases you would use a different number, depending on what you are doing, like

    Code:
    int startX = START_X;   //where START_X is a pre defined constant value.
    But if no other special value will be used the convention is to just assign 0.

    But in either example the idea is always initialise your vars before you go using them in expressions etc, otherwise you will find yourself bug hunting a lot more, and then bashing your head against the keyboard when you realise what the problem was.

    Try just printing out the values of a few uninitialised variables and you will see what i mean.
    Last edited by rogster001; 02-22-2012 at 06:43 AM.
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  7. #22
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Cowmoogun View Post
    So here is what I plan to hand in to my professor, he requires comments so many of those comments are "duh" comments but I felt i had to include them.
    IMO you don't have enough comments if this is an academic exercise. You do explain the logic, but you should also annotate all the variables:

    Code:
        /* what side1, side2, side3 are for */
        int side1, side2, side3;
        /* what semiperimeter is for */                
        double semiperimeter; 
        /* what area is for */
        double area;
    This might involve repetition in terms of explaining the logic, but that is a good thing, even if it seems redundant to you, because for someone who has never seen it or your comments before, having something repeated, particularly in slightly different terms, makes it that much clearer what your intent is. Understanding your intent is particularly important if in fact the code is wrong, and someone who's job it is to correct you will appreciate that.

    By "slightly different terms" I mean that
    1) the variable notes describe possible states involved,
    2) the running commentary on logic describes behavior.

    It also doesn't hurt to include a preliminary paragraph at the top of each function (in this case, there is just main()) explaining what this function does and a bit about how. Again, that may seem redundant but it is a good practice.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Please Help Grade Curving program!!!
    By phil12 in forum C++ Programming
    Replies: 5
    Last Post: 04-07-2011, 09:23 PM
  2. Grade Program
    By Jbrubin in forum C Programming
    Replies: 2
    Last Post: 03-28-2011, 02:02 PM
  3. Replies: 2
    Last Post: 01-29-2011, 11:58 AM
  4. Student Grade Program
    By Vinod Menon in forum C Programming
    Replies: 1
    Last Post: 05-31-2004, 12:32 AM
  5. Student Grade Program
    By Vinod Menon in forum C Programming
    Replies: 5
    Last Post: 05-28-2004, 02:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21