[Courses] [C] Beginner's Lesson Four: K's Answers to Exercises

KWMelvin kwmelvin at intrex.net
Sat Oct 12 15:49:21 EST 2002


Greetings,

These are MY answers to the exercises for Lesson Four.  If you
haven't done those exercises yourself, then you may not want
to read any further, so as not to spoil your own solution.  If
you are stuck on how to solve a problem, and want to see my answer,
then read on.  There is always more than one way to do something.
These answers are certainly not the definitive answers for any
of the exercises... remember, I am learning this stuff along
with everyone else. I have tested these programs on my machine,
and they work fine.  However, your mileage may vary ........... 
if your machine blows up while running any of these programs, 
it is NOT my fault! <== Standard Disclaimer. 8^D

1. /* ex4-1.c -- convert degress C to degrees F */
   /* formula: F = (9 / 5) * C + 32             */
   #include <stdio.h>
   int main(void)
   {
      float degF = 0.0;   /* degrees Fahrenheit */
      float degC = 0.0;   /* degrees Centigrade */
      char line[80];    /* line for input     */

      /* INPUT */
      printf("Enter degrees Centigrade: ");
      fgets(line, sizeof(line), stdin);
      sscanf(line, "%f", &degC);

      /* PROCESS */
      degF = (9 / 5) * degC + 32;

      /* OUTPUT */
      printf("Degrees Fahrenheit: %f\n", degF);

      return 0;
   }

In ex4-1.c, I could combine the process and output in one statement:
   printf("Degrees F: %f\n", degF=(9/5)*degC+32);
but I think breaking it down like I did, makes it easier to understand.

2. /* ex4-2.c -- calculate the volume of a sphere */
   /* formula: V = (4 / 3) * r^3                  */
   #include <stdio.h>
   int main(void)
   {
     char line[80];       /* line input       */
     float radius = 0.0;  /* radius of sphere */
     float volume = 0.0;  /* volume of sphere */

     printf("*** Calculate the volume of a sphere ***");

     /* INPUT */
     printf("Enter radius of sphere: ");
     fgets(line, sizeof(line), stdin);
     sscanf(line, "%f", &radius);

     /* PROCESS */
     volume = ((4 / 3) * (radius * radius * radius));

     /* OUTPUT */
     printf("Volume of sphere: %f\n", volume);

     return 0;
   }

3. /* ex4-3.c -- output perimeter of rectangle  */
   /* formula: perimeter = 2 * (width + height) */
   #include <stdio.h>
   int main(void)
   {
     char line[80];    /* line input             */
     float perim = 0;  /* perimeter of rectangle */
     float width = 0;  /* width of rectangle     */
     float height = 0; /* height of rectangle    */

     /* INPUT */
     printf("Enter rectangle width: ");
     fgets(line, sizeof(line), stdin);
     sscanf(line, "%f", &width);
     printf("Enter rectangle height: ");
     fgets(line, sizeof(line), stdin);
     sscanf(line, "%f", &height);

     /* PROCESS */
     perim = 2 * (width + height);

     /* OUTPUT */
     printf("Rectangle perimeter: %f\n", perim);

     return 0;
   }

4. /* ex4-4.c -- convert M/hr to Km/hr        */
   /* formula: miles = kilometers * 0.6213712 */
   #include <stdio.h>
   int main(void)
   {
     char line[80];  /* line input            */
     float miles;    /* miles per hour        */
     float kilom;    /* kilometers per hour   */

     /* INPUT */
     printf("Enter miles: ");
     fgets(line, sizeof(line), stdin);
     sscanf(line, "%f", &miles);

     /* PROCESS */
     kilom = miles / 0.6213712;

     /* OUTPUT */
     printf("Kph: %f\n", miles);

     return 0;
   }

5. /* ex4-5.c -- input hours & minutes, output minutes */
   /* example: 1 hour 30 min = 90 minutes              */
   #include <stdio.h>
   int main(void)
   {
     char line[80];  /* line input */
     int hour;     /* hours      */
     int mins;     /* minutes    */
     int total;    /* total minutes */
 
     /* INPUT */
     printf("Enter hours: ");
     fgets(line, sizeof(line), stdin);
     sscanf(line, "%f", &hour);
     printf("Enter minutes: ");
     fgets(line, sizeof(line), stdin);
     sscanf(line, "%f", &mins);

     /* PROCESS */
     total = (hour * 60) + mins;

     /* OUTPUT */
     printf("%d hour(s) %d minutes = %d minutes\n",
             hour,      mins,        total); 

     return 0;
   }

6. /* ex4-6.c -- input minutes, output hours & minutes */
   /* example: 90 minutes = 1 hour 30 min       */
   #include <stdio.h>
   int main(void)
   {
     char line[80]; /* line input */
     int hour;      /* hours      */
     int mins;      /* minutes    */
     int remain;    /* remainder of minutes */
 
     /* INPUT */
     printf("Enter minutes: ");
     fgets(line, sizeof(line), stdin);
     sscanf(line, "%f", &mins);

     /* PROCESS */
     hour = mins / 60;    /* get total hours          */
     remain = mins % 60;  /* get remainder of minutes */     

     /* OUTPUT */
     printf("%d minutes = %d hour(s) %d minutes\n",
             mins,        hour,      remain); 

     return 0;
   }

7. Write a program that gets the user's first name as input,
   and outputs the number of letters in their first name.
   Example: Enter your first name: Bonnie
            There are 6 letters in your first name.       

   Answer below (scroll down).
   ~
   ~
   ~
   ~
   ~
   ~
   ~
   ~
   ~
   ~
   ~
   /* ex4-7.c -- count letters in first name */
   #include <stdio.h>
   int main(void)
   {
     char line[100];  /* line input */
     int first;

     /* INPUT */
     printf("Enter your first name: ");
     fgets(line, sizeof(line), stdin);

     /* PROCESS */
     first = strlen(line) - 1;

     /* OUTPUT */
     printf("There are %d letters in your first name.\n", first);

     return 0;
   }

Happy Programming!
--
K




More information about the Courses mailing list