move exercises and lectures into subfolders

This commit is contained in:
2023-10-15 15:34:53 +01:00
parent 775b4bd643
commit 74092a17aa
177 changed files with 0 additions and 0 deletions

1
Exercises/C14/Makefile Normal file
View File

@@ -0,0 +1 @@
include ../Makefile

32
Exercises/C14/ex1.c Normal file
View File

@@ -0,0 +1,32 @@
#include <stdio.h>
#include <math.h>
void cartesian_to_polar(float x, float y, float *r, float *theta) {
*r = sqrtf(x*x + y*y);
*theta = y/x;
}
int main() {
int rc;
float x, y, r, theta;
printf("x: ");
rc = scanf("%f", &x);
// check that scanf was successful by checking if it returned 1 (1 successfully scanned item)
if (rc != 1) {
printf("Please enter a real number\n");
return 1;
}
printf("y: ");
rc = scanf("%f", &y);
if (rc != 1) {
printf("Please enter a real number\n");
return 1;
}
cartesian_to_polar(x, y, &r, &theta);
printf("r=%f theta=%f\n", r, theta);
return 0;
}

29
Exercises/C14/ex2.c Normal file
View File

@@ -0,0 +1,29 @@
#include <stdio.h>
#include <math.h>
float degrees_to_radians(float degrees) {
return M_PI * degrees / 180.0;
}
void process(float degrees, float* radians, float* sin_val, float* cos_val, float* tan_val) {
*radians = degrees_to_radians(degrees);
*sin_val = sin(*radians);
*cos_val = cos(*radians);
*tan_val = tan(*radians);
}
int main() {
float degrees, radians, sin_val, cos_val, tan_val;
printf("Degrees: ");
scanf("%f", &degrees);
process(degrees, &radians, &sin_val, &cos_val, &tan_val);
printf("Radians: %f\n", radians);
printf("Sine: %f\n", sin_val);
printf("Cosine: %f\n", cos_val);
printf("Tangent: %f\n", tan_val);
}

33
Exercises/C14/ex3.c Normal file
View File

@@ -0,0 +1,33 @@
#include <stdio.h>
#include <math.h>
float degrees_to_radians(float degrees) {
return M_PI * degrees / 180.0;
}
void process(float degrees, float* radians, float* sin_val, float* cos_val, float* tan_val) {
*radians = degrees_to_radians(degrees);
*sin_val = sin(*radians);
*cos_val = cos(*radians);
*tan_val = tan(*radians);
}
int main() {
int degrees_start, degrees_end;
float radians, sin_val, cos_val, tan_val;
printf("Start: ");
scanf("%d", &degrees_start);
printf("End: ");
scanf("%d", &degrees_end);
printf("Degs\tRad\tsin\tcos\ttan\n");
for (int i = degrees_start; i <= degrees_end; i++) {
process((float)i, &radians, &sin_val, &cos_val, &tan_val);
printf("%d\t%0.3f\t%0.3f\t%0.3f\t%0.3f\n", i, radians, sin_val, cos_val, tan_val);
}
}

View File

@@ -0,0 +1,55 @@
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int SolveQuadraticEquation(float a, float b, float c, float *x1, float *x2)
{
float d; // For storing b^2 - 4*a*c
if ( a == 0) // Not a quadratie
{
return -1;
}
// calculate and store b*b-4*a*c for testing ans use later (if OK)
d = b*b - 4*a*c;
if ( d < 0 )
{
return -1; // Complex
}
// If we have got to here, we can calculate x1 and x2
*x1 = ( -b + sqrt (d)) / (2 * a); // Note the use of the * before x1 & x2
*x2 = ( -b - sqrt (d)) / (2 * a); // to write to the relevant memory locations
// As we got here OK, return 0 to indicate all is OK
return 0;
}
int main (void )
{
float A,B,C,x1,x2;
int retval;
printf ("Please enter coefficients A,B and C separated by a space\n");
scanf ("%f %f %f", &A, &B, &C);
// Make use of the function
retval = SolveQuadraticEquation(A, B, C, &x1, &x2);
// Use the retval to determine if we can display the answers or an derror message
if ( retval == -1 )
{
printf ("Not a quadratic\n");
}
else if (retval == -1)
{
printf ("The solution is complex - I cannot solve these\n");
}
else
{
printf("\nThe solutions are x1=%f, x2=%f", x1, x2);
}
return 0; // exit
}

View File

@@ -0,0 +1,24 @@
int SolveQuadraticEquation(float a, float b, float c, float *x1, float *x2)
{
float d; // For storing b^2 - 4*a*c
if ( a == 0) // Not a quadratie
{
return -1;
}
// calculate and store b*b-4*a*c for testing ans use later (if OK)
d = b*b - 4*a*c;
if ( d < 0 )
{
return -1; // Complex
}
// If we have got to here, we can calculate x1 and x2
*x1 = ( -b + sqrt (d)) / (2 * a); // Note the use of the * before x1 & x2
*x2 = ( -b - sqrt (d)) / (2 * a); // to write to the relevant memory locations
// As we got here OK, return 0 to indicate all is OK
return 0;
}