Thread: I'm I programming right?

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    17

    I'm I programming right?

    Hi,

    I'm learning C from Mark Virtue's cds chapter3 . I have completed the tasks both work as they should but they are very different from Marks soluations. I would to know if there is a problem with the way i'm learning to program?

    My 1st soluation
    Code:
    /*
     * input frist name
     * input second name 
     * output fullname with a space 
     * output the length of the name including the space 
     */ 
    
    #include <stdio.h>
    main()
    {
             char	name[15];
             char	s_name[15];
             int    length;
             
             printf("Enter your first name: ");
       	     scanf("%s", name);
       	     printf("Enter your second name: ");
       	     scanf("%s", s_name);
       	     printf("Your name is %s %s\n", name, s_name); 
             
                 length = strlen("%[^\n] %s %s"); 
                 printf("The length of this string >>%s %s<< is %d characters\n", 
                                                             name, s_name, length);	     
                 fflush(stdin);
          	     getchar();
    }
    Marks Soluation
    Code:
    /*
     *  fullname.c
     *
     *  Solution to the name exercise from Chapter 3.
     *  Ask for a user's first name and surname as two separate questions
     *  and output the result as a concatenated string (plus the length of the
     *  name).
     *
     *  by Mark Virtue, 2001.
     */
    #include <stdio.h>
    
    main()
    {
        char    first[31];
        char    sur[31];
        char    result[62];
    
        /*
         *  First, get the names from the keyboard
         */
        printf("Please enter your FIRST name: ");
        scanf("%[^\n]", first);
        printf("Please enter your LAST name:  ");
        fflush(stdin);
        scanf("%[^\n]", sur);
    
        /*
         *  Concatenate them together into "result" with a space between
         */
        strcpy(result, first);
        strcat(result, " ");
        strcat(result, sur);
    
        /*
         *  Output the result
         */
        printf("Your FULL name is %s (length %d)\n", result, strlen(result));
    
        fflush(stdin);
        getchar();
    }
    My 2nd Soluation
    Code:
    /*
     * Celsius to Fahreneit
     * 
     */ 
    
    #include <stdio.h>
    
    main()
    {
          float cel;
          double fah;
                
          printf("Enter a Celsius to convert to Fahreneit: ");
          scanf("%f", &cel);
          fah = cel * 9 /5 + 32;
          
          printf("Fahreneit = %.2f\n", fah);
         
          fflush(stdin);
          getchar();
    }
    Marks Soluation
    Code:
    /*
     *  temperature.c
     *
     *  Solution to the Celsius to Fahrenheit exercise from Chapter 3.
     *  Ask for a temperature in degrees Celsius and output the corresponding
     *  Fahrenheit value to two decimal places.
     *
     *  by Mark Virtue, 2001.
     */
    #include <stdio.h>
    
    main()
    {
        int     celsius;
        double  fahr;
    
        /*
         *  First, get the Celsius value from the keyboard
         */
        printf("Please enter a temperature in degrees Celsius: ");
        scanf("%d", &celsius);
    
        /*
         *  Perform the conversion  F = 32 + C * 9/5
         */
        fahr = 32 + (double)celsius * 9 / 5;
    
        /*
         *  Output the result
         */
        printf("%d degrees Celsius = %.2f degrees Fahrenheit\n",
                            celsius, fahr);
    
        fflush(stdin);
        getchar();
    }
    Thanking you in advance for any of your time helping me.


    Nurofen
    Last edited by nurofen; 03-15-2008 at 10:03 AM. Reason: character missing from title

Popular pages Recent additions subscribe to a feed