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

View File

@@ -0,0 +1,81 @@
/* An example of
Creating dynamically an array
Populating the array
Displaying the contents
Freeing up the memory
Version 4 : Check memory has been assigned
Read and Write values using pointers
Pass array data to a function
*/
#include <stdio.h>
#include <stdlib.h>
int Sum ( int *Data, int n ) /* Define our function */
{
int Sum = 0,i; /* Define working variables */
for ( i = 0 ; i < n ; i++ )
{
printf("Item %d=%d\n", i, Data[i]);
Sum += Data[i]; /* This means Sum = Sum + Data[i] */
}
return Sum; /* Return the value */
}
int main(void)
{
int *ipArray = NULL; /* Create the pointer and set */
/* to null to start with */
int *ipStartValue = NULL; /* A place to store ipArray's */
/* initial value ,ie ipArray[0] */
int iSize = 0; /* Define our 'size' variable */
int i; /* A Loop variables */
/* Prompt for array size */
printf("\nHow big is the array to be ? ");
scanf("%d",&iSize);
/* Allocate the memory */
ipArray = (int *)calloc(iSize, sizeof(int));
if ( ipArray == NULL )
{
printf("\nUnable to allocate the memory requested");
printf("\n ** Program terminating ** \n");
exit (1);
}
/* Store the base memory address for use later */
ipStartValue = ipArray;
/* Populate the array ( Use Pointers: This is much faster !) */
for ( i = 0 ; i < iSize ; i++ )
*ipArray++ = i;
// Reset ipArray to start
ipArray = ipStartValue;
/* Display the sum of the values in the array */
printf("\nThe sum of the array values is %d ",Sum(ipArray,iSize) );
/* free memory : Again, reset ipArray to its origin */
ipArray = ipStartValue;
free(ipArray);
/* The above two lines could be replaced with free (ipStartValue) */
return 0;
}

View File

@@ -0,0 +1,61 @@
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
// This function will return
//
// 0 if the equation can be solved,
// -1 if it is not a quadratic
// -2 if it is complex
int SolveQuadratic ( int a, int b, int c, float *x1, float *x2)
{
int d;
// If the equations is not a quadratic return -1
if ( a == 0 )
return (-1);
// Calculate the value that will be square rooted
d = (b*b) - (4*a*c);
// If less than zero then it would be complex - give up!
if ( d < 0 )
return (-2);
// If we got to here we are OK to solve things
*x1 = (-b - sqrt(d)) / (2*a);
*x2 = (-b + sqrt(d)) / (2*a);
// This is are 'OK' return value
return 0;
}
int main()
{
int A,B,C, r;
float X1, X2;
printf ("Please enter a b & c separated by a space: ");
scanf("%d %d %d",&A, &B, &C);
r = SolveQuadratic(A,B,C, &X1, &X2);
// The value returned lets us know if we have values in X1 and X2 we can use
switch (r)
{
case 0 :
printf ("The solutions are %f and %f", X1, X2 );
break;
case -1 :
printf ("The equation provided was not a quadratic" );
break;
case -2 :
printf ("The solutions had complex roots and could not be solved");
break;
}
return 0;
}