Thread: help with my basic c programming

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    6

    help with my basic c programming

    hey there i found this website im completely new to c programming and i got an assignment due in my class. question is there is 2.54 centimeters in a inch. write a program that ask you to enter your height in inches and then displays your height in centimeters. i have no clue where to go with this. all i have at the moment is
    Code:
    #include <stdio.h>
    int main(){
            const double inches_to_cms = 2.54;
            int centimeters,inches           
            printf("387:\n");
            scanf("%f",&centimeters);
            printf(" 
            return 0;
    }
    i just dont know how to complete the actual question and my book i have i cant find out much about how to program all is gives are definitions. only done up to printf and scanf. any help would be appreicated

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by chris199236 View Post
    hey there i found this website im completely new to c programming and i got an assignment due in my class. question is there is 2.54 centimeters in a inch. write a program that ask you to enter your height in inches and then displays your height in centimeters. i have no clue where to go with this. all i have at the moment is
    Code:
    #include <stdio.h>
    int main(){
            const double inches_to_cms = 2.54;
            int centimeters,inches           
            printf("387:\n");
            scanf("%f",&centimeters);
            printf(" 
            return 0;
    }
    i just dont know how to complete the actual question and my book i have i cant find out much about how to program all is gives are definitions. only done up to printf and scanf. any help would be appreicated
    Welcome to the forum!

    Looks like you need to change your program's input to inches, instead of centimeters.
    write a program that ask you to enter your height in inches and then displays your height in centimeters.
    And then multiply the inches by 2.54. Centimeters = inches * 2.54, right?

    Since the result is almost sure not be an even integer, I would change centimeters to a float or double data type.

    And since we can't be sure the inches will be even either, I'd change it to a float or double, as well. (make it the same data type as centimeters).

    So you need to change some data types, change your input variable, add the bit of arithmetic for the conversion, and print out the final centimeters. Floats use %f as their printf() format:

    Code:
    printf("Height in Centimeters is %f \n",centimeters);

  3. #3
    Registered User
    Join Date
    Sep 2012
    Posts
    6
    thank you for the warm welcome i appreicate it so this is what i got now from what you said
    Code:
     #include <stdio.h>
    int main(){
            const double inches_to_cms = 2.54;
            float inches, centimeters;
            print ("100:\n");
            scanf("%f",&inches);     
            printf("height in centimeters is %f,\n",centimeters);
            return 0;
    }
    is this correct? and if so its this the end or do i have to add more?
    Last edited by chris199236; 09-25-2012 at 07:13 PM.

  4. #4
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Where are you calculating the centimetres?
    Fact - Beethoven wrote his first symphony in C

  5. #5
    Registered User
    Join Date
    Sep 2012
    Posts
    6
    hey click_here thats what i need help with im not sure how to do that

  6. #6
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    is this correct...
    The best way to work that out is to run the code - Use a calculator to work out some expected values and see if your code produces those results.

    Running code and comparing it with expected results is how all good code is written
    Fact - Beethoven wrote his first symphony in C

  7. #7
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    hey click_here thats what i need help with im not sure how to do that
    What don't you understand? What the calculation is, or how to write it in C?
    Fact - Beethoven wrote his first symphony in C

  8. #8
    Registered User
    Join Date
    Sep 2012
    Posts
    6
    like how to write it in c

  9. #9
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    "like how to write it in C", or "how to write it in C" :P

    I think that this will help you
    Introduction to C - Cprogramming.com
    Fact - Beethoven wrote his first symphony in C

  10. #10
    Registered User
    Join Date
    Sep 2012
    Posts
    6
    okay that helped alot thank you for that link but how would i word to get the calculation for inches to centimeters via that?

  11. #11
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Have another look at the examples in the "Reading Input" section.

    Then have a guess - Put it in your code and see if you get the expected output

    If you still have a problem, post what you tried
    Fact - Beethoven wrote his first symphony in C

  12. #12
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    I just posted this on another thread, but it seems applicable here, too.

    Hint: This assignment will require some math.

  13. #13
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by chris199236 View Post
    okay that helped alot thank you for that link but how would i word to get the calculation for inches to centimeters via that?
    To review:

    And then multiply the inches by 2.54. Centimeters = inches * 2.54, right?

  14. #14
    Registered User
    Join Date
    Sep 2012
    Posts
    6
    This is what i tried but i didnt get the answer im just not exactly sure what im suppose to do can u show me the answer then i can figure out how u did it?
    Code:
    #include <stdio.h>
    int main(){
            const double inches_to_cms = 2.54;
            float inches, centimeters;
            print ("100:\n");
            scanf("%f",&inches);
            printf("centimeters=%fx2.54,\n",centimeters);
            return 0;
    }

  15. #15
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Sorry, but we can't do your homework for you.
    Fact - Beethoven wrote his first symphony in C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Basic C programming -1
    By vivekgupta in forum C Programming
    Replies: 29
    Last Post: 06-11-2012, 03:26 PM
  2. Basic C programming
    By vivekgupta in forum C Programming
    Replies: 8
    Last Post: 06-10-2012, 09:57 AM
  3. Basic C programming help
    By jonny0503 in forum C Programming
    Replies: 3
    Last Post: 01-13-2012, 01:00 PM
  4. Basic c programming help
    By benrogers in forum C Programming
    Replies: 12
    Last Post: 01-24-2011, 05:01 PM
  5. basic C-programming Q
    By slyonedoofy in forum C Programming
    Replies: 2
    Last Post: 10-30-2002, 09:54 PM