move exercises and lectures into subfolders
This commit is contained in:
19
Exercises/C10/ex2.c
Normal file
19
Exercises/C10/ex2.c
Normal file
@@ -0,0 +1,19 @@
|
||||
#include <stdio.h>
|
||||
#include <math.h>
|
||||
|
||||
|
||||
float degrees_to_radians(float degrees) {
|
||||
return M_PI * degrees / 180.0;
|
||||
}
|
||||
|
||||
int main() {
|
||||
float input, output;
|
||||
printf("Enter an angle in degrees: ");
|
||||
scanf("%f", &input);
|
||||
|
||||
output = degrees_to_radians(input);
|
||||
|
||||
printf("%f radians\n", output);
|
||||
|
||||
return 0;
|
||||
}
|
36
Exercises/C10/ex3.c
Normal file
36
Exercises/C10/ex3.c
Normal file
@@ -0,0 +1,36 @@
|
||||
#include <stdio.h>
|
||||
#include <math.h>
|
||||
|
||||
|
||||
float degrees_to_radians(float degrees) {
|
||||
return M_PI * degrees / 180.0;
|
||||
}
|
||||
|
||||
|
||||
int main() {
|
||||
int rc, start, end;
|
||||
|
||||
printf("Start: ");
|
||||
rc = scanf("%d", &start);
|
||||
if (rc != 1) {
|
||||
printf("Please enter a real integer\n");
|
||||
return 1;
|
||||
}
|
||||
printf("End: ");
|
||||
rc = scanf("%d", &end);
|
||||
if (rc != 1) {
|
||||
printf("Please enter a real integer\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (start > end) {
|
||||
printf("Please ensure that start value is smaller than end value\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
for (int i = start; i <= end; i++) {
|
||||
printf("%d %f\n", i, degrees_to_radians((float)i));
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
42
Exercises/C10/ex4.c
Normal file
42
Exercises/C10/ex4.c
Normal file
@@ -0,0 +1,42 @@
|
||||
#include <stdio.h>
|
||||
|
||||
int date_to_day(int year, int month, int day) {
|
||||
if (month < 3) {
|
||||
month += 12;
|
||||
year -= 1;
|
||||
}
|
||||
|
||||
return ((13*month+3)/5 + day + year + year/4 - year/100 + year/400) % 7;
|
||||
}
|
||||
|
||||
int main() {
|
||||
int rc, year, month, day, nd;
|
||||
|
||||
printf("Enter a day (yyyy-mm-dd): ");
|
||||
rc = scanf("%d-%d-%d", &year, &month, &day);
|
||||
if (rc != 3) {
|
||||
printf("Failed to parse date\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
nd = date_to_day(year, month, day);
|
||||
|
||||
switch (nd) {
|
||||
case 0:
|
||||
printf("Monday\n"); break;
|
||||
case 1:
|
||||
printf("Tuesday\n"); break;
|
||||
case 2:
|
||||
printf("Wednesday\n"); break;
|
||||
case 3:
|
||||
printf("Thursday\n"); break;
|
||||
case 4:
|
||||
printf("Friday\n"); break;
|
||||
case 5:
|
||||
printf("Saturday\n"); break;
|
||||
case 6:
|
||||
printf("Sunday\n"); break;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
45
Exercises/C10/ex5.c
Normal file
45
Exercises/C10/ex5.c
Normal file
@@ -0,0 +1,45 @@
|
||||
#include <stdio.h>
|
||||
|
||||
int date_to_day(int year, int month, int day) {
|
||||
if (month < 3) {
|
||||
month += 12;
|
||||
year -= 1;
|
||||
}
|
||||
|
||||
return ((13*month+3)/5 + day + year + year/4 - year/100 + year/400) % 7;
|
||||
}
|
||||
|
||||
void print_day_of_week(int day) {
|
||||
switch (day) {
|
||||
case 0:
|
||||
printf("Monday\n"); break;
|
||||
case 1:
|
||||
printf("Tuesday\n"); break;
|
||||
case 2:
|
||||
printf("Wednesday\n"); break;
|
||||
case 3:
|
||||
printf("Thursday\n"); break;
|
||||
case 4:
|
||||
printf("Friday\n"); break;
|
||||
case 5:
|
||||
printf("Saturday\n"); break;
|
||||
case 6:
|
||||
printf("Sunday\n"); break;
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
int rc, year, month, day, nd;
|
||||
|
||||
printf("Enter a day (yyyy-mm-dd): ");
|
||||
rc = scanf("%d-%d-%d", &year, &month, &day);
|
||||
if (rc != 3) {
|
||||
printf("Failed to parse date\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
nd = date_to_day(year, month, day);
|
||||
print_day_of_week(nd);
|
||||
|
||||
return 0;
|
||||
}
|
43
Exercises/C10/function_example.c
Normal file
43
Exercises/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
Exercises/C10/void_function_example.c
Normal file
38
Exercises/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