Thread: Help me improve my code!

  1. #1
    wise_ron wise_ron's Avatar
    Join Date
    May 2006
    Posts
    75

    Help me improve my code!

    Iam new to c programming and i would like anyone suggestion in how to improve this code to do the fallowing program description. If you see any mistakes feel free to tell me.

    Thanks

    wise_ron

    Code:
    */
      Description: Write a program that prints your name, age and a paragraph about you.
    */
    
    #include<stdio.h>
    main()
    {
          char name[15]="caleb ortega";
          int age = 21;
          printf("My name is %s and iam %d years old\n",name,age);
          printf("A paragraph about me:\n");
          printf("I like to play Sports, Guitar and listen to music\n");        
          system("pause");
    return 0;
     }

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    */
      Description: Write a program that prints your name, age and a paragraph about you.
    */
    Your comment is wrong; comments start with /* and end with */.
    Code:
    char name[15]="caleb ortega";
    Well, rather than count the number of characters in the string and risk getting it wrong, if you initialize the string to a value, you can get the compiler to do it for you:
    Code:
    char name[] = "caleb ortega";
    This
    Code:
    main()
    should probably be
    Code:
    int main()
    http://faq.cprogramming.com/cgi-bin/...&id=1043284376

    And your indentation could use some working on; the return statement isn't indented, and the closing brace is.

    As for system("pause") . . . http://faq.cprogramming.com/cgi-bin/...&id=1043284385 . . . you might want to use a simple getchar() for now.

    Here's the program with my suggested modifications:
    Code:
    /*
      Description: Write a program that prints your name, age and a paragraph about you.
    */
    
    #include<stdio.h>
    int main()
    {
          char name[]="caleb ortega";
          int age = 21;
          printf("My name is %s and iam %d years old\n",name,age);
          printf("A paragraph about me:\n");
          printf("I like to play Sports, Guitar and listen to music\n");        
          getchar();
          return 0;
    }
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    wise_ron wise_ron's Avatar
    Join Date
    May 2006
    Posts
    75

    Changes to the program

    Thanks DWKS, I have change what you told me and thanks for your recommendations.

    I have improve the code and added some more interesting stuff. Thank you all hope to receive more comments.

    Code:
    /* Description: Write a program that prints your name, age and a 
                   paragraph about you.
                   Yout age and name should be decleared as variables. Use the age 
                   variable to calculate your age in months and days.
    */
    
    #include<stdio.h>
    int main()
    {
          char name[]="Caleb 0rtega";
          int age = 49;
          int age_in_months = age*12;
          int age_in_days = age*365;
          printf("My name is %s and I am %d years old.\n",name,age);
          printf("My age in Months is: %d months.\n",age_in_months);
          printf("My age in Days is: %d days\n\n",age_in_days);
          printf("A paragraph about me:\n");
          printf("I like to play Sports, Guitar and listen to music\n");        
          getchar();
    return 0;
    }
    Wise_ron
    One man's constant is another man's variable

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Code:
    int age_in_days = age*365;
    What about leap years?
    If you understand what you're doing, you're not learning anything.

  5. #5
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    int age_in_days = age*365.25

    Is how I have always calc'd it... since that is how long a year truly is (if I remember my science classes right).

    EDIT: I know that any extra stuff after the decimal gets truncated, but most times people care more about the whole days rather than the parts of days...

  6. #6
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Quote Originally Posted by Wraithan
    int age_in_days = age*365.25
    Wouldn't you have to do something like:
    int age_in_days = int((float) age * 365.25f)
    to make that work right?

  7. #7
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Quote Originally Posted by Kennedy
    Wouldn't you have to do something like:
    int age_in_days = int((float) age * 365.25f)
    to make that work right?
    No. the compiler will automagically promote age into a float or double for the multiplication and then when it's assigned to the age_in_days variable the fraction will just be truncated.

    It's actually closer to 365.24 days per year though

    According to google: http://www.google.com/search?hl=en&q=1+year+in+days
    Last edited by itsme86; 09-14-2006 at 02:46 PM.
    If you understand what you're doing, you're not learning anything.

  8. #8
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Quote Originally Posted by itsme86
    No. the compiler will automagically turn age into a float or double for the multiplication and then when it's assigned to the age_in_days variable the fraction will just be truncated.
    Strange. . . I had to do that in Dev-C++ since I wasn't getting what I was expecting. . . I'll have to go back and check that out.

    Is that C99 or just the way most compilers do it?

  9. #9
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    Bah, it has been years since my last science class. For the intents and purposes of this class assignment I should think that every 4 years should have an extra day, since that is how the actual calender works. Due to that, 4*365.24 would not give you the extra day for the leap year. besides, .25 makes for cleaner numbers and we have only put on an addition .04(approx) days in the last 2006 years.

    Anyway... this is getting OT

  10. #10
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Most compilers would give you a warning to the effect that you're assigning a floating point number to an integral variable. Usually you need a cast of some kind.

    If you want to round a number, add .5 to it:
    Code:
    int roundnumber(double d) {
        return (int)(d + .5);
    }
    [edit]
    the compiler will automagically promote age into a float or double
    There's nothing magical about it. [/edit]
    Last edited by dwks; 09-14-2006 at 03:03 PM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  11. #11
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Quote Originally Posted by Wraithan
    since that is how the actual calender works.
    Which calendar? The leap year rule for the calendar in widespread use today is a little more complicated than that.

    From wikipedia:
    The Gregorian calendar, the current standard calendar in most of the world, adds a 29th day to February in all years evenly divisible by 4, except for centennial years (those ending in -00), which receive the extra day only if they are evenly divisible by 400. Thus 1600, 2000 and 2400 are leap years but 1700, 1800, 1900 and 2100 are not.
    If you understand what you're doing, you're not learning anything.

  12. #12
    wise_ron wise_ron's Avatar
    Join Date
    May 2006
    Posts
    75

    Thanks for you suggestions

    I learn a lot about the years and how to work with them, for this program.

    Everyone thanks and have a nice day
    Wise_ron
    One man's constant is another man's variable

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Explain this C code in english
    By soadlink in forum C Programming
    Replies: 16
    Last Post: 08-31-2006, 12:48 AM
  2. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  3. How to improve code
    By rugby in forum C Programming
    Replies: 3
    Last Post: 04-15-2003, 09:24 AM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. help improve my code
    By lambs4 in forum C Programming
    Replies: 3
    Last Post: 11-21-2001, 11:33 AM