Added book examples
This commit is contained in:
43
C10/function_example.c
Normal file
43
C10/function_example.c
Normal file
@@ -0,0 +1,43 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <math.h>
|
||||
|
||||
// Define HOW the function is to be used, code comes later
|
||||
float CalculateSurfaceAreaOfCylinder ( float R, float L );
|
||||
|
||||
// Function to calculate the volume of a cylinder
|
||||
float CalculateVolumneOfCylinder ( float R, float L )
|
||||
{
|
||||
// Calculate \& return value
|
||||
float Result;
|
||||
Result = (M_PI * R * R * L);
|
||||
return (Result);
|
||||
}
|
||||
int main(void)
|
||||
{
|
||||
// Declare variables
|
||||
float r, l, SurfaceArea, Volume;
|
||||
|
||||
// Obtain values
|
||||
printf("\nPlease enter the radius");
|
||||
scanf("%f", &r);
|
||||
|
||||
printf("\nPlease enter the length");
|
||||
scanf("%f", &l);
|
||||
|
||||
// Get and display the volume
|
||||
Volume = CalculateVolumneOfCylinder(r, l);
|
||||
printf ("\nThe volume is %f", Volume);
|
||||
|
||||
// Get and display the surface area
|
||||
SurfaceArea = CalculateSurfaceAreaOfCylinder(r, l);
|
||||
printf ("\nThe surface area is %f", SurfaceArea);
|
||||
|
||||
return 0;
|
||||
}
|
||||
// Calculate the surface areas of a cylinder
|
||||
float CalculateSurfaceAreaOfCylinder ( float R, float L )
|
||||
{
|
||||
// Calculate \& return value
|
||||
return (2 * M_PI * R * R ) + ( 2 * M_PI * R * L);
|
||||
}
|
38
C10/void_function_example.c
Normal file
38
C10/void_function_example.c
Normal file
@@ -0,0 +1,38 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
// Define HOW the function is to be used, code comes later
|
||||
void DisplayDayOfTheWeek ( int Day );
|
||||
|
||||
int main(void) // Execution starts here
|
||||
{
|
||||
int d; // Declare variable
|
||||
|
||||
// Obtain values
|
||||
printf("\nPlease enter a number betwwen 0 and 6");
|
||||
scanf("%d", &d);
|
||||
|
||||
// Use a function to display the day of the week
|
||||
DisplayDayOfTheWeek(d);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Function to display day of week - nothing is returned
|
||||
void DisplayDayOfTheWeek ( int Day )
|
||||
{
|
||||
// Display date based on value
|
||||
// Case values on one line as easier to cut/paste :-)
|
||||
switch (Day)
|
||||
{
|
||||
case 0 : printf ("Sunday") ; break;
|
||||
case 1 : printf ("Monday") ; break;
|
||||
case 2 : printf ("Tuesday") ; break;
|
||||
|
||||
/* etc. for other days of the week */
|
||||
|
||||
default:
|
||||
printf ("Invaid day provided");
|
||||
}
|
||||
return; // No value needed as the return type is void
|
||||
}
|
Reference in New Issue
Block a user