commit 5b205092a72e9b563f40aafa3a301597557eb18a Author: Louise Brown Date: Thu Apr 7 16:58:01 2022 +0100 Initial commit of lecture code diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..474e379 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.vscode +build/ \ No newline at end of file diff --git a/LC10/simple_functions_1.c b/LC10/simple_functions_1.c new file mode 100644 index 0000000..058d7ab --- /dev/null +++ b/LC10/simple_functions_1.c @@ -0,0 +1,47 @@ +#include +#include +#include // Needed to give access to M_PI + +/* FUNCTION: CalculateAreaOfCircle + + INPUTS: radius float + RETURNS: area float + + Description + Calculate area using area = M_PI * radius * radius + +*/ + + +float CalculateAreaOfCircle ( float radius ) +{ + float area; + area = M_PI * radius * radius ; + return (area) ; +} + + +/* Show use of function */ +int main (void) +{ + // Declare variables - no need to initialise as values will be read in / calculated + float rad, Area; + + // Prompt for and obtain value + printf ("Please enter the raduis of the circle: "); + scanf ("%f", &rad); + + // Use our function to calculate the area + Area = CalculateAreaOfCircle(rad); + + // And display the answer on the screen + printf ("The area of a circle of radius %f is %f\n", rad, Area ); + + // Note: As the function returns a value, if we did not need to store it + // we could calculate & display within the printf statement + + //printf ("The area of a circle of radius %f is %f\n", rad, CalculateAreaOfCircle(r) ); + + // All done + return 0; +} diff --git a/LC10/simple_functions_2.c b/LC10/simple_functions_2.c new file mode 100644 index 0000000..3b70441 --- /dev/null +++ b/LC10/simple_functions_2.c @@ -0,0 +1,51 @@ +#include +#include +#include // Needed to give access to M_PI + +/* FUNCTION: CalculateSurfaceAreaOfCylinder + + INPUTS: radius float + length float + + RETURNS: SurfaceArea float + + Description + Calculate the surface area of a cylinder given radius and length + +*/ + + +float CalculateSurfaceAreaOfCylinder ( float radius, float length ) +{ + float area; + area = 2.0 * ( M_PI * radius * radius ) + ( M_PI * 2.0 * radius * length ); // two ends + side + return (area) ; +} + + +/* Show use of function */ +int main (void) +{ + // Declare variables - no need to initialise as values will be read in / calculated + float rad, len, SurfaceArea; + + // Prompt for and obtain values + printf ("Please enter the radius of the cylinder: "); + scanf ("%f", &rad); +ad + printf ("Please enter the length of the cylinder: "); + scanf ("%f", &len); + + // Use our function to calculate the area + SurfaceArea = CalculateSurfaceAreaOfCylinder(rad, len); + + // And display the answer on the screen + printf ("The surface area of a cylinder of radius %f and length %f is %f\n", rad, len, SurfaceArea ); + + // Note: As the function returns a value, if we did not need to store it + // we could calculate & display within the printf statement + //printf ("The surface area of a cylinder of radius %f and length %f is %f\n", rad, len, CalculateSurfaceAreaOfCylinder(r,l) ); + + // All done + return 0; +} diff --git a/LC11/loop_into_array.c b/LC11/loop_into_array.c new file mode 100644 index 0000000..093baa3 --- /dev/null +++ b/LC11/loop_into_array.c @@ -0,0 +1,26 @@ + +/* A very simple program that defines an array of ten integers */ +/* And puts a different value into each */ + +#include /* Usual includes */ +#include + +int main( void ) +{ + + int Values[10]; /* Define an array of 10 integers */ + int j; /* Define an integer 'j' */ + + /* Use a loop to put some values into the array, displaying the */ + /* vaules stored on the screen */ + + for ( j = 0 ; j < 10 ; j++ ) + { + Values[j] = j * 2; + printf("\nThe value in array index %d is %d ",j,Values[j]); + } + + + return 0; /* End of the program */ + +} diff --git a/LC12/global_ex1.c b/LC12/global_ex1.c new file mode 100644 index 0000000..be30202 --- /dev/null +++ b/LC12/global_ex1.c @@ -0,0 +1,28 @@ +/* + This makes global variables look like a good idea + + THEY ARE NOT - DO NOT USE THEM + +*/ + +#include +#include + +int y,k,ans; /* Define GLOBAL variables */ + +void NastyGlobalFunction (void ) /* Define function */ +{ + ans = ( y * k ); /* y, k and ans are defined globally above */ + return ; +} + +int main( void ) +{ + y = 2; /* Set value of y */ + k = 3; /* Set value of k */ + + NastyGlobalFunction(); /* call the function */ + + printf("%d multiplied by %d is %d " ,y ,k ,ans ); /* Display values */ + return 0; +} \ No newline at end of file diff --git a/LC12/global_ex2.c b/LC12/global_ex2.c new file mode 100644 index 0000000..db7235c --- /dev/null +++ b/LC12/global_ex2.c @@ -0,0 +1,37 @@ +/* + In this example, we do not have a problem as although + we have a global variable 'i', we do not corrupt it in + the function + +*/ + +#include +#include + +int i; /* Nasty global variable ! */ + +/* + This function returns the sum of the number 1->10 + Note that although we use 'i' it is not defined in + the function its self. +*/ + +int SumToTen ( void ) +{ + + int Result = 0; + + for ( i = 10 ; i >= 0 ; i-- ) // Count down as more effecient + Result = Result + i; + + return Result; +} + +/* This is our main function */ + +int main(void) +{ + printf ("\nThe sum of the values 0 to 10 is %d ",SumToTen() ); + return 0; +} + diff --git a/LC12/global_ex3.c b/LC12/global_ex3.c new file mode 100644 index 0000000..36bdebe --- /dev/null +++ b/LC12/global_ex3.c @@ -0,0 +1,57 @@ +/* + + This time we have a real problem. We are using the variable + 'i' in the main loop to count and again in the function. + + Thus the function will change the value seen by the 'main' + routine - and will cause *Serious* problems + +*/ + +#include +#include + +int i; /* Again, the nasty global variable */ + +/* + + This function returns the sum of the number 0 -> i, but + summing from the limit back to zero ( to prove a point ! ) + + The problem this will cause is that at the end of the routine + the value of 'i' will always be '1' which will stop the loop + in 'main' working ! + +*/ + + +int SumToValue ( int Limit ) +{ + + int Result = 0; + + printf("\nIn function "); + + for ( i = Limit ; i > 0 ; i-- ) + Result = Result + i; + + return Result; +} + +/* + This is our main function - this time we are aiming to print out + at table of the sums of values from 1 to 10 +*/ + + +int main( void ) +{ + + for ( i = 0 ; i < 10 ; i++ ) + { + printf ("\nThe sum of the values 0 to %d is %d ",i,SumToValue(i)); + } + + return 0; +} + diff --git a/LC14/pointer_function_example_1.c b/LC14/pointer_function_example_1.c new file mode 100644 index 0000000..7e91ee4 --- /dev/null +++ b/LC14/pointer_function_example_1.c @@ -0,0 +1,32 @@ +#include +#include +#include + +// As we are using the function BEFORE it is actually written we need to provide the +// prototype so that the compiler can verify we are calling it correctly + +void CalculateArea ( double Radius, double *pArea); // note the ‘*’ + +// This is the main code for our application + +int main() +{ + double radius, area; + radius = 1.0; + CalculateArea (radius, &area); + + printf ("The area of circle of radius %f is %f\n", radius, area); + return 0; + +} + + +// And here is our function + +// Note: Pi is written out simply to match the notes, M_PI could also be used + +void CalculateArea ( double Radius, double *pArea ) +{ + *pArea = 3.14159265 * Radius * Radius; + return; +} diff --git a/LC14/pointer_function_example_2.c b/LC14/pointer_function_example_2.c new file mode 100644 index 0000000..856f6a0 --- /dev/null +++ b/LC14/pointer_function_example_2.c @@ -0,0 +1,31 @@ +#include +#include +#include + +// As we are using the function BEFORE it is actually written we need to provide the +// prototype so that the compiler can verify we are calling it correctly + +void CalculateVolumeAndSA ( double Radius, double Length, double *Volume, double *SurfaceArea); // note the ‘*’ + +// This is the main code for our application + +int main() +{ + double radius = 3.4, length = 7.3, volume, SurfaceArea; + + CalculateVolumeAndSA(radius, length, &volume, &SurfaceArea); + + printf("The volume is %f \n", volume); + printf( "The surface area is %f\n", SurfaceArea); + return 0; + +} + + +// And here is our function + +void CalculateVolumeAndSA ( double Radius, double Length, double *Volume, double *SurfaceArea) // note the ‘*’ +{ + *Volume = M_PI * Radius * Radius * Length; + *SurfaceArea = ( 2 * M_PI * Radius * Radius * Length ) +( 2 * M_PI * Radius * Length ); +} diff --git a/LC15/pointer_to_array_1.c b/LC15/pointer_to_array_1.c new file mode 100644 index 0000000..078a657 --- /dev/null +++ b/LC15/pointer_to_array_1.c @@ -0,0 +1,33 @@ +#include +#include + +int main(void) // Main : Execution starts here... +{ + // Define variables - pre-populate the array + int MyArray[10] = {12,34,23,11,8,19,6,44,9,16}; + int *pArray = &MyArray[0]; + int i; + + // Direct from the array + printf ("Directly from the array\n"); + for ( i = 0 ; i < 10 ; i++ ) + printf ("Item %d contains value %d\n",i,MyArray[i]); + + + // Pointer approach 1 + printf ("Pointer method 1\n"); + for ( i = 0 ; i < 10 ; i++ ) + printf ("Item %d contains value %d\n", i, pArray[i] ); + + printf ("Pointer method 1\n"); + for ( i = 0 ; i < 10 ; i++ ) + printf ("Item %d contains value %d\n", i, *(pArray+i) ); + + printf ("Pointer method 2 (only works for moving sequentially\n"); + for ( i = 0 ; i < 10 ; i++ ) + printf ("Item %d contains value %d\n", i, *pArray++ ); + + + // Exit the application + return 0; +} diff --git a/LC15/pointer_to_array_2.c b/LC15/pointer_to_array_2.c new file mode 100644 index 0000000..078a657 --- /dev/null +++ b/LC15/pointer_to_array_2.c @@ -0,0 +1,33 @@ +#include +#include + +int main(void) // Main : Execution starts here... +{ + // Define variables - pre-populate the array + int MyArray[10] = {12,34,23,11,8,19,6,44,9,16}; + int *pArray = &MyArray[0]; + int i; + + // Direct from the array + printf ("Directly from the array\n"); + for ( i = 0 ; i < 10 ; i++ ) + printf ("Item %d contains value %d\n",i,MyArray[i]); + + + // Pointer approach 1 + printf ("Pointer method 1\n"); + for ( i = 0 ; i < 10 ; i++ ) + printf ("Item %d contains value %d\n", i, pArray[i] ); + + printf ("Pointer method 1\n"); + for ( i = 0 ; i < 10 ; i++ ) + printf ("Item %d contains value %d\n", i, *(pArray+i) ); + + printf ("Pointer method 2 (only works for moving sequentially\n"); + for ( i = 0 ; i < 10 ; i++ ) + printf ("Item %d contains value %d\n", i, *pArray++ ); + + + // Exit the application + return 0; +} diff --git a/LC15/pointer_to_array_examples.c b/LC15/pointer_to_array_examples.c new file mode 100644 index 0000000..078a657 --- /dev/null +++ b/LC15/pointer_to_array_examples.c @@ -0,0 +1,33 @@ +#include +#include + +int main(void) // Main : Execution starts here... +{ + // Define variables - pre-populate the array + int MyArray[10] = {12,34,23,11,8,19,6,44,9,16}; + int *pArray = &MyArray[0]; + int i; + + // Direct from the array + printf ("Directly from the array\n"); + for ( i = 0 ; i < 10 ; i++ ) + printf ("Item %d contains value %d\n",i,MyArray[i]); + + + // Pointer approach 1 + printf ("Pointer method 1\n"); + for ( i = 0 ; i < 10 ; i++ ) + printf ("Item %d contains value %d\n", i, pArray[i] ); + + printf ("Pointer method 1\n"); + for ( i = 0 ; i < 10 ; i++ ) + printf ("Item %d contains value %d\n", i, *(pArray+i) ); + + printf ("Pointer method 2 (only works for moving sequentially\n"); + for ( i = 0 ; i < 10 ; i++ ) + printf ("Item %d contains value %d\n", i, *pArray++ ); + + + // Exit the application + return 0; +} diff --git a/LC16/Dynamic1.c b/LC16/Dynamic1.c new file mode 100644 index 0000000..8d1e0e9 --- /dev/null +++ b/LC16/Dynamic1.c @@ -0,0 +1,46 @@ +/* An example of + + Creating dynamically an array + Populating the array + Displaying the contents + Freeing up the memory + + Example 1 : No sucessful allocation checking + + +*/ + +#include +#include + + +int main(void) +{ + int *ipArray = NULL; /* Create the pointer and set */ + /* to null to start with */ + 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)); + + /* Populate the array (Method 1) */ + for ( i = 0 ; i < iSize ; i++ ) + ipArray[i] = iSize - i; + + /* display the data */ + for ( i = 0 ; i < iSize ; i++ ) + printf("Value %d is %d\n",i,ipArray[i] ); + + /* free memory */ + free(ipArray); + + return 0; +} + diff --git a/LC16/Dynamic2.c b/LC16/Dynamic2.c new file mode 100644 index 0000000..4176c7f --- /dev/null +++ b/LC16/Dynamic2.c @@ -0,0 +1,54 @@ +/* An example of + + Creating dynamically an array + Populating the array + Displaying the contents + Freeing up the memory + + Version 2 : Check memory has been assigned + + + +*/ + +#include +#include + + +int main(void) +{ + int *ipArray = NULL; /* Create the pointer and set */ + /* to null to start with */ + 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); + } + + /* Populate the array (Method 1) */ + for ( i = 0 ; i < iSize ; i++ ) + ipArray[i] = iSize - i; + + /* display the data */ + for ( i = 0 ; i < iSize ; i++ ) + printf("Value %d is %d\n",i,ipArray[i] ); + + /* free memory */ + free(ipArray); + + return 0; +} + diff --git a/LC16/Dynamic3.c b/LC16/Dynamic3.c new file mode 100644 index 0000000..e883720 --- /dev/null +++ b/LC16/Dynamic3.c @@ -0,0 +1,66 @@ +/* An example of + + Creating dynamically an array + Populating the array + Displaying the contents + Freeing up the memory + + Version 3 : Read and Write values using pointers + + +*/ + +#include +#include + + +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 (Method 2 - Use Pointers: This is much faster !) */ + for ( i = 0 ; i < iSize ; i++ ) + *ipArray++ = iSize - i; + + /* Reset the pointer to the origin of the array */ + ipArray = ipStartValue; + + /* display the data */ + for ( i = 0 ; i < iSize ; i++ ) + printf("Value %d is %d\n",i,*ipArray++ ); + + /* free memory : Again, reset ipArray to its origin */ + + ipArray = ipStartValue; + free(ipArray); + + /* The above two lines could be replaced with free (ipStartValue) */ + + return 0; +} + diff --git a/LC17/DynamicFunction.c b/LC17/DynamicFunction.c new file mode 100644 index 0000000..8dace68 --- /dev/null +++ b/LC17/DynamicFunction.c @@ -0,0 +1,79 @@ +/* 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 +#include + + +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 (Method 2 - Use Pointers: This is much faster !) */ + for ( i = 0 ; i < iSize ; i++ ) + ipArray[i] = i; + + /* 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; +} + diff --git a/LC17/Quadratic_with_Pointers.c b/LC17/Quadratic_with_Pointers.c new file mode 100644 index 0000000..8eb030e --- /dev/null +++ b/LC17/Quadratic_with_Pointers.c @@ -0,0 +1,61 @@ +#include +#include +#include + + +// 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; +} diff --git a/LC18/filemove.c b/LC18/filemove.c new file mode 100644 index 0000000..b8108c1 --- /dev/null +++ b/LC18/filemove.c @@ -0,0 +1,45 @@ +#include +#include + +/* Example 3 - jumping & getting element count */ + +int main(void) +{ + + int ValRead; + int Item; + int Locn; + FILE *fptr; + /* Open the file from example 1 */ + + fptr = fopen ("data.dat","rb"); + if ( fptr == NULL ) + { + printf ("\nError opening input file - aborting "); + exit (0); + } + + /* Ask the user which value to jump to */ + printf ("\nWhich value do you wish to view ? "); + scanf ("%d",&Item); + + /* Jump to this item */ + /* notice we move in steps of item size */ + + Locn = Item * sizeof(int); + fseek (fptr, Locn, SEEK_SET); + + /* And read a single integer in */ + fread (&ValRead, sizeof(int), 1, fptr ); + + /* Display the read value */ + printf ("Item %d is %d\n",Item,ValRead); + + /* And close */ + fclose (fptr); + + /* All Done */ + return 0; + +} + \ No newline at end of file diff --git a/LC18/filesize.c b/LC18/filesize.c new file mode 100644 index 0000000..3c0f676 --- /dev/null +++ b/LC18/filesize.c @@ -0,0 +1,38 @@ +#include +#include + +/* Example 3 - jumping & getting element count */ + +main() +{ + + long FileBytes; + FILE *fptr; + /* Open the file from example 1 */ + + fptr = fopen ("data.dat","rb"); + if ( fptr == NULL ) + { + printf ("\nError opening input file - aborting "); + exit (0); + } + + /* Move to the end of file */ + fseek (fptr, 0, SEEK_END); + + /* Get the byte size */ + FileBytes = ftell(fptr); + + /* Convert to size based on fact all int's */ + FileBytes = FileBytes / sizeof(int); + + /* Display the read value */ + printf ("No. of items in file is %d\n",FileBytes); + + /* And close */ + fclose (fptr); + + /* All Done */ + return 0; +} + \ No newline at end of file diff --git a/LC19/ConstHashDefine.c b/LC19/ConstHashDefine.c new file mode 100644 index 0000000..4f253b7 --- /dev/null +++ b/LC19/ConstHashDefine.c @@ -0,0 +1,25 @@ +#include + +//macro definition +#define X 30 + +//global integer constant +const int Y = 10; + +int main() +{ + //local integer constant` + const int Z = 20; + + printf("Value of X: %d\n", X); + printf("Value of Y: %d\n", Y); + printf("Value of Z: %d\n", Z); + +// #undef X +// #define X 300 +// printf("Value of X: %d\n", X); + +// Y = 30; + + return 0; +} diff --git a/LC19/Static.c b/LC19/Static.c new file mode 100644 index 0000000..c4cc180 --- /dev/null +++ b/LC19/Static.c @@ -0,0 +1,45 @@ +/* + Static variables - a variable that is created when a function is first + called and remains until the program terminates + + This is to say that in future calls to the function it is not redefined + and can take the value it last had in the function. + + This should hopefully show this ! +*/ + +#include +#include + +void NonStaticFunction(void) +{ + int i =0 ; /* This is issued EVERY time the function is called */ + i = i + 1; + printf ("\nIn Non static function, the value of i is %d ",i); + return; +} + +void StaticFunction(void) +{ + static int i =0 ; /* This is issued THE FIRST TIME, + AND ONLY THE FIRST TIME + that the function is called */ + i = i + 1; + printf ("\nIn *static* function, the value of i is %d ",i); + return; +} + + +int main(void) +{ + + int i; + for ( i = 0 ; i < 10 ; i++ ) + { + NonStaticFunction(); + StaticFunction(); + } + + return 0; +} + \ No newline at end of file diff --git a/LC19/Structure_1.c b/LC19/Structure_1.c new file mode 100644 index 0000000..b8b91a0 --- /dev/null +++ b/LC19/Structure_1.c @@ -0,0 +1,27 @@ + +#include +#include + +struct DataSet /* Define a struture called DataSet */ +{ + int x; /* The X data */ + int y; /* The Y data */ +}; + + +int main(void) +{ + + struct DataSet MyPoints; /* Define structure, type DataSet */ + + MyPoints.x = 1; + MyPoints.y = 2; + + printf("\nThe dataset values are "); + printf("\n\t\t X = %d ",MyPoints.x); + printf("\n\t\t Y = %d ",MyPoints.y); + + + + return 0; +} \ No newline at end of file diff --git a/LC19/Structure_2.c b/LC19/Structure_2.c new file mode 100644 index 0000000..ad9748a --- /dev/null +++ b/LC19/Structure_2.c @@ -0,0 +1,39 @@ +#include +#include + +struct DataSet /* Define a struture called DataSet */ +{ + int x; /* The X data */ + int y; /* The Y data */ +}; + + +int main(void) +{ + + struct DataSet MyPoints[5]; /* Define array of structure, */ + /* type DataSet */ + /* Elements : 10 */ + + int i; /* Define an integer */ + + + + for ( i = 0 ; i < 5 ; i++ ) /* Populate structure */ + { + MyPoints[i].x = i; + MyPoints[i].y = i*i; + } + + printf("\nThe dataset values are "); /* Display message & Values */ + + for ( i = 0 ; i < 5 ; i++ ) + { + printf("\n\t Set %d : ",i); + printf("\t X = %d ",MyPoints[i].x); + printf("\t Y = %d ",MyPoints[i].y); + } + + + return 0; +} \ No newline at end of file diff --git a/LC19/Structure_3.c b/LC19/Structure_3.c new file mode 100644 index 0000000..834d680 --- /dev/null +++ b/LC19/Structure_3.c @@ -0,0 +1,59 @@ +#include +#include + +struct DataSet /* Define a struture called DataSet */ +{ + int x; /* The X data */ + int y; /* The Y data */ +}; + + +int main(void) +{ + + struct DataSet MyPoints[5]; /* Define array of structure, */ + /* type DataSet */ + /* Elements : 10 */ + + struct DataSet CpPoints[5]; /* Define array of structure, */ + /* type DataSet */ + /* Elements : 10 */ + + int i; /* Define an integer */ + + + + for ( i = 0 ; i < 5 ; i++ ) /* Populate structure */ + { + MyPoints[i].x = i; + MyPoints[i].y = i*i; + } + + printf("\nThe dataset values are "); /* Display message & Values */ + + for ( i = 0 ; i < 5 ; i++ ) + { + printf("\n\t Set %d : ",i); + printf("\t X = %d ",MyPoints[i].x); + printf("\t Y = %d ",MyPoints[i].y); + } + + /* Make a copy of the data */ + + for ( i = 0 ; i < 5 ; i++ ) + CpPoints[i] = MyPoints[i]; + + + /* And print it out again */ + + printf("\nThe copied dataset values are "); /* Display message & Values */ + + for ( i = 0 ; i < 5 ; i++ ) + { + printf("\n\t Set %d : ",i); + printf("\t X = %d ",MyPoints[i].x); + printf("\t Y = %d ",MyPoints[i].y); + } + + return 0; +} \ No newline at end of file diff --git a/LC19/WriteHeader.c b/LC19/WriteHeader.c new file mode 100644 index 0000000..3ae28c6 --- /dev/null +++ b/LC19/WriteHeader.c @@ -0,0 +1,77 @@ +#include +#include + +struct MyStruct +{ + int NoItems; + int max; + float average; +}; + +int main() +{ + + struct MyStruct MyRecord; + FILE *fptr; + + int ArrayData[100]; + int items = 0; + int sum = 0; + int i; + + + + /* Write this file to disk - binary format, usual error checking */ + fptr = fopen ("strdata.dat","wb"); + if ( fptr == NULL ) + { + printf ("\nError creating file - aborting "); + exit (0); + } + + /* Populate the array & set header values*/ + for ( i = 0 ; i < 100 ; i++) + { + ArrayData[i] = i; + if ( ArrayData [i] > MyRecord.max) + MyRecord.max = ArrayData[i]; + sum = sum + ArrayData[i]; + items++; + } + + /* Update the values in the structure */ + MyRecord.NoItems = items; + MyRecord.NoItems = (float)sum / (float)items; + + /* This line does the writing of the structure */ + fwrite ( &MyRecord, sizeof(struct MyStruct), 1, fptr); + + + /* This line does the writing of the Array */ + fwrite ( &ArrayData[0], sizeof(struct MyStruct),1, fptr); + + + /* And close */ + fclose (fptr); + + /* To read the data we would use */ + + fptr = fopen ("strdata.dat","rb"); + if ( fptr == NULL ) + { + printf ("\nError opening input file - aborting "); + exit (0); + } + + /* This line does the reading */ + fread ( &MyRecord, sizeof(struct MyStruct), 1, fptr); + printf ("No. items in the file = %d", MyRecord.NoItems); + printf ("Maximum = %d", MyRecord.max); + printf ("Average is = %.2f", MyRecord.average); + + /* And close */ + fclose (fptr); + + /* All Done */ + return 0; +} diff --git a/LC19/WriteHeaderArrayAndUpdate.c b/LC19/WriteHeaderArrayAndUpdate.c new file mode 100644 index 0000000..074828f --- /dev/null +++ b/LC19/WriteHeaderArrayAndUpdate.c @@ -0,0 +1,90 @@ +#include +#include + +struct MyStruct +{ + int NoItems; + int max; + float average; +}; + +int main() +{ + + struct MyStruct MyRecord; + FILE *fptr; + + int ArrayData[100]; + int items = 0; + int sum = 0; + int i; + + + + /* Write this file to disk - binary format, usual error checking */ + fptr = fopen ("strdata.dat","wb"); + if ( fptr == NULL ) + { + printf ("\nError creating file - aborting "); + exit (0); + } + + /* Pop some dummy data into our structure */ + MyRecord.NoItems = 0; + MyRecord.max = 0; ; + MyRecord.average = 0; + + + /* This line does the writing of the structure */ + fwrite ( &MyRecord, sizeof(struct MyStruct), 1, fptr); + + /* Populate the array */ + for ( i = 0 ; i < 100 ; i++) + { + ArrayData[i] = i; + if ( ArrayData [i] > MyRecord.max) + MyRecord.max = ArrayData[i]; + sum = sum + ArrayData[i]; + items++; + } + + + /* This line does the writing of the Array */ + fwrite ( &ArrayData[0], sizeof(int),100, fptr); + + + /* Update the values in the structure */ + MyRecord.NoItems = items; + MyRecord.NoItems = (float)sum / (float)items; + + /* rewind the file to write the structure again */ + fseek(fptr, SEEK_SET, 0); + rewind (fptr); + + /* This line does the writing of the structure */ + fwrite ( &MyRecord, sizeof(struct MyStruct), 1, fptr); + + /* And close */ + fclose (fptr); + +/* To read the data we would use */ + + fptr = fopen ("strdata.dat","rb"); + if ( fptr == NULL ) + { + printf ("\nError opening input file - aborting "); + exit (0); + } + + /* This line does the reading */ + fread ( &MyRecord, sizeof(struct MyStruct), 1, fptr); + printf ("No. items in the file = %d", MyRecord.NoItems); + printf ("Maximum = %d", MyRecord.max); + printf ("Average is = %.2f", MyRecord.average); + + /* And close */ + fclose (fptr); + + /* All Done */ + return 0; +} diff --git a/LC19/WriteHeaderArrayAndUpdateAndSeek.c b/LC19/WriteHeaderArrayAndUpdateAndSeek.c new file mode 100644 index 0000000..fb0ef03 --- /dev/null +++ b/LC19/WriteHeaderArrayAndUpdateAndSeek.c @@ -0,0 +1,99 @@ +#include +#include + +struct MyStruct +{ + int NoItems; + int max; + float average; +}; + +int main() +{ + + struct MyStruct MyRecord; + FILE *fptr; + + int ArrayData[100]; + int items = 0; + int sum = 0; + int i; + int AValue; + + + + /* Write this file to disk - binary format, usual error checking */ + fptr = fopen ("strdata.dat","wb"); + if ( fptr == NULL ) + { + printf ("\nError creating file - aborting "); + exit (0); + } + + /* Pop some dummy data into our structure */ + MyRecord.NoItems = 0; + MyRecord.max = 0; ; + MyRecord.average = 0; + + /* This line does the writing of the structure */ + fwrite ( &MyRecord, sizeof(struct MyStruct), 1, fptr); + + /* Populate the array */ + for ( i = 0 ; i < 100 ; i++) + { + ArrayData[i] = i; + if ( ArrayData [i] > MyRecord.max) + MyRecord.max = ArrayData[i]; + sum = sum + ArrayData[i]; + items++; + } + + + /* This line does the writing of the Array */ + fwrite ( &ArrayData[0], sizeof(int),100, fptr); + + /* Update the values in the structure */ + MyRecord.NoItems = items; + MyRecord.average= (float)sum / (float)items; + + /* rewind the file to write the structure again */ + fseek(fptr, SEEK_SET, 0); + rewind (fptr); + + /* This line does the writing of the structure */ + fwrite ( &MyRecord, sizeof(struct MyStruct), 1, fptr); + + /* And close */ + fclose (fptr); + +/* To read the data we would use */ + + fptr = fopen ("strdata.dat","rb"); + if ( fptr == NULL ) + { + printf ("\nError opening input file - aborting "); + exit (0); + } + + /* This line does the reading */ + fread ( &MyRecord, sizeof(struct MyStruct), 1, fptr); + printf ("No. items in the file = %d\n", MyRecord.NoItems); + printf ("Maximum = %d\n", MyRecord.max); + printf ("Average is = %.2f\n", MyRecord.average); + + // Get how far to move for thr 10th item + long Posn = sizeof (struct MyStruct) + (9 * sizeof(int)); + + // Move + fseek(fptr, Posn, SEEK_SET); + + // Read value + fread ( &AValue, sizeof(int),1 , fptr); + printf ("The 10th value is %d", AValue); + + /* And close */ + fclose (fptr); + + /* All Done */ + return 0; +} diff --git a/LC19/WriteHeaderWithArray.c b/LC19/WriteHeaderWithArray.c new file mode 100644 index 0000000..4724d3f --- /dev/null +++ b/LC19/WriteHeaderWithArray.c @@ -0,0 +1,77 @@ +#include +#include + +struct MyStruct +{ + int NoItems; + int max; + float average; +}; + +int main() +{ + + struct MyStruct MyRecord; + FILE *fptr; + + int ArrayData[100]; + int items = 0; + int sum = 0; + int i; + + + + /* Write this file to disk - binary format, usual error checking */ + fptr = fopen ("strdata.dat","wb"); + if ( fptr == NULL ) + { + printf ("\nError creating file - aborting "); + exit (0); + } + + /* Populate the array & set header values*/ + for ( i = 0 ; i < 100 ; i++) + { + ArrayData[i] = i; + if ( ArrayData [i] > MyRecord.max) + MyRecord.max = ArrayData[i]; + sum = sum + ArrayData[i]; + items++; + } + + /* Update the values in the structure */ + MyRecord.NoItems = items; + MyRecord.NoItems = (float)sum / (float)items; + + /* This line does the writing of the structure */ + fwrite ( &MyRecord, sizeof(struct MyStruct), 1, fptr); + + + /* This line does the writing of the Array */ + fwrite ( &ArrayData[0], sizeof(int),100, fptr); + + + /* And close */ + fclose (fptr); + + /* To read the data we would use */ + + fptr = fopen ("strdata.dat","rb"); + if ( fptr == NULL ) + { + printf ("\nError opening input file - aborting "); + exit (0); + } + + /* This line does the reading */ + fread ( &MyRecord, sizeof(struct MyStruct), 1, fptr); + printf ("No. items in the file = %d", MyRecord.NoItems); + printf ("Maximum = %d", MyRecord.max); + printf ("Average is = %.2f", MyRecord.average); + + /* And close */ + fclose (fptr); + + /* All Done */ + return 0; +} diff --git a/LC19/enum.c b/LC19/enum.c new file mode 100644 index 0000000..9ed292f --- /dev/null +++ b/LC19/enum.c @@ -0,0 +1,24 @@ +#include +#include + +int main(void) +{ + + /* Set up our enumerated type */ + enum Days { Mon, Tue, Wed, Thu, Fri, Sat, Sun }; + + /* If we wish, we can set the base level */ + //enum Days { Mon = 1 , Tue, Wed, Thu, Fri, Sat, Sun }; + + /* Create a variable that can take values of this type */ + enum Days DOW; + + /* Set variable equal to one of these days */ + DOW = Sun; + + /* And print the assigned value */ + printf ("\nThe value assigned to Sun is %d ",DOW); + + + return 0; +} \ No newline at end of file diff --git a/LC19/enumdefine.c b/LC19/enumdefine.c new file mode 100644 index 0000000..84a41e7 --- /dev/null +++ b/LC19/enumdefine.c @@ -0,0 +1,41 @@ +/* This program shows the use of an 'enum' statements to make code + more general. This is an 'OK' method but realise that it does + mean that we need to recompile if we change a vlaue. + + This method is best used only for fixed constants (eg PI ) + + While this version acts the same as the previous, it is better + as the compiler handles the 'enum'. Whereas the pre-processor + deals with #defines (performing simple substitutions) this is done + no with little error checking, the 'enum' is dealt with by the + compiler which finds error more easily - hence more stable code. + +*/ + +#include +#include + +enum ProblemSize { SIZE = 3 }; /* 'ProblemSize' is optional + but a warning is generated is + it not specified */ + + +int main(void) +{ + int i,j; + int iMatrix[SIZE][SIZE]; + int iCols = SIZE; + int iRows = SIZE; + + for (i = 0 ; i < iCols ; i++ ) + { + for (j = 0 ; j < iRows ; j++ ) + { + iMatrix[i][j] = i+j; + printf ("%2d ",iMatrix[i][j]); + } + printf ("\n"); + } + + return 0; +} \ No newline at end of file diff --git a/LC19/file_header.c b/LC19/file_header.c new file mode 100644 index 0000000..074828f --- /dev/null +++ b/LC19/file_header.c @@ -0,0 +1,90 @@ +#include +#include + +struct MyStruct +{ + int NoItems; + int max; + float average; +}; + +int main() +{ + + struct MyStruct MyRecord; + FILE *fptr; + + int ArrayData[100]; + int items = 0; + int sum = 0; + int i; + + + + /* Write this file to disk - binary format, usual error checking */ + fptr = fopen ("strdata.dat","wb"); + if ( fptr == NULL ) + { + printf ("\nError creating file - aborting "); + exit (0); + } + + /* Pop some dummy data into our structure */ + MyRecord.NoItems = 0; + MyRecord.max = 0; ; + MyRecord.average = 0; + + + /* This line does the writing of the structure */ + fwrite ( &MyRecord, sizeof(struct MyStruct), 1, fptr); + + /* Populate the array */ + for ( i = 0 ; i < 100 ; i++) + { + ArrayData[i] = i; + if ( ArrayData [i] > MyRecord.max) + MyRecord.max = ArrayData[i]; + sum = sum + ArrayData[i]; + items++; + } + + + /* This line does the writing of the Array */ + fwrite ( &ArrayData[0], sizeof(int),100, fptr); + + + /* Update the values in the structure */ + MyRecord.NoItems = items; + MyRecord.NoItems = (float)sum / (float)items; + + /* rewind the file to write the structure again */ + fseek(fptr, SEEK_SET, 0); + rewind (fptr); + + /* This line does the writing of the structure */ + fwrite ( &MyRecord, sizeof(struct MyStruct), 1, fptr); + + /* And close */ + fclose (fptr); + +/* To read the data we would use */ + + fptr = fopen ("strdata.dat","rb"); + if ( fptr == NULL ) + { + printf ("\nError opening input file - aborting "); + exit (0); + } + + /* This line does the reading */ + fread ( &MyRecord, sizeof(struct MyStruct), 1, fptr); + printf ("No. items in the file = %d", MyRecord.NoItems); + printf ("Maximum = %d", MyRecord.max); + printf ("Average is = %.2f", MyRecord.average); + + /* And close */ + fclose (fptr); + + /* All Done */ + return 0; +} diff --git a/LC19/file_header_move.c b/LC19/file_header_move.c new file mode 100644 index 0000000..fb0ef03 --- /dev/null +++ b/LC19/file_header_move.c @@ -0,0 +1,99 @@ +#include +#include + +struct MyStruct +{ + int NoItems; + int max; + float average; +}; + +int main() +{ + + struct MyStruct MyRecord; + FILE *fptr; + + int ArrayData[100]; + int items = 0; + int sum = 0; + int i; + int AValue; + + + + /* Write this file to disk - binary format, usual error checking */ + fptr = fopen ("strdata.dat","wb"); + if ( fptr == NULL ) + { + printf ("\nError creating file - aborting "); + exit (0); + } + + /* Pop some dummy data into our structure */ + MyRecord.NoItems = 0; + MyRecord.max = 0; ; + MyRecord.average = 0; + + /* This line does the writing of the structure */ + fwrite ( &MyRecord, sizeof(struct MyStruct), 1, fptr); + + /* Populate the array */ + for ( i = 0 ; i < 100 ; i++) + { + ArrayData[i] = i; + if ( ArrayData [i] > MyRecord.max) + MyRecord.max = ArrayData[i]; + sum = sum + ArrayData[i]; + items++; + } + + + /* This line does the writing of the Array */ + fwrite ( &ArrayData[0], sizeof(int),100, fptr); + + /* Update the values in the structure */ + MyRecord.NoItems = items; + MyRecord.average= (float)sum / (float)items; + + /* rewind the file to write the structure again */ + fseek(fptr, SEEK_SET, 0); + rewind (fptr); + + /* This line does the writing of the structure */ + fwrite ( &MyRecord, sizeof(struct MyStruct), 1, fptr); + + /* And close */ + fclose (fptr); + +/* To read the data we would use */ + + fptr = fopen ("strdata.dat","rb"); + if ( fptr == NULL ) + { + printf ("\nError opening input file - aborting "); + exit (0); + } + + /* This line does the reading */ + fread ( &MyRecord, sizeof(struct MyStruct), 1, fptr); + printf ("No. items in the file = %d\n", MyRecord.NoItems); + printf ("Maximum = %d\n", MyRecord.max); + printf ("Average is = %.2f\n", MyRecord.average); + + // Get how far to move for thr 10th item + long Posn = sizeof (struct MyStruct) + (9 * sizeof(int)); + + // Move + fseek(fptr, Posn, SEEK_SET); + + // Read value + fread ( &AValue, sizeof(int),1 , fptr); + printf ("The 10th value is %d", AValue); + + /* And close */ + fclose (fptr); + + /* All Done */ + return 0; +} diff --git a/LC19/swap.c b/LC19/swap.c new file mode 100644 index 0000000..eb2fb4a --- /dev/null +++ b/LC19/swap.c @@ -0,0 +1,72 @@ +#include +#include + +union number +{ + double d; + float f; + long l; + int i; + unsigned short s; + unsigned char c[8]; +}; + +int main(void) +{ + /* Define variables and a union */ + int my_i; + short my_s; + unsigned char tc; + union number mynum; + + /* Prompt for use input ao a number whose bytes we are to swap */ + printf ("\nPlease enter a number "); + scanf("%hi",&my_s); // hi is the format for a signed short + + /* pop this number into our union */ + mynum.s = my_s; + + /* Show the present byte order */ + printf ("\nThe bytes making up your number %hi are ",my_s); + printf ("%d %d %d %d %d %d %d %d ",mynum.c[0],mynum.c[1],mynum.c[2],mynum.c[3],mynum.c[4],mynum.c[5],mynum.c[6],mynum.c[7]); + + /* We now swap the two lowest bytes (via an intermediate variable) */ + tc = mynum.c[1]; + mynum.c[1] = mynum.c[0]; + mynum.c[0] = tc; + + printf ("\nThe bytes making up your number are now "); + printf ("%d %d %d %d %d %d %d %d ",mynum.c[0],mynum.c[1],mynum.c[2],mynum.c[3],mynum.c[4],mynum.c[5],mynum.c[6],mynum.c[7]); + + printf ("\nWhich corresponds to %d ",mynum.s); + + // Repeat for an integer + /* Prompt for use input ao a number whose bytes we are to swap */ + printf ("\nPlease enter a number [INTEGER] "); + scanf("%d",&my_i); + + /* pop this number into our union */ + mynum.i = my_i; + + /* Show the present byte order */ + printf ("\nThe bytes making up your number %d are ",my_i); + printf ("%d %d %d %d %d %d %d %d ",mynum.c[0],mynum.c[1],mynum.c[2],mynum.c[3],mynum.c[4],mynum.c[5],mynum.c[6],mynum.c[7]); + + /* We now swap the 1st and 4th bytes (via an intermediate variable) */ + tc = mynum.c[3]; + mynum.c[3] = mynum.c[0]; + mynum.c[0] = tc; + + /* We now swap the 1st and 4th bytes (via an intermediate variable) */ + tc = mynum.c[2]; + mynum.c[2] = mynum.c[1]; + mynum.c[1] = tc; + + printf ("\nThe bytes making up your number are now "); + printf ("%d %d %d %d %d %d %d %d ",mynum.c[0],mynum.c[1],mynum.c[2],mynum.c[3],mynum.c[4],mynum.c[5],mynum.c[6],mynum.c[7]); + + printf ("\nWhich corresponds to %d ",mynum.i); + + return 0; + +} diff --git a/LC19/union.c b/LC19/union.c new file mode 100644 index 0000000..cc0fe09 --- /dev/null +++ b/LC19/union.c @@ -0,0 +1,31 @@ +#include +#include + +union number{ + double d; + float f; + long l; + int i; + short s; + unsigned char c[8]; +}; + +int main(void) +{ + int my_i; + union number mynum; + printf ("\nPlease enter a number "); + scanf("%d",&my_i); + printf ("\nThe bytes making up your number %d are ",my_i); + + mynum.i = my_i; + printf ("%d %d %d %d %d %d %d %d ",mynum.c[0],mynum.c[1],mynum.c[2],mynum.c[3],mynum.c[4],mynum.c[5],mynum.c[6],mynum.c[7]); + + printf ("\nSize of double id %d ",sizeof(double)); + printf ("\nSize of float id %d ",sizeof(float)); + printf ("\nSize of long id %d ",sizeof(long)); + printf ("\nSize of int id %d ",sizeof(int)); + printf ("\nSize of short id %d ",sizeof(short)); + return 0; + +} \ No newline at end of file diff --git a/LC21/cond.c b/LC21/cond.c new file mode 100644 index 0000000..d3409f3 --- /dev/null +++ b/LC21/cond.c @@ -0,0 +1,37 @@ +#include +#include + +/* We define one thing - we need not give it a value, but we can */ +#define BUILD1 + +int main(void) +{ + + + printf ("\nThis code is common to all "); + +/* We check for the existance ob 'BUILD1' and act accordingly */ + +#ifdef BUILD1 + + printf ("\nWe had BUILD1 defined "); + +#else + + printf ("\nBUILD1 was not defined "); + +#endif + +/* Can can see if somthing was not defined too */ + +#ifndef BUILD2 + + printf ("\nBUILD2 was *NOT* defined "); + +#endif + + printf ("\n\n"); + + return 0; + +} diff --git a/LC21/getthem.c b/LC21/getthem.c new file mode 100644 index 0000000..abd4026 --- /dev/null +++ b/LC21/getthem.c @@ -0,0 +1,24 @@ +#include +#include + +int main(int argc, char *argv[]) +{ + int age; + char name[10]; + + /* we check the argument count is 3 ( prog name plus 2 params ) */ + if ( argc != 3 ) + { + printf ("\nProgram use %s name age ",argv[0]); + exit (0); + } + + /* Copy the command parameters into suitable variables */ + sscanf(argv[1],"%s",name); + sscanf(argv[2],"%d",&age); + + /* And display for all to see */ + printf ("\nHello %s, you are %d ",name,age); + + return 0; +} diff --git a/LC21/simple.c b/LC21/simple.c new file mode 100644 index 0000000..7c013bb --- /dev/null +++ b/LC21/simple.c @@ -0,0 +1,19 @@ +#include +#include + +/* Note the new type of main */ + +int main(int argc, char *argv[]) +{ + int x; + + /* Print the argument count */ + printf("Arguments -> %d\n",argc); + + /* And the arguments themselves */ + for (x=0; x +#include +#include + +int main(void) +{ + char buffer[100]; + int age=6; + char name[6]; + + /* This command copies "James" into the character array 'Name' */ + + strcpy(name,"James"); + + /* Print to the string (rather than screen) */ + + sprintf(buffer,"Name: %s, Age %d ",name,age); + + /* And output the string created */ + printf("\n->%s\n",buffer); + + return 0; +} diff --git a/LC21/use.c b/LC21/use.c new file mode 100644 index 0000000..e127221 --- /dev/null +++ b/LC21/use.c @@ -0,0 +1,29 @@ +#include +#include + +#define BUILD1 23 + +int main(void) +{ + + int MyValue = 0; + + printf ("\nThis code is common to all "); + +#ifdef BUILD1 + + printf ("\nWe had BUILD1 defined, will adjust value accordingly "); + MyValue = BUILD1; + +#else + + printf ("\nBUILD1 was not defined, leaving MyValue alone "); + +#endif + + printf ("\nThe value in MyValue is %d ",MyValue); + printf ("\n\n"); + + return 0; + +} diff --git a/LC4/hello_world.c b/LC4/hello_world.c new file mode 100644 index 0000000..d089699 --- /dev/null +++ b/LC4/hello_world.c @@ -0,0 +1,9 @@ +#include +#include + +int main( void ) +{ + /* My first program in C */ + printf ("Hello World \n"); + return 0; // Return from prog +} diff --git a/LC4/hello_world.exe b/LC4/hello_world.exe new file mode 100644 index 0000000..35f237e Binary files /dev/null and b/LC4/hello_world.exe differ diff --git a/LC5/printf_example.c b/LC5/printf_example.c new file mode 100644 index 0000000..c54b7c1 --- /dev/null +++ b/LC5/printf_example.c @@ -0,0 +1,14 @@ +#include +#include +int main(void) +{ + int a,b,c,sum; /* Define variables */ + a = 1; /* Assign values */ + b = 2; + c = 3; + sum = a + b + c ; /* Calculate sum & Display */ + + printf ("\nThe sum of %d + %d + %d is %d \n", a, b, c, sum); + + return 0; /* Return from prog */ +} diff --git a/LC5/printf_example.exe b/LC5/printf_example.exe new file mode 100644 index 0000000..6e6e745 Binary files /dev/null and b/LC5/printf_example.exe differ diff --git a/LC7/fgets_string.c b/LC7/fgets_string.c new file mode 100644 index 0000000..a6471c1 --- /dev/null +++ b/LC7/fgets_string.c @@ -0,0 +1,22 @@ +/* EEEE1001/H61CAE*/ + +/* A program to show how to use fgets to safely read in a string */ + +#include +#include + +int main() +{ + + char MyName[50]; /* Define a string of 10 characters */ + + + /* A string */ + + printf("\nPlease enter your name & press return "); + fgets(MyName, 80, stdin); + printf("\nHello %s ",MyName); + + return 0; /* End of the program */ + +} diff --git a/LC7/getch.c b/LC7/getch.c new file mode 100644 index 0000000..98f7990 --- /dev/null +++ b/LC7/getch.c @@ -0,0 +1,27 @@ +/* + Chapter 7: example of getch + + This program reads a single keystroke and displays it + on the screen + +*/ + +#include /* Standard include files */ +#include +#include + +int main(void) +{ + + char x; /* Define a variable of type char */ + + x = getch(); /* Use getchar to read a keypress and store the */ + /* result in 'x' */ + + putchar(x); /* Display the character stored in 'x' on the */ + /* screen using putchar */ + + + return 0; + +} diff --git a/LC7/getchar.c b/LC7/getchar.c new file mode 100644 index 0000000..9826d10 --- /dev/null +++ b/LC7/getchar.c @@ -0,0 +1,27 @@ +/* + Chapter 7: example of getchar + + This program reads a single keystroke and displays it + on the screen + +*/ + +#include /* Standard include files */ +#include + + +int main(void) +{ + + char x; /* Define a variable of type char */ + + x = getchar(); /* Use getchar to read a keypress and store the */ + /* result in 'x' */ + + putchar(x); /* Display the character stored in 'x' on the */ + /* screen using putchar */ + + + return 0; + +} diff --git a/LC7/gets_string.c b/LC7/gets_string.c new file mode 100644 index 0000000..2a7c94b --- /dev/null +++ b/LC7/gets_string.c @@ -0,0 +1,22 @@ +/* EEEE1001/H61CAE - Chapter 7 */ + +/* A program to show how to use gets to read a string (that can include spaces) */ + +#include +#include + +int main( void ) +{ + + char MyName[50]; /* Define a string of 10 characters */ + + + /* A string */ + + printf("\nPlease enter your name & press return "); + gets(MyName); + printf("\nHello %s ",MyName); + + return 0; /* End of the program */ + +} diff --git a/LC7/scanf_examples.c b/LC7/scanf_examples.c new file mode 100644 index 0000000..9e676cc --- /dev/null +++ b/LC7/scanf_examples.c @@ -0,0 +1,36 @@ +/* Chapter 7 : Examples of using scanf */ + +/* A program to show how to use scanf to read various types of values (float/integer)*/ + +#include +#include + + +int main( void ) +{ + + int v1; /* An integer */ + float fv; /* A float */ + + + /* Reading an integer */ + printf("\nPlease enter an integer and press return "); + scanf("%d",&v1); + printf("\nThe value you entered was %d ",v1); + + /* Reading a floating point number */ + printf("\nPlease enter a floating point number & press return "); + scanf("%f",&fv); + printf("\nThe value you typed in was %f ",fv); + + + /* Reading all three in one go */ + printf("\nPlease enter your age and salary\n"); + scanf("%d %f", &v1, &fv); + + printf("\nYou are %d years old ",v1); + printf("\nAnd earn %f per year ",fv); + + return 0; /* End of the program */ + +} diff --git a/LC7/scanf_string.c b/LC7/scanf_string.c new file mode 100644 index 0000000..12874b5 --- /dev/null +++ b/LC7/scanf_string.c @@ -0,0 +1,23 @@ +/* EEEE1001/H61CAE - Chapter 7 */ + +#include +#include + + +int main(void) +{ + + char MyName[50]; /* Define a string of 50 characters */ + + + /* Prompt for and read in a string */ + printf("\nPlease enter your name & press return "); + scanf("%s",MyName); + + /* Display fixed text and string on the screen */ + printf("\nHello %s ",MyName); + + + return 0; /* End of the program */ + +} diff --git a/LC8/complex_if.c b/LC8/complex_if.c new file mode 100644 index 0000000..3d70e30 --- /dev/null +++ b/LC8/complex_if.c @@ -0,0 +1,35 @@ +/* EEEE1001/H61CAE Chapter 8 */ + +/* Example of a simple if statements */ + + +#include /* Standard include files */ +#include +#include + + +int main( void ) +{ + + int a = 1, b = 6; // Define and intialise variables + + + if ( (a == 1 ) && ( b == 6 ) ) + { + printf("a is 1 AND b is 6\n" ); + } + + if ( (a == 1 ) || ( b == 99 ) ) + { + printf("a is 1 OR b is 99\n" ); + } + + if (( b < a ) || ( a == 7 ) ) + { + printf ("The value of b is less than a OR a is 7\n"); + } + + + return 0; + +} diff --git a/LC8/if_else_if_else.c b/LC8/if_else_if_else.c new file mode 100644 index 0000000..40bb1bf --- /dev/null +++ b/LC8/if_else_if_else.c @@ -0,0 +1,35 @@ +/* Chapter 8 */ + +/* Example of a simple if statements */ + + +#include /* Standard include files */ +#include +#include + + +int main( void ) +{ + + int x = 55; // Define and intialise variable + + + if ( x == 2 ) + { + printf ("The value of x was 2\n"); + printf ("I will now do something\n"); + } + else if ( x == 3 ) + { + printf ("The value of x was 3\n"); + printf ("I will now do something else\n"); + } + else + { + printf ("x is neither 2 or 3; I will do this instead!\n"); + } + + + return 0; + +} diff --git a/LC8/if_equals_example.c b/LC8/if_equals_example.c new file mode 100644 index 0000000..659589c --- /dev/null +++ b/LC8/if_equals_example.c @@ -0,0 +1,37 @@ +/* EEEE1001/H61CAE Chapter 8 */ + +/* Incorrect use of a single equals sign in an if statement */ + + +#include /* Standard include files */ +#include +#include + + +int main( void ) +{ + + int c = 6; // Define variable as initialise with value + + printf ("\nThe current value in c is %d\n",c); // Display value in c + + // This is correct + if ( c == 1 ) + { + printf("\nThe value of c is 1\n" ); + } + + printf ("\nThe current value in c is %d\n",c); // Display value in c + + // This is WRONG - it is setting c to one, this value is then tested + // and, as non-zero, is considered to be TRUE + if ( c = 1 ) + { + printf("\nThe value of c is 1\n" ); + } + + printf ("\nThe current value in c is %d\n",c); // Display value in c + + return 0; + +} diff --git a/LC8/if_examples.c b/LC8/if_examples.c new file mode 100644 index 0000000..04c8d9e --- /dev/null +++ b/LC8/if_examples.c @@ -0,0 +1,34 @@ +/* Chapter 8 */ + +/* Example of a simple if statements */ + + +#include /* Standard include files */ +#include +#include + + +int main( void ) +{ + + int a = 1, b = 6; // Define and intialise variables + + + if ( a == 1 ) + { + printf("\nThe value of a is 1\n" ); + } + + if ( a < b ) + { + printf ("The value of a is less than b\n"); + } + + if ( b < a ) + { + printf ("The value of b is less than a\n"); + } + + return 0; + +} diff --git a/LC8/switch_1.c b/LC8/switch_1.c new file mode 100644 index 0000000..65afe5e --- /dev/null +++ b/LC8/switch_1.c @@ -0,0 +1,31 @@ +/* EEEE1001/H61CAE Chapter 8 */ + +/* Example of a simple if statements */ + + +#include /* Standard include files */ +#include + +int main( void ) +{ + + int a; /* Define an int */ + scanf ("%d",&a); /* Get value */ + switch (a) /* Start of switch */ + { + case 1: + printf("Hi"); /* Case 1 */ + break; + + case 2: + printf("Bye"); /* Case 2 */ + break; + + default : + printf("Err"); /* Default */ + break; + } + + return 0; + +} \ No newline at end of file diff --git a/LC8/switch_2.c b/LC8/switch_2.c new file mode 100644 index 0000000..76719cf --- /dev/null +++ b/LC8/switch_2.c @@ -0,0 +1,34 @@ +/* Chapter 8 */ + +/* Example of a simple if statements */ + + +#include /* Standard include files */ +#include + +#include /* Required for getch() */ + +int main( void ) +{ + + char c; /* Define an char */ + c = getch(); /* Get value - no return required*/ + + switch (c) /* Start of switch */ + { + case '1': + printf("Hi"); /* Case 1 */ + break; + + case '2': + printf("Bye"); /* Case 2 */ + break; + + default : + printf("Err"); /* Default */ + break; + } + + return 0; + +} diff --git a/LC8/switch_3.c b/LC8/switch_3.c new file mode 100644 index 0000000..7b6ed51 --- /dev/null +++ b/LC8/switch_3.c @@ -0,0 +1,33 @@ +/* EEEE1001/H61CAE Chapter 8 */ + +/* Example of a simple if statements */ + + +#include /* Standard include files */ +#include + +#include /* Required for getch() */ + +int main( void ) +{ + + char c; /* Define an char */ + c = getch(); /* Get value - no return required*/ + + switch (c) /* Start of switch */ + { + case '1': + printf("Hi"); /* Case 1 */ + + case '2': + printf("Bye"); /* Case 2 */ + break; + + default : + printf("Err"); /* Default */ + break; + } + + return 0; + +} diff --git a/LC9/do_while_example.c b/LC9/do_while_example.c new file mode 100644 index 0000000..7af177c --- /dev/null +++ b/LC9/do_while_example.c @@ -0,0 +1,42 @@ +/* + This program shows how to use a DO/WHILE statement to keep displaying + the key pressed on the screen. It terminates when the 'q' key is + pressed + + James Bonnyman +*/ + +#include /* Standard include files */ +#include +#include + +int main(void) +{ + + char x; /* Define a variable of type char */ + + + /* We now use the while statement. The 'loop' continues to execute */ + /* until such time as the expression in the brackets becomes false */ + + /* we use != , meaning 'Not Equal to' as the test, as we wish the */ + /* the statements to be executed every time we press a key other */ + /* than 'q'. Once we press 'q' the code continues on to the next */ + /* statement */ + + /* Note: This time the getch is in the while expression */ + + + do + { + x = getch(); + /* Display a message on the screen */ + printf("\nThe key you pressed was the %c key",x ); + } + while ( x != 'q' ); + + // All done :-) + + return 0; + +} diff --git a/LC9/for_loops.c b/LC9/for_loops.c new file mode 100644 index 0000000..54bccc5 --- /dev/null +++ b/LC9/for_loops.c @@ -0,0 +1,42 @@ +/* Example Program */ + +/* + This program shows how to write a simple loop that counts up + from zero to 9. + + A second loop then counts from 10 down to 1 + + Remember + + j++ means j = j + 1 + j-- means j = j - 1 + +*/ + +#include /* Standard include files */ +#include + + +int main(void) +{ + + char j; /* Define a variable of type integer */ + + + /* The count up loop */ + printf("\nCounting Up ... "); + + for ( j = 0 ; j < 10 ; j++ ) + printf("\nThe value of j is %d",j ); + + + /* The count down loop */ + + printf ("\nCounting Down .. "); + for ( j = 10 ; j > 0 ; j-- ) + printf("\nThe value of j is %d",j ); + + + return 0; + +} diff --git a/LC9/while_example_version_1.c b/LC9/while_example_version_1.c new file mode 100644 index 0000000..1dde928 --- /dev/null +++ b/LC9/while_example_version_1.c @@ -0,0 +1,47 @@ +/* + This program shows how to use a WHILE statment to keep displaying + the key pressed on the screen. It terminates when the 'q' key is + pressed + + +*/ + +#include /* Standard include files */ +#include +#include + + +int main( void ) +{ + + char x; /* Define a variable of type char */ + + printf("Press a key "); + x = getch(); /* Use getch to read a keypress and store the */ + /* result in 'x'. We do this initially so that */ + /* there is a value in 'x' for the while statment */ + /* to consider the first time round */ + + /* We now use the while statement. The 'loop' continues to execute */ + /* until such time as the expression in the brackets becomes false */ + + /* we use != , meaning 'Not Equal to' as the test, as we wish the */ + /* the statements to be executed every time we press a key other */ + /* than 'q'. Once we press 'q' the code continues on to the next */ + /* statement */ + + while ( x != 'q' ) + { + /* Display a message on the screen */ + printf("\nThe key you pressed was the %c key\n",x ); + + /* Get a new value for X, if we do not the 'expression' will never */ + /* change, and the loop will go for ever */ + + printf( "Press another key or q to quit \n"); + x = getch(); + + } + return 0; + +} diff --git a/LC9/while_example_version_2.c b/LC9/while_example_version_2.c new file mode 100644 index 0000000..5978fb2 --- /dev/null +++ b/LC9/while_example_version_2.c @@ -0,0 +1,37 @@ +/* + This program shows how to use a WHILE statment to keep displaying + the key pressed on the screen. It terminates when the 'q' key is + pressed + +*/ + +#include /* Standard include files */ +#include +#include + +int main(void) +{ + + char x; /* Define a variable of type char */ + + + /* We now use the while statement. The 'loop' continues to execute */ + /* until such time as the expression in the brackets becomes false */ + + /* we use != , meaning 'Not Equal to' as the test, as we wish the */ + /* the statements to be executed every time we press a key other */ + /* than 'q'. Once we press 'q' the code continues on to the next */ + /* statement */ + + /* Note: This time the getch is in the while expression */ + + + while ( ( x = getch() ) != 'q' ) + { + /* Display a message on the screen */ + printf("\nThe key you pressed was the %c key",x ); + } + + return 0; + +}