diff --git a/Appendix1/Assertion/Assertions.c b/Appendix1/Assertion/Assertions.c deleted file mode 100644 index 40a0388..0000000 --- a/Appendix1/Assertion/Assertions.c +++ /dev/null @@ -1,18 +0,0 @@ -#include -#include - -int main() -{ - int x = 7; - - /* Some big code in between and let's say x -       is accidentally changed to 9 */ - x = 9; - - // Programmer assumes x to be 7 in rest of the code - assert(x==7); - - /* Rest of the code */ - - return 0; -} \ No newline at end of file diff --git a/Appendix1/CodeBlocking/Blocking.c b/Appendix1/CodeBlocking/Blocking.c deleted file mode 100644 index ac55f27..0000000 --- a/Appendix1/CodeBlocking/Blocking.c +++ /dev/null @@ -1,45 +0,0 @@ -#include - -#define BLOCKSIZE (8) - -int main(void) -{ - int i = 0; - int limit = 19; /* could be anything */ - int blocklimit; - - /* The limit may not be divisible by BLOCKSIZE, - * go as near as we can first, then tidy up. - */ - blocklimit = (limit / BLOCKSIZE) * BLOCKSIZE; - /* unroll the loop in blocks of 8 */ - while( i < blocklimit ) - { - printf("process(%d)\n", i); - printf("process(%d)\n", i+1); - printf("process(%d)\n", i+2); - printf("process(%d)\n", i+3); - printf("process(%d)\n", i+4); - printf("process(%d)\n", i+5); - printf("process(%d)\n", i+6); - printf("process(%d)\n", i+7); - - /* update the counter */ - i += 8; - } - if( i < limit ) - { - /* Jump into the case at the place that will allow - * us to finish off the appropriate number of items. */ - switch( limit - i ) - { - case 7 : printf("process(%d)\n", i); i++; - case 6 : printf("process(%d)\n", i); i++; - case 5 : printf("process(%d)\n", i); i++; - case 4 : printf("process(%d)\n", i); i++; - case 3 : printf("process(%d)\n", i); i++; - case 2 : printf("process(%d)\n", i); i++; - case 1 : printf("process(%d)\n", i); - } - } -} \ No newline at end of file diff --git a/Appendix1/DebugAssertion/DebugAssertion.c b/Appendix1/DebugAssertion/DebugAssertion.c deleted file mode 100644 index 6f7c59c..0000000 --- a/Appendix1/DebugAssertion/DebugAssertion.c +++ /dev/null @@ -1,11 +0,0 @@ -# define NDEBUG -#include -# include - -int main() -{ - int x = 7; - assert (x==5); - printf("x = %d\n", x); - return 0; -} \ No newline at end of file diff --git a/Exercises/C10/ex2.c b/Exercises/C10/ex2.c deleted file mode 100644 index ea510fe..0000000 --- a/Exercises/C10/ex2.c +++ /dev/null @@ -1,19 +0,0 @@ -#include -#include - - -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; -} diff --git a/Exercises/C10/ex3.c b/Exercises/C10/ex3.c deleted file mode 100644 index d633fb5..0000000 --- a/Exercises/C10/ex3.c +++ /dev/null @@ -1,36 +0,0 @@ -#include -#include - - -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; -} diff --git a/Exercises/C10/ex4.c b/Exercises/C10/ex4.c deleted file mode 100644 index f060508..0000000 --- a/Exercises/C10/ex4.c +++ /dev/null @@ -1,42 +0,0 @@ -#include - -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; -} diff --git a/Exercises/C10/ex5.c b/Exercises/C10/ex5.c deleted file mode 100644 index 0899684..0000000 --- a/Exercises/C10/ex5.c +++ /dev/null @@ -1,45 +0,0 @@ -#include - -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; -} diff --git a/Exercises/C10/function_example.c b/Exercises/C10/function_example.c deleted file mode 100644 index 23e97c3..0000000 --- a/Exercises/C10/function_example.c +++ /dev/null @@ -1,43 +0,0 @@ -#include -#include -#include - -// 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); -} \ No newline at end of file diff --git a/Exercises/C10/void_function_example.c b/Exercises/C10/void_function_example.c deleted file mode 100644 index 8710c4e..0000000 --- a/Exercises/C10/void_function_example.c +++ /dev/null @@ -1,38 +0,0 @@ -#include -#include - -// 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 -} diff --git a/Exercises/C11/array_loop_example_1.c b/Exercises/C11/array_loop_example_1.c deleted file mode 100644 index 06027a1..0000000 --- a/Exercises/C11/array_loop_example_1.c +++ /dev/null @@ -1,16 +0,0 @@ -#include -#include - -int main(void) // Main : Execution starts here... -{ - // Declare variables - pre-populate the array - int Ages[10] = {12,34,23,11,8,19,6,44,9,16}; - int i; - - // Loop from 0 to 9 inclusive - for ( i = 0 ; i < 10 ; i++ ) - printf ("Item %d contains value %d\n",i ,Ages[i]); - - // Exit the application - return 0; -} \ No newline at end of file diff --git a/Exercises/C11/array_loop_example_2.c b/Exercises/C11/array_loop_example_2.c deleted file mode 100644 index 5979dab..0000000 --- a/Exercises/C11/array_loop_example_2.c +++ /dev/null @@ -1,25 +0,0 @@ -#include -#include - -// Main () - execution starts here -int main (void) -{ - // Declare variables - int SampleData[10]; - int a=0; - - // Show initial value of a - printf ("Values in a is %d \n", a); - - // Change an array item that IS NOT DEFINED - SampleData[10] = 20; - - - // See how thi affect the value in a - printf ("Values in a is %d \n", a); - - // Note: we can retieive this 'invalid' value - it is however 'a' we are getting - printf ("Values in SampleData[10] is %d \n", SampleData[10]); - - return (0); // Exit indicating sucess -} \ No newline at end of file diff --git a/Exercises/C11/ex2.c b/Exercises/C11/ex2.c deleted file mode 100644 index cf5dbae..0000000 --- a/Exercises/C11/ex2.c +++ /dev/null @@ -1,15 +0,0 @@ -#include - -#define ARRAY_LEN 90 - -int main() { - int i; - float arr[ARRAY_LEN]; - - for (i = 0; i < ARRAY_LEN; i++) { - arr[i] = (float)i; - } - for (i = 0; i < ARRAY_LEN; i++) { - printf("%d %f\n", i, arr[i]); - } -} diff --git a/Exercises/C11/ex3.c b/Exercises/C11/ex3.c deleted file mode 100644 index b8fd202..0000000 --- a/Exercises/C11/ex3.c +++ /dev/null @@ -1,21 +0,0 @@ -#include -#include - -#define ARRAY_LEN 90 - -float degrees_to_radians(float degrees) { - return M_PI * degrees / 180.0; -} - -int main() { - int i; - float arr[ARRAY_LEN]; - - for (i = 0; i < ARRAY_LEN; i++) { - arr[i] = degrees_to_radians((float)i); - } - - for (i = 0; i < ARRAY_LEN; i++) { - printf("%d %f\n", i, arr[i]); - } -} diff --git a/Exercises/C11/initialise_string_example_1.c b/Exercises/C11/initialise_string_example_1.c deleted file mode 100644 index f19d9bb..0000000 --- a/Exercises/C11/initialise_string_example_1.c +++ /dev/null @@ -1,16 +0,0 @@ -#include -#include - -// Main : Execution starts here... -int main(void) -{ - // Declare variable - pre-populate the array - char msg[] = {'H','i',' ','W','o','r','l','d','\0'}; - - // Output with printf - printf ("The text is: %s\n", msg); - - puts(msg); // We could also use puts to display the string - - return 0; // Exit the application -} \ No newline at end of file diff --git a/Exercises/C11/initialise_string_example_2.c b/Exercises/C11/initialise_string_example_2.c deleted file mode 100644 index 0df3449..0000000 --- a/Exercises/C11/initialise_string_example_2.c +++ /dev/null @@ -1,15 +0,0 @@ -#include -#include - -int main(void) // Main : Execution starts here... -{ - // Declare variable - pre-populate the array - char msg[] = "Hello World"; - - // Output with printf - printf ("The text is: %s\n", msg); - - puts(msg); // We could also use puts to display the string - - return 0; // Exit the application -} \ No newline at end of file diff --git a/Exercises/C12/scope_of_variables.c b/Exercises/C12/scope_of_variables.c deleted file mode 100644 index d827b6f..0000000 --- a/Exercises/C12/scope_of_variables.c +++ /dev/null @@ -1,20 +0,0 @@ -void SampleFunction1 ( int x ) -{ - int i,j,k; - return; -} - -void SampleFunction2 ( int x ) -{ - int i,j,k; - SampleFunction1(x); - return; -} - -// Main : Execution starts here... -int main(void) -{ - int i = 1; - SampleFunction2(i); - return 0; -} \ No newline at end of file diff --git a/Exercises/C13/AccessingViaPointers/accessing_via_pointers.c b/Exercises/C13/AccessingViaPointers/accessing_via_pointers.c deleted file mode 100644 index 52fcf7a..0000000 --- a/Exercises/C13/AccessingViaPointers/accessing_via_pointers.c +++ /dev/null @@ -1,25 +0,0 @@ -#include -#include - -int main (void ) -{ - // Declare a in integer - int c,d; - - // Declar and integer pointer - int *ptrC; - - // Some assgnments - c = 10; // C now contains the value 10 - ptrC = &c; // ptrC now 'Points' to c - - // Get the value of c via the pointer and store in d - d = *ptrC; // d now contains 10 - printf ("\nThe value in d is %d", d); - - // Change the value of c via the pointer ptrC - *ptrC = 1; //c now contains 1 - printf ("\nThe value in c is %d", c); - - return 0; // exit -} \ No newline at end of file diff --git a/Exercises/C13/assigning_pointers.c b/Exercises/C13/assigning_pointers.c deleted file mode 100644 index 32da375..0000000 --- a/Exercises/C13/assigning_pointers.c +++ /dev/null @@ -1,24 +0,0 @@ -#include -#include - -int main (void ) -{ - // Integer variables - int a, ValueB, d; - - // integer pointers - int *ptrA=&a, *B=&ValueB, *Data=&d; - - // Float variables - float f,y,z; - - // Float pointers - float *pf=&f , *q=&y, *Zvalue=&z; - - // We could also do this on separate lines e.g. - int SomeData; - int *Another; - Another = &SomeData; - - return 0; -} \ No newline at end of file diff --git a/Exercises/C13/ex2.c b/Exercises/C13/ex2.c deleted file mode 100644 index 1b7f012..0000000 --- a/Exercises/C13/ex2.c +++ /dev/null @@ -1,27 +0,0 @@ -#include -#include - -int main (void ) -{ - // Declare a in integer - int c,d; - - // Declar and integer pointer - int *ptrC; - - // Some assgnments - c = 10; // C now contains the value 10 - ptrC = &c; // ptrC now 'Points' to c - - *ptrC = 20; - - // Get the value of c via the pointer and store in d - d = *ptrC; // d now contains 10 - printf ("The value in d is %d\n", d); - - // Change the value of c via the pointer ptrC - *ptrC = 1; //c now contains 1 - printf ("The value in c is %d\n", c); - - return 0; // exit -} diff --git a/Exercises/C13/ex3.c b/Exercises/C13/ex3.c deleted file mode 100644 index 7111359..0000000 --- a/Exercises/C13/ex3.c +++ /dev/null @@ -1,27 +0,0 @@ -#include -#include - -int main (void ) -{ - // Declare a in integer - float c,d; - - // Declar and integer pointer - float *ptrC; - - // Some assgnments - c = 10; // C now contains the value 10 - ptrC = &c; // ptrC now 'Points' to c - - *ptrC = 20; - - // Get the value of c via the pointer and store in d - d = *ptrC; // d now contains 10 - printf ("The value in d is %f\n", d); - - // Change the value of c via the pointer ptrC - *ptrC = 1; //c now contains 1 - printf ("The value in c is %f\n", c); - - return 0; // exit -} diff --git a/Exercises/C14/Makefile b/Exercises/C14/Makefile deleted file mode 100644 index 3924bfd..0000000 --- a/Exercises/C14/Makefile +++ /dev/null @@ -1 +0,0 @@ -include ../../Makefile diff --git a/Exercises/C14/ex1.c b/Exercises/C14/ex1.c deleted file mode 100644 index eebd18d..0000000 --- a/Exercises/C14/ex1.c +++ /dev/null @@ -1,32 +0,0 @@ -#include -#include - -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; -} diff --git a/Exercises/C14/ex2.c b/Exercises/C14/ex2.c deleted file mode 100644 index 28d0a70..0000000 --- a/Exercises/C14/ex2.c +++ /dev/null @@ -1,29 +0,0 @@ -#include -#include - - -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", °rees); - - 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); -} diff --git a/Exercises/C14/ex3.c b/Exercises/C14/ex3.c deleted file mode 100644 index de55733..0000000 --- a/Exercises/C14/ex3.c +++ /dev/null @@ -1,33 +0,0 @@ -#include -#include - - -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", °rees_start); - printf("End: "); - scanf("%d", °rees_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); - } - - -} diff --git a/Exercises/C14/quadratic_solver.c b/Exercises/C14/quadratic_solver.c deleted file mode 100644 index 37f01e4..0000000 --- a/Exercises/C14/quadratic_solver.c +++ /dev/null @@ -1,55 +0,0 @@ -#include -#include -#include - -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 -} \ No newline at end of file diff --git a/Exercises/C14/quadratic_solver_function.c b/Exercises/C14/quadratic_solver_function.c deleted file mode 100644 index 31158dc..0000000 --- a/Exercises/C14/quadratic_solver_function.c +++ /dev/null @@ -1,24 +0,0 @@ -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; -} \ No newline at end of file diff --git a/Exercises/C15/pointer_array_example_1.c b/Exercises/C15/pointer_array_example_1.c deleted file mode 100644 index ac47055..0000000 --- a/Exercises/C15/pointer_array_example_1.c +++ /dev/null @@ -1,20 +0,0 @@ -#include -#include - -int main(void) -{ - - // Declare and populate an integer array - int MyArray[10] = {2,4,6,8,10,12,14,18,20}; - - // Declate an integer pointer - int *pI; - - // Get the start address by asking for the address iof array item [0] - pI = &MyArray[0]; - - // Or, use the fact the array name on its own is the start address of the array - pI = MyArray; - - return 0; // Exit -} \ No newline at end of file diff --git a/Exercises/C15/pointer_array_example_2.c b/Exercises/C15/pointer_array_example_2.c deleted file mode 100644 index 9c9630f..0000000 --- a/Exercises/C15/pointer_array_example_2.c +++ /dev/null @@ -1,22 +0,0 @@ -#include -#include - -int main(void) -{ - // Declare an integer array and an integer pointer - int MyArray[10] = {2,4,6,8,10,12,14,16,18,20}; - int *pI; - - // Get the start address by asking for the address iof array item [0] - pI = &MyArray[0]; // Or use: pI = MyArray; - - // Display the 1st item in the array, first be accessing rhe array - printf("The value at array item [0] is %d\n", MyArray[0]); - - // Since the pointer points to the address of the 1st item we can - // access it as we would for a pointer pointing to any single variable - - printf("The value at the memory address held in pI is %d\n", *pI); - - return 0; // Exit -} \ No newline at end of file diff --git a/Exercises/C15/pointer_array_example_3.c b/Exercises/C15/pointer_array_example_3.c deleted file mode 100644 index 046e249..0000000 --- a/Exercises/C15/pointer_array_example_3.c +++ /dev/null @@ -1,23 +0,0 @@ -#include -#include - -int main(void) -{ - - // Declare an integer array and an integer pointer - int MyArray[10] = {2,4,6,8,10,12,14,16,18,20}; - int *pI; - int i; - - // Get the start address by asking for the address iof array item [0] - pI = &MyArray[0]; // or use: pI = MyArray; - - // Use loop to display values - for ( i = 0 ; i < 10 ; i++ ) - { - printf ("Value at index %d (direct access to the arrays) is: %d\n", i, MyArray[i]); - printf ("Value at index %d (access via the pointer) is: %d\n", i, pI[i]); - } - - return 0; // Exit -} \ No newline at end of file diff --git a/Exercises/C15/pointer_array_example_4.c b/Exercises/C15/pointer_array_example_4.c deleted file mode 100644 index 519d6bf..0000000 --- a/Exercises/C15/pointer_array_example_4.c +++ /dev/null @@ -1,33 +0,0 @@ -#include -#include - -int main(void) -{ - // Declare an integer array and an integer pointer - int MyArray[10]; - int *pI; - int i; - - // Get the start address by asking for the address iof array item [0] - pI = &MyArray[0]; // or use: pI = MyArray; - - // Use loop to display values - for ( i = 0 ; i < 10 ; i++ ) - { - // Set the value then use the increment operator to move the pointer - // to the next memory location. you can picture this as two steps: ' - // - // *pI = 5 + 4*i; - // then - // *pI++; - - *pI++ = 5 + 4*i; // set value at index[i] to 5+4*i - } - - // Display the values placed in the array - for ( i = 0 ; i < 10 ; i++) - { - printf("%d ", MyArray[i]); - } - return 0; // Exit -} \ No newline at end of file diff --git a/Exercises/C16/alloc_example_1.c b/Exercises/C16/alloc_example_1.c deleted file mode 100644 index a200700..0000000 --- a/Exercises/C16/alloc_example_1.c +++ /dev/null @@ -1,17 +0,0 @@ -#include -#include - -int main(void) -{ - - // Declare an integer array and an integer pointer - int *pData; - - // Using malloc - pData = malloc ( 10000 * sizeof (int)); - - // Using calloc - pData = calloc ( 10000 , sizeof (int)); - - return 0; // Exit -} \ No newline at end of file diff --git a/Exercises/C16/alloc_example_2.c b/Exercises/C16/alloc_example_2.c deleted file mode 100644 index 9b7aa35..0000000 --- a/Exercises/C16/alloc_example_2.c +++ /dev/null @@ -1,14 +0,0 @@ -#include -#include - -int main(void) -{ - - // Declare an integer array and an integer pointer - int *pData; - - pData = calloc ( 10000 , sizeof (float)); // No warning - pData = (float *)calloc ( 10000 , sizeof (float)); // Warning - - return 0; // Exit -} \ No newline at end of file diff --git a/Exercises/C16/alloc_example_3.c b/Exercises/C16/alloc_example_3.c deleted file mode 100644 index bfa379c..0000000 --- a/Exercises/C16/alloc_example_3.c +++ /dev/null @@ -1,22 +0,0 @@ -#include -#include - -int main(void) -{ - - // Declare an integer array and an integer pointer - int *pData; - - // Using calloc (same approach malloc) - pData = calloc ( 10000 , sizeof (int)); - - if ( pData == NULL) - { - printf ("\nMemory could not be allocated - terminating"); - return -1; // Use minus one as we did not exit successfully - } - - // We have our memory, make use of it here! - - return 0; // Exit successfully -} \ No newline at end of file diff --git a/Exercises/C16/alloc_example_4.c b/Exercises/C16/alloc_example_4.c deleted file mode 100644 index 168b3da..0000000 --- a/Exercises/C16/alloc_example_4.c +++ /dev/null @@ -1,25 +0,0 @@ -#include -#include - -int main(void) -{ - - // Declare an integer array and an integer pointer - int *pData; - - // Using calloc (same approach malloc) - pData = calloc ( 10000 , sizeof (int)); - - if ( pData == NULL) - { - printf ("\nMemory could not be allocated - terminating"); - return -1; // Use minus one as we did not exit sucesfully - } - - // We have our memory, make use of it here! - - // Free up the allocated memoey - free (pData); - - return 0; // Exit sucesfully -} \ No newline at end of file diff --git a/Exercises/C16/ex2.c b/Exercises/C16/ex2.c deleted file mode 100644 index 6137a98..0000000 --- a/Exercises/C16/ex2.c +++ /dev/null @@ -1,18 +0,0 @@ -#include -#include - -int main(void) -{ - // Declare an integer array and an integer pointer - int *pData; - int datlen; - - printf("Length of data to be allocated: "); - scanf("%d", &datlen); - - // Using malloc - pData = malloc ( datlen * sizeof (int)); - - free(pData); - return 0; // Exit -} diff --git a/Exercises/C16/ex3.c b/Exercises/C16/ex3.c deleted file mode 100644 index 9a2323c..0000000 --- a/Exercises/C16/ex3.c +++ /dev/null @@ -1,23 +0,0 @@ -#include -#include - -int main(void) -{ - // Declare an integer array and an integer pointer - int *pData; - int datlen; - - printf("Length of data to be allocated: "); - scanf("%d", &datlen); - - // Using malloc - pData = malloc ( datlen * sizeof (int)); - - for (int i = 0; i < datlen; i++) { - pData[i] = i; - printf("%d %d\n", i, i); - } - - free(pData); - return 0; // Exit -} diff --git a/Exercises/C17/ArraysToFunctions1/arrays_to_functions_example_1.c b/Exercises/C17/ArraysToFunctions1/arrays_to_functions_example_1.c deleted file mode 100644 index 5d31e47..0000000 --- a/Exercises/C17/ArraysToFunctions1/arrays_to_functions_example_1.c +++ /dev/null @@ -1,40 +0,0 @@ -#include -#include - -// Simple function to populate an integer array -void PopulateTheArray ( int Size, int ArrayData[]) -{ - int i; // Variable to use in our loop - - for ( i = 0 ; i < Size ; i++) - { - ArrayData[i] = 2*i + 1; // Treat it like a normal array - } -} - - -// Simple function to display contents of an integer array -void DisplayTheArray ( int Size, int ArrayData[]) -{ - int i; // Variable to use in our loop - - for ( i = 0 ; i < Size ; i++) - { - printf ("Item %d of the array contains %d\n", i, ArrayData[i]); - } -} - -// Main () - execution starts here -int main (void) -{ - int Data[10]; - - // Pass the size of the array and the array to our function - - // remembering that the array name on its own is the base address, - // and so is the same as passing &Data[0] - - PopulateTheArray(10, Data); - DisplayTheArray(10, Data); - - return (0); // Exit indicating sucess -} diff --git a/Exercises/C17/ArraysToFunctions2/arrays_to_functions_example_2.c b/Exercises/C17/ArraysToFunctions2/arrays_to_functions_example_2.c deleted file mode 100644 index 4a76f18..0000000 --- a/Exercises/C17/ArraysToFunctions2/arrays_to_functions_example_2.c +++ /dev/null @@ -1,55 +0,0 @@ -#include -#include - -// Simple function to populate an integer array -void PopulateTheArray ( int Size, int ArrayData[]) -{ - int i; // Variable to use in our loop - - for ( i = 0 ; i < Size ; i++) - { - ArrayData[i] = 2*i + 1; // Treat it like a normal array - } -} -// Simple function do display contents an integer array -void DisplayTheArray ( int Size, int ArrayData[]) -{ - int i; // Variable to use in our loop - - for ( i = 0 ; i < Size ; i++) - { - printf ("Item %d of the array contains %d\n", i, ArrayData[i]); - } -} -// Main () - execution starts here -int main (void) -{ - - int iSizeForArray; - int *pData; // A pointer to hold the base address of out array - - // Ask for the size of the array and store result - - printf("\nPlease enter the size of the array to dynamically allocate"); - scanf ("%d", &iSizeForArray); - - // Use calloc with checking - pData = calloc ( iSizeForArray, sizeof (int)); - - // Check we got the memory - if ( pData == NULL) - { - printf ("\nSorry, I could not allocate the memory, bye!"); - return -1; - } - - // Pass the size, iSizeForArray) and the pointer created - // which points to the start of the sucesfully allocated memory - - PopulateTheArray(iSizeForArray, pData); - DisplayTheArray(iSizeForArray, pData); - - free (pData); // Free up the memory before exiting - - return (0); // Exit indicating sucess -} \ No newline at end of file diff --git a/Exercises/C17/ex2.c b/Exercises/C17/ex2.c deleted file mode 100644 index c26b245..0000000 --- a/Exercises/C17/ex2.c +++ /dev/null @@ -1,62 +0,0 @@ -#include -#include -#include - - -float degrees_to_radians(float degrees) { - return M_PI * degrees / 180.0; -} - - -// Simple function to populate an integer array -void PopulateTheArray ( int Size, float ArrayData[]) -{ - int i; // Variable to use in our loop - - for ( i = 0 ; i < Size ; i++) - { - ArrayData[i] = degrees_to_radians(i); // Treat it like a normal array - } -} -// Simple function do display contents an integer array -void DisplayTheArray ( int Size, float ArrayData[]) -{ - int i; // Variable to use in our loop - - for ( i = 0 ; i < Size ; i++) - { - printf ("%d\t%0.3f\n", i, ArrayData[i]); - } -} -// Main () - execution starts here -int main (void) -{ - - int iSizeForArray; - float *pData; // A pointer to hold the base address of out array - - // Ask for the size of the array and store result - - printf("Please enter the size of the array to dynamically allocate: "); - scanf ("%d", &iSizeForArray); - - // Use calloc with checking - pData = calloc( iSizeForArray, sizeof (float)); - - // Check we got the memory - if ( pData == NULL) - { - printf ("Sorry, I could not allocate the memory, bye!\n"); - return -1; - } - - // Pass the size, iSizeForArray) and the pointer created - // which points to the start of the sucesfully allocated memory - - PopulateTheArray(iSizeForArray, pData); - DisplayTheArray(iSizeForArray, pData); - - free(pData); // Free up the memory before exiting - - return (0); // Exit indicating sucess -} diff --git a/Exercises/C18/BinaryFile/binary_file_example.c b/Exercises/C18/BinaryFile/binary_file_example.c deleted file mode 100644 index 00a2023..0000000 --- a/Exercises/C18/BinaryFile/binary_file_example.c +++ /dev/null @@ -1,64 +0,0 @@ -#include -#include - -// Main () - execution starts here -int main (void) -{ - - // Declate file stream variables - FILE *fInput, *fOutput; - - // Other variables needed - int i; - int SampleArray[10] = {1,2,3,4,5,6,7,8,9,10}; - float f = 23.4; - - // Try and open the binary "numbers.dat" (in the current directory) file for writing - fOutput = fopen ("numbers.dat", "wb"); - - // Check we were able to open the file - if ( fOutput == NULL) - { - printf ("\nThe file could not be opened for writing, exiting"); - return -1; - } - - // Write out a single float to the binary file - fwrite ( &f, sizeof(float), 1 , fOutput); - - // Now the entire array on one go - fwrite ( SampleArray, sizeof(int), 10 , fOutput); - - // And close the file - fclose (fOutput); - - - // Try and open the binary "numbers.dat" (in the current directory) file for reading - fInput = fopen ("numbers.dat", "rb"); - - // Check we were able to open the file - if ( fInput== NULL) - { - printf ("\nthe file could not be opened for reading, exiting"); - return -1; - } - - // Read a single float from the binary file into f - fread ( &f, sizeof(float), 1 , fOutput); - - // Now read the entire array on one go - fread ( SampleArray, sizeof(int), 10 , fOutput); - - // Display the values read from the file on the screen - printf ("The value read into f is %f\n", f);; - for ( i = 0 ; i < 10 ; i++) - { - printf ("Item %d of the array contains %d\n",i, SampleArray[i]); - } - - // And close the file - fclose (fInput); - - - return (0); // Exit indicating sucess -} \ No newline at end of file diff --git a/Exercises/C18/BinaryFile/numbers.dat b/Exercises/C18/BinaryFile/numbers.dat deleted file mode 100644 index a7a19b2..0000000 Binary files a/Exercises/C18/BinaryFile/numbers.dat and /dev/null differ diff --git a/Exercises/C18/FileOpen/file_open_example.c b/Exercises/C18/FileOpen/file_open_example.c deleted file mode 100644 index 6cc5e3e..0000000 --- a/Exercises/C18/FileOpen/file_open_example.c +++ /dev/null @@ -1,52 +0,0 @@ -#include -#include - -// Main () - execution starts here -int main (void) -{ - - // Declate file stream variables - FILE *fInput, *fOutput, *fRecords; - - - // Try and open the text "sample.txt" (in the current directory) file for reading - fInput = fopen ("sample.txt", "r"); - - // Check we were able to open the file - if ( fInput == NULL) - { - printf ("\nthe file could not be opened"); - return -1; // Exit as unsuccessful - } - - fclose (fInput); // Close the file - - // Try and open the binary "samples.dat" (in the current directory) file for writing - // if a file of this name already exists it will be deleted - fOutput = fopen ("samples.dat", "wb"); - - // Check we were able to open the file - if ( fOutput == NULL) - { - printf ("\nthe file could not be opened"); - return -1; // Exit as unsuccessful - } - - fclose (fOutput); // Close the file - - // Open, for appending, the text file "records.txt". If the file does not already - // exists, a new one of this name will be created (as if "w") were the mocde - fRecords = fopen ("records.txt", "a"); - - // Check we were able to open the file - if ( fRecords == NULL) - { - printf ("\nthe file could not be opened"); - return -1; // Exit as unsuccessful - } - - fclose (fRecords); - - - return (0); // Exit indicating sucess -} \ No newline at end of file diff --git a/Exercises/C18/TextFile/numbers.txt b/Exercises/C18/TextFile/numbers.txt deleted file mode 100644 index f00c965..0000000 --- a/Exercises/C18/TextFile/numbers.txt +++ /dev/null @@ -1,10 +0,0 @@ -1 -2 -3 -4 -5 -6 -7 -8 -9 -10 diff --git a/Exercises/C18/TextFile/text_file_example.c b/Exercises/C18/TextFile/text_file_example.c deleted file mode 100644 index 9e29c10..0000000 --- a/Exercises/C18/TextFile/text_file_example.c +++ /dev/null @@ -1,55 +0,0 @@ -#include -#include - -// Main () - execution starts here -int main (void) -{ - // Declate file stream variables - FILE *fInput, *fOutput; - - // Other variables needed - int i,d; - - // Try and open the text "sample.txt" (in the current directory) file for writing - fOutput = fopen ("numbers.txt", "w"); - - // Check we were able to open the file - if ( fOutput == NULL) - { - printf ("\nthe file could not be opened for writing, exiting"); - return -1; - } - - // Use a loop to write values to the newly created file - for ( i = 1 ; i <= 10 ; i++) - { - fprintf (fOutput, "%d\n", i); - } - - // And close the file - fclose (fOutput); - - // Try and open the binary "numbers " (in the current directory) file for reading - - fInput = fopen ("numbers.txt", "r"); - - // Check we were able to open the file - if ( fInput == NULL) - { - printf ("\nthe file could not be opened for reading, exiting"); - return -1; - } - - // Read, line by line the 10 values written into variable d - // and then display the contents of d on the screen - for ( i = 1 ; i <= 10 ; i++) - { - fscanf (fInput, "%d", &d); - printf ("Value read from file %d\n",d); - } - - // And close the file - fclose (fInput); - - return (0); // Exit indicating success -} \ No newline at end of file diff --git a/Exercises/C18/ex2.c b/Exercises/C18/ex2.c deleted file mode 100644 index 1217fe9..0000000 --- a/Exercises/C18/ex2.c +++ /dev/null @@ -1,55 +0,0 @@ -#include -#include - -// Main () - execution starts here -int main (void) -{ - // Declate file stream variables - FILE *fInput, *fOutput; - - // Other variables needed - int i, d, d2; - - // Try and open the text "sample.txt" (in the current directory) file for writing - fOutput = fopen ("numbers.txt", "w"); - - // Check we were able to open the file - if ( fOutput == NULL) - { - printf ("The file could not be opened for writing, exiting\n"); - return -1; - } - - // Use a loop to write values to the newly created file - for ( i = 1 ; i <= 10 ; i++) - { - fprintf (fOutput, "%d %d\n", i, i*i); - } - - // And close the file - fclose (fOutput); - - // Try and open the binary "numbers " (in the current directory) file for reading - - fInput = fopen ("numbers.txt", "r"); - - // Check we were able to open the file - if ( fInput == NULL) - { - printf ("The file could not be opened for reading, exiting\n"); - return -1; - } - - // Read, line by line the 10 values written into variable d - // and then display the contents of d on the screen - for ( i = 1 ; i <= 10 ; i++) - { - fscanf (fInput, "%d %d", &d, &d2); - printf ("Value read from file: %d %d\n", d, d2); - } - - // And close the file - fclose (fInput); - - return (0); // Exit indicating success -} diff --git a/Exercises/C18/ex3.c b/Exercises/C18/ex3.c deleted file mode 100644 index 2b8dec4..0000000 --- a/Exercises/C18/ex3.c +++ /dev/null @@ -1,60 +0,0 @@ -#include -#include - -// Main () - execution starts here -int main (void) -{ - // Declate file stream variables - FILE *fInput, *fOutput; - // 8x the maximum filename length on most filesystems should be a decent size - char filename[2049] = { 0 }; - - printf("Enter filename: "); - scanf("%2048s", filename); - - // Other variables needed - int i, d, d2; - - // Try and open the text "sample.txt" (in the current directory) file for writing - fOutput = fopen (filename, "w"); - - // Check we were able to open the file - if ( fOutput == NULL) - { - printf ("The file could not be opened for writing, exiting\n"); - return -1; - } - - // Use a loop to write values to the newly created file - for ( i = 1 ; i <= 10 ; i++) - { - fprintf (fOutput, "%d %d\n", i, i*i); - } - - // And close the file - fclose (fOutput); - - // Try and open the binary "numbers " (in the current directory) file for reading - - fInput = fopen ("numbers.txt", "r"); - - // Check we were able to open the file - if ( fInput == NULL) - { - printf ("The file could not be opened for reading, exiting\n"); - return -1; - } - - // Read, line by line the 10 values written into variable d - // and then display the contents of d on the screen - for ( i = 1 ; i <= 10 ; i++) - { - fscanf (fInput, "%d %d", &d, &d2); - printf ("Value read from file: %d %d\n", d, d2); - } - - // And close the file - fclose (fInput); - - return (0); // Exit indicating success -} diff --git a/Exercises/C18/ex4.c b/Exercises/C18/ex4.c deleted file mode 100644 index 42aaf52..0000000 --- a/Exercises/C18/ex4.c +++ /dev/null @@ -1,92 +0,0 @@ -#include -#include -#include - - -float degrees_to_radians(float degrees) { - return M_PI * degrees / 180.0; -} - - -// Simple function to populate an integer array -void PopulateTheArray ( int Size, float ArrayData[]) -{ - int i; // Variable to use in our loop - - for ( i = 0 ; i < Size ; i++) - { - ArrayData[i] = degrees_to_radians(i); // Treat it like a normal array - } -} -// Simple function do display contents an integer array -void DisplayTheArray ( int Size, float ArrayData[]) -{ - int i; // Variable to use in our loop - - for ( i = 0 ; i < Size ; i++) - { - printf ("%d\t%0.3f\n", i, ArrayData[i]); - } -} - -int SaveArray (char *filename, int Size, float ArrayData[]) -{ - FILE *fp; // File Pointer - int i; // Variable to use in our loop - - fp = fopen(filename, "w"); - // if file open failed, return error to caller - if (fp == NULL) { - return 1; - } - - - for ( i = 0 ; i < Size ; i++) - { - fprintf(fp, "%d\t%0.3f\n", i, ArrayData[i]); - } - - // close file, returns 0 on successful closing (therefore function has executed sucessfully) - return fclose(fp); -} -// Main () - execution starts here -int main (void) -{ - char filename[2049]; - int rc, iSizeForArray; - float *pData; // A pointer to hold the base address of out array - - // Ask for the size of the array and store result - - printf("Please enter the size of the array to dynamically allocate: "); - scanf ("%d", &iSizeForArray); - - // Use calloc with checking - pData = calloc( iSizeForArray, sizeof (float)); - - // Check we got the memory - if ( pData == NULL) - { - printf ("Sorry, I could not allocate the memory, bye!\n"); - return -1; - } - - // Pass the size, iSizeForArray) and the pointer created - // which points to the start of the sucesfully allocated memory - - PopulateTheArray(iSizeForArray, pData); - DisplayTheArray(iSizeForArray, pData); - - // Exercise doesn't ask to use function but kind of pointless without - printf("Enter filename: "); - scanf("%2048s", filename); - rc = SaveArray(filename, iSizeForArray, pData); - if (rc != 0) { - printf("Error saving file\n"); - free(pData); - return rc; - } - - free(pData); // Free up the memory before exiting - return (0); // Exit indicating sucess -} diff --git a/Exercises/C18/ex5.c b/Exercises/C18/ex5.c deleted file mode 100644 index a5a8136..0000000 --- a/Exercises/C18/ex5.c +++ /dev/null @@ -1,88 +0,0 @@ -#include -#include -#include - - -float degrees_to_radians(float degrees) { - return M_PI * degrees / 180.0; -} - - -// Simple function to populate an integer array -void PopulateTheArray ( int Size, float ArrayData[]) -{ - int i; // Variable to use in our loop - - for ( i = 0 ; i < Size ; i++) - { - ArrayData[i] = degrees_to_radians(i); // Treat it like a normal array - } -} -// Simple function do display contents an integer array -void DisplayTheArray ( int Size, float ArrayData[]) -{ - int i; // Variable to use in our loop - - for ( i = 0 ; i < Size ; i++) - { - printf ("%d\t%0.3f\n", i, ArrayData[i]); - } -} - -int SaveArrayBinary (char *filename, int Size, float ArrayData[]) -{ - FILE *fp; // File Pointer - int i; // Variable to use in our loop - - fp = fopen(filename, "wb"); - // if file open failed, return error to caller - if (fp == NULL) { - return 1; - } - - // write data - fwrite(ArrayData, sizeof(float), Size, fp); - // close file, returns 0 on successful closing (therefore function has executed sucessfully) - return fclose(fp); -} -// Main () - execution starts here -int main (void) -{ - char filename[2049]; - int rc, iSizeForArray; - float *pData; // A pointer to hold the base address of out array - - // Ask for the size of the array and store result - - printf("Please enter the size of the array to dynamically allocate: "); - scanf ("%d", &iSizeForArray); - - // Use calloc with checking - pData = calloc( iSizeForArray, sizeof (float)); - - // Check we got the memory - if ( pData == NULL) - { - printf ("Sorry, I could not allocate the memory, bye!\n"); - return -1; - } - - // Pass the size, iSizeForArray) and the pointer created - // which points to the start of the sucesfully allocated memory - - PopulateTheArray(iSizeForArray, pData); - DisplayTheArray(iSizeForArray, pData); - - // Exercise doesn't ask to use function but kind of pointless without - printf("Enter filename: "); - scanf("%2048s", filename); - rc = SaveArrayBinary(filename, iSizeForArray, pData); - if (rc != 0) { - printf("Error saving file\n"); - free(pData); - return rc; - } - - free(pData); // Free up the memory before exiting - return (0); // Exit indicating sucess -} diff --git a/Exercises/C18/ex6.c b/Exercises/C18/ex6.c deleted file mode 100644 index f315699..0000000 --- a/Exercises/C18/ex6.c +++ /dev/null @@ -1,51 +0,0 @@ -#include -#include - - -int main() { - FILE *fp; // file pointer - float *data; // data pointer (array of floats) - int i, file_size, arr_size; - char filename[2049]; - - printf("Enter filename to read: "); - scanf("%2048s", filename); - - fp = fopen(filename, "rb"); - if (fp == NULL) { - printf("Failed to open file\n"); - return 1; - } - - // get file size (technically isn't the best way because SEEK_END is undefined behaviour: - // - // "Library implementations are allowed to not meaningfully support SEEK_END - // (therefore, code using it has no real standard portability)." - // ~ https://cplusplus.com/reference/cstdio/fseek/ - // - // " Setting the file position indicator to end-of-file, as with fseek(file, 0, SEEK_END), - // has undefined behavior for a binary stream (because of possible trailing null characters) or - // for any stream with state-dependent encoding that does not assuredly end in the initial shift - // state. " - // ~ https://port70.net/~nsz/c/c11/n1570.html#note268 - // - // it is also limited to 2GB files as it returns a signed integer - // better to use something like this probably: - // https://stackoverflow.com/a/238609 - // - // but louise uses fseek so i will too - fseek(fp, 0, SEEK_END); - file_size = ftell(fp); - arr_size = file_size/sizeof(float); - rewind(fp); - - // allocate memory and read data - data = malloc(file_size); - fread(data, sizeof(float), arr_size, fp); - - for (i = 0; i < arr_size; i++) { - printf("%d %0.3f\n", i, data[i]); - } - - free(data); -} diff --git a/Exercises/C19/DefineExamples/define_example.c b/Exercises/C19/DefineExamples/define_example.c deleted file mode 100644 index a088ad5..0000000 --- a/Exercises/C19/DefineExamples/define_example.c +++ /dev/null @@ -1,18 +0,0 @@ -#define UP 1 -#define DOWN 2 - -int main() -{ - int i = 1; - - if (i == UP ) - { - // Do something - } - - if ( i == DOWN) - { - // Doe something else - } - return 0; -} \ No newline at end of file diff --git a/Exercises/C19/enum_example.c b/Exercises/C19/enum_example.c deleted file mode 100644 index 996041b..0000000 --- a/Exercises/C19/enum_example.c +++ /dev/null @@ -1,22 +0,0 @@ -#include -#include - -enum DOW { sun, mon, tue, wed, thu, fri, sat } ; - -// Main () - execution starts here -int main (void) -{ - enum DOW day; - - /* Code that get a value for 'day' */ - day = tue; - - switch (day) - { - case sun : printf ("Sunday\n") ; break ; - case mon : printf ("Monday\n") ; break ; - case tue : printf ("Tuesday\n") ; break ; - /* etc. */ - } - return (0); // Exit indicating success -} \ No newline at end of file diff --git a/Exercises/C19/ex1.c b/Exercises/C19/ex1.c deleted file mode 100644 index f6dcd03..0000000 --- a/Exercises/C19/ex1.c +++ /dev/null @@ -1,20 +0,0 @@ -#include - - -struct Person { - int age; - char forename[30]; - char surname[50]; -}; - - -int main() { - struct Person p; - - printf("Enter age: "); scanf("%d", &p.age); - printf("Enter forename: "); scanf("%29s", p.forename); - printf("Enter surname: "); scanf("%49s", p.surname); - - printf("p.age=%d p.forename=%s p.surname=%s\n", p.age, p.forename, p.surname); - return 0; -} diff --git a/Exercises/C19/ex2.c b/Exercises/C19/ex2.c deleted file mode 100644 index 51e153c..0000000 --- a/Exercises/C19/ex2.c +++ /dev/null @@ -1,25 +0,0 @@ -#include - - -struct Person { - int age; - char forename[30]; - char surname[50]; -}; - - -void print_person(struct Person p) { - printf("p.age=%d p.forename=%s p.surname=%s\n", p.age, p.forename, p.surname); -} - - -int main() { - struct Person p; - - printf("Enter age: "); scanf("%d", &p.age); - printf("Enter forename: "); scanf("%29s", p.forename); - printf("Enter surname: "); scanf("%49s", p.surname); - - print_person(p); - return 0; -} diff --git a/Exercises/C19/ex3.c b/Exercises/C19/ex3.c deleted file mode 100644 index 96dc041..0000000 --- a/Exercises/C19/ex3.c +++ /dev/null @@ -1,27 +0,0 @@ -#include - - -struct Person { - int age; - int year_of_birth; - char forename[30]; - char surname[50]; -}; - - -void print_person(struct Person p) { - printf("p.age=%d p.year_of_birth=%d p.forename=%s p.surname=%s\n", p.age, p.year_of_birth, p.forename, p.surname); -} - - -int main() { - struct Person p; - - printf("Enter age: "); scanf("%d", &p.age); - printf("Enter birth year: "); scanf("%d", &p.year_of_birth); - printf("Enter forename: "); scanf("%29s", p.forename); - printf("Enter surname: "); scanf("%49s", p.surname); - - print_person(p); - return 0; -} diff --git a/Exercises/C19/ex4.c b/Exercises/C19/ex4.c deleted file mode 100644 index 76b47cf..0000000 --- a/Exercises/C19/ex4.c +++ /dev/null @@ -1,50 +0,0 @@ -#include - - -struct Person { - int age; - int year_of_birth; - char forename[30]; - char surname[50]; -}; - - -void print_person(struct Person p) { - printf("p.age=%d p.year_of_birth=%d p.forename=%s p.surname=%s\n", p.age, p.year_of_birth, p.forename, p.surname); -} - - -int main() { - struct Person p; - FILE *fp; - char choice; - char filename[4097]; - - printf("Read or write a file? (r/w) "); scanf("%c", &choice); - - if (choice == 'w') { - printf("Enter age: "); scanf("%d", &p.age); - printf("Enter birth year: "); scanf("%d", &p.year_of_birth); - printf("Enter forename: "); scanf("%29s", p.forename); - printf("Enter surname: "); scanf("%49s", p.surname); - - printf("Enter filename to save to: "); scanf("%4096s", filename); - fp = fopen(filename, "wb"); - if (fp == NULL) { - printf("Failed to open file for writing\n"); - return 1; - } - fwrite(&p, 1, sizeof(p), fp); - } else { - printf("Enter file to read from: "); scanf("%4096s", filename); - fp = fopen(filename, "rb"); - if (fp == NULL) { - printf("Failed to open file for reading\n"); - return 1; - } - fread(&p, 1, sizeof(p), fp); - } - - print_person(p); - return 0; -} diff --git a/Exercises/C19/static_variable_example.c b/Exercises/C19/static_variable_example.c deleted file mode 100644 index 4fb626c..0000000 --- a/Exercises/C19/static_variable_example.c +++ /dev/null @@ -1,26 +0,0 @@ -#include -#include - -void DisplayHelloWorld (void) -{ - static int k = 0; // Counter for how many times the function is called - - printf ("Hello World\n"); - - // Increment counter and display value - k = k + 1; - printf ("I have now said this %d times\n",k); -} -// Main () - execution starts here -int main (void) -{ - int i; - - // Loop calling out function 10 times - for ( i =0 ; i < 10 ; i++ ) - { - DisplayHelloWorld(); - } - - return (0); // Exit indicating success -} \ No newline at end of file diff --git a/Exercises/C20/ConditionalDirective/conditional_directive_example.c b/Exercises/C20/ConditionalDirective/conditional_directive_example.c deleted file mode 100644 index c2fffe2..0000000 --- a/Exercises/C20/ConditionalDirective/conditional_directive_example.c +++ /dev/null @@ -1,15 +0,0 @@ -#include -#include - -#define DEBUG_ON 0 - -int main(void) -{ -#if DEBUG_ON == 1 - printf("Debug mode - about to do something\n"); -#else - printf("Running in standard mode"); -#endif - - return 0; -} \ No newline at end of file diff --git a/Exercises/C20/FormattingDirective/formatting_directive_example.c b/Exercises/C20/FormattingDirective/formatting_directive_example.c deleted file mode 100644 index 1973848..0000000 --- a/Exercises/C20/FormattingDirective/formatting_directive_example.c +++ /dev/null @@ -1,15 +0,0 @@ -#include -#include - -//#define DEBUG_ON 0 - -int main(void) -{ -#ifdef DEBUG_ON - printf("Debug mode - about to do something\n"); -#else - printf("Running in standard mode"); -#endif - -return 0; -} \ No newline at end of file diff --git a/Exercises/C20/macro_function_example.c b/Exercises/C20/macro_function_example.c deleted file mode 100644 index 3627fc6..0000000 --- a/Exercises/C20/macro_function_example.c +++ /dev/null @@ -1,8 +0,0 @@ -#include -#define MIN(a,b) ((a)<(b)?(a):(b)) - -int main(void) -{ - printf("The minimum value of 10 and 20 is: %d\n", MIN(10,20)); - return 0; -} \ No newline at end of file diff --git a/Exercises/C21/sprintf_example.c b/Exercises/C21/sprintf_example.c deleted file mode 100644 index ce38214..0000000 --- a/Exercises/C21/sprintf_example.c +++ /dev/null @@ -1,19 +0,0 @@ -#include -#include - -int main() -{ - int i; - char FileName[100]; - - for ( i = 1 ; i < 10 ; i++) - { - //'Print' text into string - sprintf(FileName , "file%d.dat" , i); - - // Sidplay the name created - printf("Current file name: %s\n", FileName); - } - - return 0; -} \ No newline at end of file diff --git a/Exercises/C3/hello_world.c b/Exercises/C3/hello_world.c deleted file mode 100644 index 7141063..0000000 --- a/Exercises/C3/hello_world.c +++ /dev/null @@ -1,10 +0,0 @@ -#include -#include - -/* My first program */ -int main(void) -{ - // This is a single line of comment - printf("Hello World\n"); - return 0; -} diff --git a/Exercises/C4/Correct_answer/correct_answer.c b/Exercises/C4/Correct_answer/correct_answer.c deleted file mode 100644 index a9f0f76..0000000 --- a/Exercises/C4/Correct_answer/correct_answer.c +++ /dev/null @@ -1,20 +0,0 @@ -#include -#include - -int main(void) -{ - // Declare and initialise as required - int a = 1,b = 4; - float ans; - - // Perform calculation - // This time a & b are treated as if they are floats - // the result of this float calculation is stored in ans - ans = (float)a / (float)b; - - // Display answer - printf ("\nThe answer is %f",ans); - - return 0; - -} \ No newline at end of file diff --git a/Exercises/C4/Wrong_answer/wrong_answer.c b/Exercises/C4/Wrong_answer/wrong_answer.c deleted file mode 100644 index d5eceb4..0000000 --- a/Exercises/C4/Wrong_answer/wrong_answer.c +++ /dev/null @@ -1,20 +0,0 @@ -#include -#include - -int main(void) -{ - // Declare and initialise as required - int a = 1,b = 4; - float ans; - - // Perform calculation - // a and b are integers so the answer will be the result of the integer division - - ans = a / b; - - // Display answer - printf ("\nThe answer is %f",ans); - - return 0; - -} \ No newline at end of file diff --git a/Exercises/C4/declare_a_string.c b/Exercises/C4/declare_a_string.c deleted file mode 100644 index 591b072..0000000 --- a/Exercises/C4/declare_a_string.c +++ /dev/null @@ -1,13 +0,0 @@ -#include -#include -int main(void) -{ - // A variable to store a name (maximum 49 characters) - char Name[50]; - - // Declare multiple strings on the same line - char AddressLine1[100], AddressLine2[100], PostCode[10]; - - // all done - return 0; -} \ No newline at end of file diff --git a/Exercises/C4/difficult_to_read_code.c b/Exercises/C4/difficult_to_read_code.c deleted file mode 100644 index 53300e2..0000000 --- a/Exercises/C4/difficult_to_read_code.c +++ /dev/null @@ -1,4 +0,0 @@ -#include -int main(void) { int revenue = 80; int cost = 50; int roi; -roi = (100 * (revenue - cost)) / cost; if (roi >= 0) { -printf ("%d\n", roi); } return 0; } \ No newline at end of file diff --git a/Exercises/C4/poor_layout/poor_layout.c b/Exercises/C4/poor_layout/poor_layout.c deleted file mode 100644 index b018462..0000000 --- a/Exercises/C4/poor_layout/poor_layout.c +++ /dev/null @@ -1,4 +0,0 @@ -#include - - /* My first program */ -int main(void) { printf("Hello World"); return 0; } \ No newline at end of file diff --git a/Exercises/C4/reformatted_code.c b/Exercises/C4/reformatted_code.c deleted file mode 100644 index a253d00..0000000 --- a/Exercises/C4/reformatted_code.c +++ /dev/null @@ -1,19 +0,0 @@ -#include -int main(void) -{ - // Declare variables and give initial values - int revenue = 80; - int cost = 50; - int roi; - - // Perform calculation - roi = (100 * (revenue - cost)) / cost; - - // Make decision based on value of roi - if (roi >= 0) - { - printf ("%d\n", roi); - } - - return 0; // Exit indicating success -} \ No newline at end of file diff --git a/Exercises/C4/simple_claculation.c b/Exercises/C4/simple_claculation.c deleted file mode 100644 index 0544520..0000000 --- a/Exercises/C4/simple_claculation.c +++ /dev/null @@ -1,8 +0,0 @@ -#include -#include -int main(void) -{ - int a = 6,b = 7,c; // Declare and initialiase as required - c = a + b; // Add the values and store in c - -} \ No newline at end of file diff --git a/Exercises/C4/well_laid_out_code.c b/Exercises/C4/well_laid_out_code.c deleted file mode 100644 index 127af97..0000000 --- a/Exercises/C4/well_laid_out_code.c +++ /dev/null @@ -1,8 +0,0 @@ -#include - -/* My first program */ -int main(void) -{ - printf("Hello World"); - return 0; -} \ No newline at end of file diff --git a/Exercises/C5/FormattingNumbers/formatting_numbers.c b/Exercises/C5/FormattingNumbers/formatting_numbers.c deleted file mode 100644 index 49fc8cd..0000000 --- a/Exercises/C5/FormattingNumbers/formatting_numbers.c +++ /dev/null @@ -1,24 +0,0 @@ -#include -#include - -int main(void) -{ - // Declare some variables - int a = 123; - float f = 12.456f; - - // Use printf display text on the screen - printf ("Examples of integer formatting\n"); - printf ("a = %d (no modifier)\n", a); - printf ("a = %6d (w:6, justify:right)\n", a); - printf ("a = %-6d (w:6 )\n", a); - - // Use printf display text on the screen - printf ("Examples of float formatting\n"); - printf ("f = %f (no modifier)\n", f); - printf ("f = %6.2f (w:6, 2dp, justify:right)\n", f); - printf ("f = %-6.1f (w:6, 1dp)\n", f); - - // Exit from main - return 0; -} \ No newline at end of file diff --git a/Exercises/C5/displaying_variables.c b/Exercises/C5/displaying_variables.c deleted file mode 100644 index 39ef929..0000000 --- a/Exercises/C5/displaying_variables.c +++ /dev/null @@ -1,15 +0,0 @@ -#include -#include - -int main(void) -{ - // Declare some variables - int a = 1, b = 2; - float f = 1.23f; - - // Use printf display text on the screen - printf ("The variables are\na = %d\nb=%d\nf=%f", a, b, f); - - // Exit from main - return 0; -} \ No newline at end of file diff --git a/Exercises/C5/ex4.c b/Exercises/C5/ex4.c deleted file mode 100644 index d5c7cb8..0000000 --- a/Exercises/C5/ex4.c +++ /dev/null @@ -1,11 +0,0 @@ -#include -#include - -int main() { - float r; - printf("Enter radius: "); - scanf("%f", &r); - - printf("Area of sphere: %f\n", 4.0*M_PI*r*r); - printf("Volume of sphere: %f\n", M_PI*r*r*r*4.0/3.0); -} diff --git a/Exercises/C5/ex5.c b/Exercises/C5/ex5.c deleted file mode 100644 index 9fb188e..0000000 --- a/Exercises/C5/ex5.c +++ /dev/null @@ -1,11 +0,0 @@ -#include -#include - -int main() { - float r; - printf("Enter radius: "); - scanf("%f", &r); - - printf("Area of circle: %0.2f\n", M_PI*r*r); - printf("Volume of sphere: %.02f\n", M_PI*r*r*r*4.0/3.0); -} diff --git a/Exercises/C5/not_displaying.c b/Exercises/C5/not_displaying.c deleted file mode 100644 index 178845a..0000000 --- a/Exercises/C5/not_displaying.c +++ /dev/null @@ -1,17 +0,0 @@ -#include -#include - -int main(void) -{ - - // Declare some variables - int a = 1, b = 2; - float f = 1.23f; - - // Use printf display text on the screen - printf ("The variables are a, b and f"); - - // Exit from main - return 0; - -} \ No newline at end of file diff --git a/Exercises/C5/printf_example_1.c b/Exercises/C5/printf_example_1.c deleted file mode 100644 index 2d602e6..0000000 --- a/Exercises/C5/printf_example_1.c +++ /dev/null @@ -1,12 +0,0 @@ -#include -#include - -int main(void) -{ - - printf ("This is my first computer program"); - printf ("Hello World"); - - return 0; - -} \ No newline at end of file diff --git a/Exercises/C5/printf_example_2.c b/Exercises/C5/printf_example_2.c deleted file mode 100644 index 4b489fb..0000000 --- a/Exercises/C5/printf_example_2.c +++ /dev/null @@ -1,12 +0,0 @@ -#include -#include - -int main(void) -{ - - printf ("This is my first computer program\n"); - printf ("Hello World\n"); - - return 0; - -} \ No newline at end of file diff --git a/Exercises/C6/ex1.c b/Exercises/C6/ex1.c deleted file mode 100644 index 2e15ea7..0000000 --- a/Exercises/C6/ex1.c +++ /dev/null @@ -1,28 +0,0 @@ -#include -#include - -int main(void) -{ - // Declare some variables - int a, b; - - // Note: Order of a,b indicates the order in which operations are carried out on execution - - // Increment operators - b = 3; - printf("a=%d b=%d\n", a, b); - a = ++b; // b is now 4, a is also 4 - printf("a=%d b=%d\n", a, b); - a = b++; // a is 4, b is now 5, - printf("a=%d b=%d\n", a, b); - - // Decrement operators (reset a back to 3) - b = 3; - printf("a=%d b=%d\n", a, b); - a = b--; // a is 3, b is now 2 - printf("a=%d b=%d\n", a, b); - a = --b; // b is now 1, a is also 1 - printf("a=%d b=%d\n", a, b); - - return 0; // Exit from main -} diff --git a/Exercises/C6/ex2.c b/Exercises/C6/ex2.c deleted file mode 100644 index 05994e7..0000000 --- a/Exercises/C6/ex2.c +++ /dev/null @@ -1,14 +0,0 @@ -#include - -int main() { - unsigned int a = 60; - unsigned int b = 13; - unsigned int r; - - r = a & b; - printf("bitwise AND of 60 and 13: %d\n", r); - r = a | b; - printf("bitwise OR of 60 and 13: %d\n", r); - r = a ^ b; - printf("bitwise XOR of 60 and 13: %d\n", r); -} diff --git a/Exercises/C6/ex3.c b/Exercises/C6/ex3.c deleted file mode 100644 index acc02fd..0000000 --- a/Exercises/C6/ex3.c +++ /dev/null @@ -1,14 +0,0 @@ -#include - -int main() { - unsigned int a = 0x60; - unsigned int b = 0x13; - unsigned int r; - - r = a & b; - printf("bitwise AND of 0x60 and 0x13: 0x%02x\n", r); - r = a | b; - printf("bitwise OR of 0x60 and 0x13: 0x%02x\n", r); - r = a ^ b; - printf("bitwise XOR of 0x60 and 0x13: 0x%02x\n", r); -} diff --git a/Exercises/C6/inc_dec_examples.c b/Exercises/C6/inc_dec_examples.c deleted file mode 100644 index 9c22a8f..0000000 --- a/Exercises/C6/inc_dec_examples.c +++ /dev/null @@ -1,22 +0,0 @@ -#include -#include - -int main(void) -{ - // Declare some variables - int a, b; - - // Note: Order of a,b indicates the order in which operations are carried out on execution - - // Increment operators - b = 3; - a = ++b; // b is now 4, a is also 4 - a = b++; // a is 4, b is now 5, - - // Decrement operators (reset a back to 3) - b = 3; - a = b--; // a is 3, b is now 2 - a = --b; // b is now 1, a is also 1 - - return 0; // Exit from main -} \ No newline at end of file diff --git a/Exercises/C7/Getchar/getchar_example.c b/Exercises/C7/Getchar/getchar_example.c deleted file mode 100644 index 4f26421..0000000 --- a/Exercises/C7/Getchar/getchar_example.c +++ /dev/null @@ -1,20 +0,0 @@ -#include -#include - -int main(void) -{ - // declare variable - char c; - - printf("Input a character: "); - // Wait for a kepypress, store result in c - c = getchar(); - - // Display on the screen using printf - printf ("The charcter pressed was %c\n", c); - - // Display the charcter using putchar() - putchar(c); - - return 0; // Exit from main -} \ No newline at end of file diff --git a/Exercises/C7/Scanf1/scanf_example_1.c b/Exercises/C7/Scanf1/scanf_example_1.c deleted file mode 100644 index ae3f870..0000000 --- a/Exercises/C7/Scanf1/scanf_example_1.c +++ /dev/null @@ -1,28 +0,0 @@ -#include -#include - -int main(void) -{ - int a; // Declare some variables - float f; - - // Use printf to prompt the use to enter an integer - printf ("Please enter an integer\n"); - - // use scanf with %d to read into 'a' - scanf ("%d",&a); // note the important & - - // And display on the screen - printf ("The value you entered for a is %d\n", a); - - // Use printf to prompt the use to enter an float - printf ("Please enter a float\n"); - - // use scanf with %f to read into 'f' - scanf ("%f",&f); // note the important & - - // And display on the screen - printf ("The value you entered for f is %f\n", f); - - return 0; // Exit from main -} \ No newline at end of file diff --git a/Exercises/C7/Scanf2/scanf_example_2.c b/Exercises/C7/Scanf2/scanf_example_2.c deleted file mode 100644 index feefa2d..0000000 --- a/Exercises/C7/Scanf2/scanf_example_2.c +++ /dev/null @@ -1,28 +0,0 @@ -#include - -int main(void) -{ - // Declare some variables - int a,b,c; - float f,g; - - // Use printf to prompt the use to enter 3 integers - printf ("Please enter three integer\n"); - - // use scanf with %d to read into a, b and c - scanf ("%d %d %d",&a, &b, &c); // note the important & - - // And display on the screen - printf ("The values entered were %d %d %d\n", a, b, c); - - // Use printf to prompt the use to enter an float - printf ("Please enter two floats\n"); - - // use scanf with %f to read into f ang g - scanf ("%f %f",&f, &g); // note the important & - - // And display on the screen - printf ("The value you entered were %f and %f \n", f,g); - - return 0; // Exit from main -} \ No newline at end of file diff --git a/Exercises/C7/ex2.c b/Exercises/C7/ex2.c deleted file mode 100644 index 207f22b..0000000 --- a/Exercises/C7/ex2.c +++ /dev/null @@ -1,11 +0,0 @@ -#include -#include - -int main() { - float r; - printf("Enter radius: "); - scanf("%f", &r); - - printf("Area of circle: %f\n", M_PI*r*r); - printf("Volume of sphere: %f\n", M_PI*r*r*r*4.0/3.0); -} diff --git a/Exercises/C7/ex3.c b/Exercises/C7/ex3.c deleted file mode 100644 index 7d6b3bf..0000000 --- a/Exercises/C7/ex3.c +++ /dev/null @@ -1,13 +0,0 @@ -#include -#include - -int main() { - float radius, height; - - printf("Enter radius: "); - scanf("%f", &radius); - printf("Enter height: "); - scanf("%f", &height); - - printf("Surface area: %f\n", 2*M_PI*radius*radius + 2*M_PI*radius*height); -} diff --git a/Exercises/C7/ex4.c b/Exercises/C7/ex4.c deleted file mode 100644 index 92bbf40..0000000 --- a/Exercises/C7/ex4.c +++ /dev/null @@ -1,11 +0,0 @@ -#include - -int main() { - char name[200] = { 0 }; - - printf("What is your name? "); - scanf("%s", name); - - printf("Hello %s\n", name); - -} diff --git a/Exercises/C7/ex5.c b/Exercises/C7/ex5.c deleted file mode 100644 index f828e8d..0000000 --- a/Exercises/C7/ex5.c +++ /dev/null @@ -1,15 +0,0 @@ -#include - -int main() { - char name[200] = { 0 }; - int age = 0; - - printf("What is your name? "); - scanf("%s", name); - - printf("What is your age? "); - scanf("%ud", &age); - - printf("Hello %s\nYou are %d years old\n", name, age); - -} diff --git a/Exercises/C7/string_with_gets.c b/Exercises/C7/string_with_gets.c deleted file mode 100644 index 231a943..0000000 --- a/Exercises/C7/string_with_gets.c +++ /dev/null @@ -1,19 +0,0 @@ -#include -#include -int main() -{ - // Declare variable - maximum 99 characters - char str[100]; - - // Prompt for text - printf("enter some text\n"); - - // Store intput in str - gets(str); - - // Display a message on the screen - printf("you entered : %s\n", str); - - // Exit from main - return 0; -} \ No newline at end of file diff --git a/Exercises/C7/string_with_scanf.c b/Exercises/C7/string_with_scanf.c deleted file mode 100644 index 08d4a8a..0000000 --- a/Exercises/C7/string_with_scanf.c +++ /dev/null @@ -1,20 +0,0 @@ -#include -#include - -int main(void) -{ - // Declare some variables - char name[50]; - - // Wait for a kepypress, store result in c - printf ("Please enter your name"); - - // read in using scanf with %s - scanf ("%s",name); - - // Display on the screen using printf - printf ("Hello %s\n", name); - - // Exit from main - return 0; -} \ No newline at end of file diff --git a/Exercises/C8/ex2.c b/Exercises/C8/ex2.c deleted file mode 100644 index c1dd316..0000000 --- a/Exercises/C8/ex2.c +++ /dev/null @@ -1,14 +0,0 @@ -#include - -int main() { - int input; - - printf("Enter an integer: "); - scanf("%d", &input); - - if (input >= 0 && input <= 10) { - printf("The number is in range\n"); - } - - return 0; -} diff --git a/Exercises/C8/ex3.c b/Exercises/C8/ex3.c deleted file mode 100644 index 9a2d714..0000000 --- a/Exercises/C8/ex3.c +++ /dev/null @@ -1,14 +0,0 @@ -#include - -int main() { - int input; - - printf("Enter an integer: "); - scanf("%d", &input); - - if (!(input >= 0 && input <= 10)) { - printf("The number is not in range\n"); - } - - return 0; -} diff --git a/Exercises/C8/ex4.c b/Exercises/C8/ex4.c deleted file mode 100644 index 1878661..0000000 --- a/Exercises/C8/ex4.c +++ /dev/null @@ -1,21 +0,0 @@ -#include - -int main() { - int age; - - printf("Enter your age: "); - scanf("%d", &age); - - // c only matches one branch so don't need to specify lower limits of each age range - if ( age <= 0 ) { - printf("Still a baby\n"); - } else if ( age <= 12 ) { - printf("The junior years\n"); - } else if ( age < 20 ) { - printf("Teenage years\n"); - } else { - printf("Downhill all the way now!\n"); - } - - return 0; -} diff --git a/Exercises/C8/ex7.c b/Exercises/C8/ex7.c deleted file mode 100644 index 74c7e5f..0000000 --- a/Exercises/C8/ex7.c +++ /dev/null @@ -1,25 +0,0 @@ -#include -#include - -int main() { - int rc; // used to check return codes of scanf - float radius, height; - - printf("Enter radius: "); - rc = scanf("%f", &radius); - // scanf returns number of scanned items. If input is invalid then 0 values will have been scanned. - if (rc != 1) { - printf("Please enter an positive real integer or decimal\n"); - return 1; - } - - printf("Enter height: "); - rc = scanf("%f", &height); - // scanf returns number of scanned items. If input is invalid then 0 values will have been scanned. - if (rc != 1) { - printf("Please enter an positive real integer or decimal\n"); - return 1; - } - - printf("Surface area: %f\n", 2*M_PI*radius*radius + 2*M_PI*radius*height); -} diff --git a/Exercises/C8/if_example.c b/Exercises/C8/if_example.c deleted file mode 100644 index 83939d0..0000000 --- a/Exercises/C8/if_example.c +++ /dev/null @@ -1,27 +0,0 @@ -#include -#include - -int main(void) -{ - // Declare some variables - int a = 7, b=6; - - printf("Enter a value for a: "); - scanf("%d", &a); - printf("Enter a value for b: "); - scanf("%d", &b); - - // A single line of code conditional on the value of a - if ( a == 7 ) - printf ("The value of a is 7 - so I will do this\n"); - - // Multiple lines of code conditional on b not equalling 4 - // so then need to be placed inside { and } - if ( b != 4 ) - { - printf ("The value of b is not 4\n"); - printf ("So I will do multiple tasks\n"); - } - - return 0; // Exit from main -} diff --git a/Exercises/C8/switch_example_1.c b/Exercises/C8/switch_example_1.c deleted file mode 100644 index 4dc4a9a..0000000 --- a/Exercises/C8/switch_example_1.c +++ /dev/null @@ -1,29 +0,0 @@ -#include -#include - -int main(void) -{ - // Declare some variables - int a = 1; - switch ( a ) - { - case 0 : - printf ("Sunday\n"); - break ; // end of the lines of code to execute - - case 1: - printf ("Monday\n"); - break ; // end of the lines of code to execute - - case 2: - printf ("Tuesday\n"); - break ; // end of the lines of code to execute - - // etc... - - default: // If no case is met (OPTIONAL) - printf ("\nThe value supplied is out of range\n"); - - } - return 0; -} \ No newline at end of file diff --git a/Exercises/C8/switch_example_2.c b/Exercises/C8/switch_example_2.c deleted file mode 100644 index bf64f9e..0000000 --- a/Exercises/C8/switch_example_2.c +++ /dev/null @@ -1,25 +0,0 @@ -#include -#include - -int main(void) -{ - // Declare some variables - int a = 1; - switch ( a ) - { - case 1 : - case 2 : - case 3 : - // Code to do if a is 1, 2 or 3 - break ; // end of the lines of code to execute - - case 4: - case 5: - // Code to do if a is 4 or 5 - break ; // end of the lines of code to execute - - default: // If no case is met (OPTIONAL) - // Code to do if no case is met - } - return 0; -} \ No newline at end of file diff --git a/Exercises/C8/switch_example_3.c b/Exercises/C8/switch_example_3.c deleted file mode 100644 index db2154e..0000000 --- a/Exercises/C8/switch_example_3.c +++ /dev/null @@ -1,23 +0,0 @@ -#include -#include - -int main(void) -{ - // Declare some variables - int a = 1; - switch ( a ) - { - case 1 : - printf ("This is case 1\n"); - case 2 : - printf ("This is case 2\n"); - case 3 : - printf ("This is case 3\n"); - break ; // end of the lines of code to execute - - default: // If no case is met - printf ("This default case\n"); - // Code to do if no case is met - } - return 0; -} \ No newline at end of file diff --git a/Exercises/C9/do_while_loop.c b/Exercises/C9/do_while_loop.c deleted file mode 100644 index 6a0cb93..0000000 --- a/Exercises/C9/do_while_loop.c +++ /dev/null @@ -1,24 +0,0 @@ -#include -#include - -int main(void) -{ - int age; // Declare variable, no need to initialise this time - // as we read into it before it is tested against - - // The loop is always entered as the test is at the end - do - { - // Code must be executed at least once - printf ("\nPlease enter your age"); - scanf("%d", &age); - printf ("You are %d years old\n", age); - - // Test is now made and the code - // repeats if the test equates as non-zerp - // (i.e. is age is not zero) - } - while ( age != 0); - - return 0; -} \ No newline at end of file diff --git a/Exercises/C9/ex2.c b/Exercises/C9/ex2.c deleted file mode 100644 index 906166b..0000000 --- a/Exercises/C9/ex2.c +++ /dev/null @@ -1,23 +0,0 @@ -#include -#include - -int main(void) -{ - int age = 1; // Declare variable and initialise to 1 - - while ( age != 0) // Loop as long as age is not zero - { - // Code in {} executed if condition is true (non-zero) - - printf ("\nPlease enter your age: "); - scanf("%d", &age); - printf ("You are %d years old\n", age); - - if (age == 18 || age == 21) { - printf("You have come of age\n"); - } - - // Code now goes back and repeats the test with the value of age just entered - } - return 0; // Exit code -} diff --git a/Exercises/C9/ex3.c b/Exercises/C9/ex3.c deleted file mode 100644 index 7b8e564..0000000 --- a/Exercises/C9/ex3.c +++ /dev/null @@ -1,14 +0,0 @@ -#include - -#define MAX_NAME_LEN 200 - -int main() { - char name[MAX_NAME_LEN] = { 0 }; - - printf("Please enter your name: "); - scanf("%s", name); - - while (1) { - printf("%s\n", name); - } -} diff --git a/Exercises/C9/ex4.c b/Exercises/C9/ex4.c deleted file mode 100644 index f3a7ef3..0000000 --- a/Exercises/C9/ex4.c +++ /dev/null @@ -1,21 +0,0 @@ -#include -#include - -int main(void) -{ - int age = 1; // Declare variable and initialise to 1 - - do { - - printf ("\nPlease enter your age: "); - scanf("%d", &age); - printf ("You are %d years old\n", age); - - if (age == 18 || age == 21) { - printf("You have come of age\n"); - } - - // Code now goes back and repeats the test with the value of age just entered - } while ( age != 0); // Loop as long as age is not zero - return 0; // Exit code -} diff --git a/Exercises/C9/ex5.c b/Exercises/C9/ex5.c deleted file mode 100644 index 0508542..0000000 --- a/Exercises/C9/ex5.c +++ /dev/null @@ -1,9 +0,0 @@ -#include - -int main() { - for (int i = 1; i <= 15; i++) { - printf("%d\n", i); - } - - return 0; -} diff --git a/Exercises/C9/ex6.c b/Exercises/C9/ex6.c deleted file mode 100644 index d72d131..0000000 --- a/Exercises/C9/ex6.c +++ /dev/null @@ -1,29 +0,0 @@ -#include - -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\n", i); - } - - return 0; -} diff --git a/Exercises/C9/ex7.c b/Exercises/C9/ex7.c deleted file mode 100644 index 26db66a..0000000 --- a/Exercises/C9/ex7.c +++ /dev/null @@ -1,29 +0,0 @@ -#include - -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 %d\n", i, i*i); - } - - return 0; -} diff --git a/Exercises/C9/ex8.c b/Exercises/C9/ex8.c deleted file mode 100644 index a977093..0000000 --- a/Exercises/C9/ex8.c +++ /dev/null @@ -1,32 +0,0 @@ -#include - -int main() { - int rc, start, end, sum = 0; - - 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\n", i); - sum += i; - } - - printf("Sum: %d\n", sum); - - return 0; -} diff --git a/Exercises/C9/for_loop_examples.c b/Exercises/C9/for_loop_examples.c deleted file mode 100644 index 0e936ef..0000000 --- a/Exercises/C9/for_loop_examples.c +++ /dev/null @@ -1,31 +0,0 @@ -#include -#include - -int main(void) -{ - // Ddeclare variable and initialise to 1 - int i; - - // Count up from 1 to 100 in steps of 1 - // Note the test could also be: i <= 100 - for ( i = 0 ; i < 101 ; i++ ) - { - printf ("The value of i is %d\n",i); - } - - // Count down form 10 to zero - // Note: we could also use the test: i != 0 - for ( i = 10 ; i >= 0 ; i-- ) - { - printf ("The value of i is %d\n",i); - } - - // Count up from 1 to 100 in steps of 3 - // Note the test could also be: i <= 100 - // Increement could also be written as i+=3 - for ( i = 0 ; i < 101 ; i=i+1 ) - { - printf ("The value of i is %d\n",i); - } - return 0; -} \ No newline at end of file diff --git a/Exercises/C9/infinite_while_loop.c b/Exercises/C9/infinite_while_loop.c deleted file mode 100644 index a8e94e8..0000000 --- a/Exercises/C9/infinite_while_loop.c +++ /dev/null @@ -1,23 +0,0 @@ -#include -#include - -int main(void) -{ - int age ; // Declare variable, no need to initialise this time - // as the test condition is not based on its value - - // This is always non-zero, the loop will never break - while ( 1 ) - { - // Code in {} executed if condition is true (non-zero) - - printf ("\nPlease enter your age"); - scanf("%d", &age); - printf ("You are %d years old\n", age); - - // Code now goes back and repeats - // as the conditions is always non-zero (true) - } - - return 0; // Exit code -} \ No newline at end of file diff --git a/Exercises/C9/while_loop.c b/Exercises/C9/while_loop.c deleted file mode 100644 index b9ffe86..0000000 --- a/Exercises/C9/while_loop.c +++ /dev/null @@ -1,19 +0,0 @@ -#include -#include - -int main(void) -{ - int age = 1; // Declare variable and initialise to 1 - - while ( age != 0) // Loop as long as age is not zero - { - // Code in {} executed if condition is true (non-zero) - - printf ("\nPlease enter your age"); - scanf("%d", &age); - printf ("You are %d years old\n", age); - - // Code now goes back and repeats the test with the value of age just entered - } - return 0; // Exit code -} \ No newline at end of file diff --git a/Labs/Lab_1/InitialiseEncoderStateMachineAAR.drawio b/InitialiseEncoderStateMachineAAR.drawio similarity index 100% rename from Labs/Lab_1/InitialiseEncoderStateMachineAAR.drawio rename to InitialiseEncoderStateMachineAAR.drawio diff --git a/Labs/Lab_1/InitialiseEncoderStateMachineAAR.pdf b/InitialiseEncoderStateMachineAAR.pdf similarity index 100% rename from Labs/Lab_1/InitialiseEncoderStateMachineAAR.pdf rename to InitialiseEncoderStateMachineAAR.pdf diff --git a/Labs/Lab_1/Makefile b/Labs/Lab_1/Makefile deleted file mode 100644 index 65e94a1..0000000 --- a/Labs/Lab_1/Makefile +++ /dev/null @@ -1,7 +0,0 @@ -include ../../Makefile - -build: - zip Lab1PrepAAR.zip *.c *.ino *.pdf *.png - -clean: - rm -f Lab1PrepAAR.zip TestEncoderAAR TwoSensorsAAR diff --git a/Lectures/LC10/SimpleFunctions1/simple_functions_1.c b/Lectures/LC10/SimpleFunctions1/simple_functions_1.c deleted file mode 100644 index d94ecc5..0000000 --- a/Lectures/LC10/SimpleFunctions1/simple_functions_1.c +++ /dev/null @@ -1,49 +0,0 @@ -#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 ); - - -/* 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; -} - -float CalculateAreaOfCircle ( float radius ) -{ - float area; - area = M_PI * radius * radius ; - return (area) ; -} \ No newline at end of file diff --git a/Lectures/LC10/SimpleFunctions2/simple_functions_2.c b/Lectures/LC10/SimpleFunctions2/simple_functions_2.c deleted file mode 100644 index 46ec4ab..0000000 --- a/Lectures/LC10/SimpleFunctions2/simple_functions_2.c +++ /dev/null @@ -1,51 +0,0 @@ -#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); - - 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/Lectures/LC11/loop_into_array.c b/Lectures/LC11/loop_into_array.c deleted file mode 100644 index 093baa3..0000000 --- a/Lectures/LC11/loop_into_array.c +++ /dev/null @@ -1,26 +0,0 @@ - -/* 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/Lectures/LC12/Global1/global_ex1.c b/Lectures/LC12/Global1/global_ex1.c deleted file mode 100644 index be30202..0000000 --- a/Lectures/LC12/Global1/global_ex1.c +++ /dev/null @@ -1,28 +0,0 @@ -/* - 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/Lectures/LC12/Global2/global_ex2.c b/Lectures/LC12/Global2/global_ex2.c deleted file mode 100644 index db7235c..0000000 --- a/Lectures/LC12/Global2/global_ex2.c +++ /dev/null @@ -1,37 +0,0 @@ -/* - 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/Lectures/LC12/Global3/global_ex3.c b/Lectures/LC12/Global3/global_ex3.c deleted file mode 100644 index 36bdebe..0000000 --- a/Lectures/LC12/Global3/global_ex3.c +++ /dev/null @@ -1,57 +0,0 @@ -/* - - 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/Lectures/LC14/LineLength/LineLength.c b/Lectures/LC14/LineLength/LineLength.c deleted file mode 100644 index 214d9c2..0000000 --- a/Lectures/LC14/LineLength/LineLength.c +++ /dev/null @@ -1,26 +0,0 @@ -#include -#include -#include - -float LineLength(float x1, float x2, float y1, float y2); - -int main() -{ - float p1x = 3.4, p2x = 5.7, p1y = 0.0, p2y = 6.8; - float length = 0; - length = LineLength(p1x, p2x, p1y, p2y); - printf("Length = %f", length); - return 0; -} - -float LineLength(float x1, float x2, float y1, float y2) -{ - float result; - float dx = x2-x1; - float dy = y2-y1; - float tol = 1e-10; - if (fabs(dx) < tol && fabs(dy) < tol ) - return -1; - result = sqrt(dx*dx + dy*dy); - return result; -} diff --git a/Lectures/LC14/PointerFunc1/pointer_function_example_1.c b/Lectures/LC14/PointerFunc1/pointer_function_example_1.c deleted file mode 100644 index 7e91ee4..0000000 --- a/Lectures/LC14/PointerFunc1/pointer_function_example_1.c +++ /dev/null @@ -1,32 +0,0 @@ -#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/Lectures/LC14/PointerFunc2/pointer_function_example_2.c b/Lectures/LC14/PointerFunc2/pointer_function_example_2.c deleted file mode 100644 index 856f6a0..0000000 --- a/Lectures/LC14/PointerFunc2/pointer_function_example_2.c +++ /dev/null @@ -1,31 +0,0 @@ -#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/Lectures/LC15/PointerToArray/pointer_to_array_examples.c b/Lectures/LC15/PointerToArray/pointer_to_array_examples.c deleted file mode 100644 index 078a657..0000000 --- a/Lectures/LC15/PointerToArray/pointer_to_array_examples.c +++ /dev/null @@ -1,33 +0,0 @@ -#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/Lectures/LC15/PointerToArray1/pointer_to_array_1.c b/Lectures/LC15/PointerToArray1/pointer_to_array_1.c deleted file mode 100644 index 944f8b1..0000000 --- a/Lectures/LC15/PointerToArray1/pointer_to_array_1.c +++ /dev/null @@ -1,24 +0,0 @@ -#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] ); - - // Exit the application - return 0; -} diff --git a/Lectures/LC15/PointerToArray2/pointer_to_array_2.c b/Lectures/LC15/PointerToArray2/pointer_to_array_2.c deleted file mode 100644 index 638cd79..0000000 --- a/Lectures/LC15/PointerToArray2/pointer_to_array_2.c +++ /dev/null @@ -1,32 +0,0 @@ -#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 2 using index\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/Lectures/LC16/Dynamic1/Dynamic1.c b/Lectures/LC16/Dynamic1/Dynamic1.c deleted file mode 100644 index bf57a95..0000000 --- a/Lectures/LC16/Dynamic1/Dynamic1.c +++ /dev/null @@ -1,46 +0,0 @@ -/* 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/Lectures/LC16/Dynamic2/Dynamic2.c b/Lectures/LC16/Dynamic2/Dynamic2.c deleted file mode 100644 index 4176c7f..0000000 --- a/Lectures/LC16/Dynamic2/Dynamic2.c +++ /dev/null @@ -1,54 +0,0 @@ -/* 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/Lectures/LC16/Dynamic3/Dynamic3.c b/Lectures/LC16/Dynamic3/Dynamic3.c deleted file mode 100644 index a745531..0000000 --- a/Lectures/LC16/Dynamic3/Dynamic3.c +++ /dev/null @@ -1,67 +0,0 @@ -/* 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/Lectures/LC17/DynamicFunction/DynamicFunction.c b/Lectures/LC17/DynamicFunction/DynamicFunction.c deleted file mode 100644 index 449c9a5..0000000 --- a/Lectures/LC17/DynamicFunction/DynamicFunction.c +++ /dev/null @@ -1,81 +0,0 @@ -/* 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 ( 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; -} - diff --git a/Lectures/LC17/Quadratic_with_Pointers.c b/Lectures/LC17/Quadratic_with_Pointers.c deleted file mode 100644 index 8eb030e..0000000 --- a/Lectures/LC17/Quadratic_with_Pointers.c +++ /dev/null @@ -1,61 +0,0 @@ -#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/Lectures/LC18/EndOfFile/EndOfFile.c b/Lectures/LC18/EndOfFile/EndOfFile.c deleted file mode 100644 index 4051455..0000000 --- a/Lectures/LC18/EndOfFile/EndOfFile.c +++ /dev/null @@ -1,55 +0,0 @@ -#include -#include - -// Main () - execution starts here -int main (void) -{ - // Declate file stream variables - FILE *fInput, *fOutput; - - // Other variables needed - int i,d; - - // Try and open the text "sample.txt" (in the current directory) file for writing - fOutput = fopen ("numbers.txt", "w"); - - // Check we were able to open the file - if ( fOutput == NULL) - { - printf ("\nthe file could not be opened for writing, exiting"); - return -1; - } - - // Use a loop to write values to the newly created file - for ( i = 1 ; i <= 10 ; i++) - { - fprintf (fOutput, "%d\n", i); - } - - // And close the file - fclose (fOutput); - - // Try and open the binary "numbers " (in the current directory) file for reading - - fInput = fopen ("numbers.txt", "r"); - - // Check we were able to open the file - if ( fInput == NULL) - { - printf ("\nthe file could not be opened for reading, exiting"); - return -1; - } - - // Read, line by line the 10 values written into variable d - // and then display the contents of d on the screen - while (!feof(fInput)) - { - fscanf (fInput, "%d", &d); - printf ("Value read from file %d\n",d); - } - - // And close the file - fclose (fInput); - - return (0); // Exit indicating success -} diff --git a/Lectures/LC18/EndOfFile/numbers.txt b/Lectures/LC18/EndOfFile/numbers.txt deleted file mode 100644 index f00c965..0000000 --- a/Lectures/LC18/EndOfFile/numbers.txt +++ /dev/null @@ -1,10 +0,0 @@ -1 -2 -3 -4 -5 -6 -7 -8 -9 -10 diff --git a/Lectures/LC18/EndOfFileBreak/EndOfFileBreak.c b/Lectures/LC18/EndOfFileBreak/EndOfFileBreak.c deleted file mode 100644 index 385bf61..0000000 --- a/Lectures/LC18/EndOfFileBreak/EndOfFileBreak.c +++ /dev/null @@ -1,57 +0,0 @@ -#include -#include - -// Main () - execution starts here -int main (void) -{ - // Declate file stream variables - FILE *fInput, *fOutput; - - // Other variables needed - int i,d; - - // Try and open the text "sample.txt" (in the current directory) file for writing - fOutput = fopen ("numbers.txt", "w"); - - // Check we were able to open the file - if ( fOutput == NULL) - { - printf ("\nthe file could not be opened for writing, exiting"); - return -1; - } - - // Use a loop to write values to the newly created file - for ( i = 1 ; i <= 10 ; i++) - { - fprintf (fOutput, "%d\n", i); - } - - // And close the file - fclose (fOutput); - - // Try and open the binary "numbers " (in the current directory) file for reading - - fInput = fopen ("numbers.txt", "r"); - - // Check we were able to open the file - if ( fInput == NULL) - { - printf ("\nthe file could not be opened for reading, exiting"); - return -1; - } - - // Read, line by line the 10 values written into variable d - // and then display the contents of d on the screen - while (1) - { - fscanf (fInput, "%d", &d); - if ( feof(fInput)) - break; - printf ("Value read from file %d\n",d); - } - - // And close the file - fclose (fInput); - - return (0); // Exit indicating success -} diff --git a/Lectures/LC18/EndOfFileBreak/numbers.txt b/Lectures/LC18/EndOfFileBreak/numbers.txt deleted file mode 100644 index f00c965..0000000 --- a/Lectures/LC18/EndOfFileBreak/numbers.txt +++ /dev/null @@ -1,10 +0,0 @@ -1 -2 -3 -4 -5 -6 -7 -8 -9 -10 diff --git a/Lectures/LC18/EndOfFileScanf/EndOfFileScanf.c b/Lectures/LC18/EndOfFileScanf/EndOfFileScanf.c deleted file mode 100644 index d24b0b6..0000000 --- a/Lectures/LC18/EndOfFileScanf/EndOfFileScanf.c +++ /dev/null @@ -1,54 +0,0 @@ -#include -#include - -// Main () - execution starts here -int main (void) -{ - // Declate file stream variables - FILE *fInput, *fOutput; - - // Other variables needed - int i,d; - - // Try and open the text "sample.txt" (in the current directory) file for writing - fOutput = fopen ("numbers.txt", "w"); - - // Check we were able to open the file - if ( fOutput == NULL) - { - printf ("\nthe file could not be opened for writing, exiting"); - return -1; - } - - // Use a loop to write values to the newly created file - for ( i = 1 ; i <= 10 ; i++) - { - fprintf (fOutput, "%d\n", i); - } - - // And close the file - fclose (fOutput); - - // Try and open the binary "numbers " (in the current directory) file for reading - - fInput = fopen ("numbers.txt", "r"); - - // Check we were able to open the file - if ( fInput == NULL) - { - printf ("\nthe file could not be opened for reading, exiting"); - return -1; - } - - // Read, line by line the 10 values written into variable d - // and then display the contents of d on the screen - while (fscanf (fInput, "%d", &d) != EOF) - { - printf ("Value read from file %d\n",d); - } - - // And close the file - fclose (fInput); - - return (0); // Exit indicating success -} diff --git a/Lectures/LC18/EndOfFileScanf/numbers.txt b/Lectures/LC18/EndOfFileScanf/numbers.txt deleted file mode 100644 index f00c965..0000000 --- a/Lectures/LC18/EndOfFileScanf/numbers.txt +++ /dev/null @@ -1,10 +0,0 @@ -1 -2 -3 -4 -5 -6 -7 -8 -9 -10 diff --git a/Lectures/LC18/FileMove/data.dat b/Lectures/LC18/FileMove/data.dat deleted file mode 100644 index a7a19b2..0000000 Binary files a/Lectures/LC18/FileMove/data.dat and /dev/null differ diff --git a/Lectures/LC18/FileMove/filemove.c b/Lectures/LC18/FileMove/filemove.c deleted file mode 100644 index 3cb4589..0000000 --- a/Lectures/LC18/FileMove/filemove.c +++ /dev/null @@ -1,48 +0,0 @@ -#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 */ - if (fread (&ValRead, sizeof(int), 1, fptr ) == 1) - { - /* Display the read value */ - printf ("Item %d is %d\n",Item,ValRead); - } - else - printf("Failed to read at location %d\n", Item); - - /* And close */ - fclose (fptr); - - /* All Done */ - return 0; - -} - \ No newline at end of file diff --git a/Lectures/LC18/FileSize/data.dat b/Lectures/LC18/FileSize/data.dat deleted file mode 100644 index a7a19b2..0000000 Binary files a/Lectures/LC18/FileSize/data.dat and /dev/null differ diff --git a/Lectures/LC18/FileSize/filesize.c b/Lectures/LC18/FileSize/filesize.c deleted file mode 100644 index c5d9771..0000000 --- a/Lectures/LC18/FileSize/filesize.c +++ /dev/null @@ -1,38 +0,0 @@ -#include -#include - -/* Example 3 - jumping & getting element count */ - -int 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 %ld\n",FileBytes); - - /* And close */ - fclose (fptr); - - /* All Done */ - return 0; -} - \ No newline at end of file diff --git a/Lectures/LC19/ConstHashDefine/ConstHashDefine.c b/Lectures/LC19/ConstHashDefine/ConstHashDefine.c deleted file mode 100644 index 51736a4..0000000 --- a/Lectures/LC19/ConstHashDefine/ConstHashDefine.c +++ /dev/null @@ -1,33 +0,0 @@ -#include - -//macro definition -#define X 30 - -//global integer constant -const int Y = 10; - -void func(); - -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); - - func(); - - //Y = 30; - Z = 40; - - return 0; -} - -void func() -{ - int Z = 40; - printf("Value of Z in function = %d\n", Z); - //Y = 30; -} diff --git a/Lectures/LC19/Enum/enum.c b/Lectures/LC19/Enum/enum.c deleted file mode 100644 index 9ed292f..0000000 --- a/Lectures/LC19/Enum/enum.c +++ /dev/null @@ -1,24 +0,0 @@ -#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/Lectures/LC19/FileHeader/file_header.c b/Lectures/LC19/FileHeader/file_header.c deleted file mode 100644 index 83c1b54..0000000 --- a/Lectures/LC19/FileHeader/file_header.c +++ /dev/null @@ -1,90 +0,0 @@ -#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.average = (float)sum / (float)items; - - /* rewind the file to write the structure again */ - fseek(fptr, SEEK_SET, 0); - //rewind (fptr); // This is an alternative to the previous line - - /* 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/Lectures/LC19/FileHeader/strdata.dat b/Lectures/LC19/FileHeader/strdata.dat deleted file mode 100644 index 68cf19d..0000000 Binary files a/Lectures/LC19/FileHeader/strdata.dat and /dev/null differ diff --git a/Lectures/LC19/FileHeaderMove/file_header_move.c b/Lectures/LC19/FileHeaderMove/file_header_move.c deleted file mode 100644 index fb0ef03..0000000 --- a/Lectures/LC19/FileHeaderMove/file_header_move.c +++ /dev/null @@ -1,99 +0,0 @@ -#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/Lectures/LC19/FileHeaderMove/strdata.dat b/Lectures/LC19/FileHeaderMove/strdata.dat deleted file mode 100644 index 68cf19d..0000000 Binary files a/Lectures/LC19/FileHeaderMove/strdata.dat and /dev/null differ diff --git a/Lectures/LC19/Static/Static.c b/Lectures/LC19/Static/Static.c deleted file mode 100644 index c4cc180..0000000 --- a/Lectures/LC19/Static/Static.c +++ /dev/null @@ -1,45 +0,0 @@ -/* - 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/Lectures/LC19/Structure1/Structure_1.c b/Lectures/LC19/Structure1/Structure_1.c deleted file mode 100644 index b8b91a0..0000000 --- a/Lectures/LC19/Structure1/Structure_1.c +++ /dev/null @@ -1,27 +0,0 @@ - -#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/Lectures/LC19/Structure2/Structure_2.c b/Lectures/LC19/Structure2/Structure_2.c deleted file mode 100644 index 50d45e5..0000000 --- a/Lectures/LC19/Structure2/Structure_2.c +++ /dev/null @@ -1,39 +0,0 @@ -#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 : 5 */ - - 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/Lectures/LC19/Structure3/Structure_3.c b/Lectures/LC19/Structure3/Structure_3.c deleted file mode 100644 index 13ae5e8..0000000 --- a/Lectures/LC19/Structure3/Structure_3.c +++ /dev/null @@ -1,59 +0,0 @@ -#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 ",CpPoints[i].x); - printf("\t Y = %d ",CpPoints[i].y); - } - - return 0; -} \ No newline at end of file diff --git a/Lectures/LC19/StructureFunction/StructureFunc.c b/Lectures/LC19/StructureFunction/StructureFunc.c deleted file mode 100644 index 2ab3ac4..0000000 --- a/Lectures/LC19/StructureFunction/StructureFunc.c +++ /dev/null @@ -1,36 +0,0 @@ -#include -#include - -struct Point /* Define a struture called Point */ -{ - int x; /* The X data */ - int y; /* The Y data */ -}; - -struct Line -{ - struct Point p1; - struct Point p2; - float length; -}; - -void LineLength( struct Line* line); - -int main() -{ - struct Line line; - line.p1.x = 0; - line.p1.y = 0; - line.p2.x = 3; - line.p2.y = 4; - - LineLength( &line ); - printf( "Line length = %.3f\n", line.length); -} - -void LineLength( struct Line* line) -{ - float dx = line->p2.x - line->p1.x; - float dy = line->p2.y - line->p1.y; - line->length = sqrt( dx*dx + dy*dy ); -} \ No newline at end of file diff --git a/Lectures/LC19/WriteHeaderArrayAndUpdate.c b/Lectures/LC19/WriteHeaderArrayAndUpdate.c deleted file mode 100644 index 074828f..0000000 --- a/Lectures/LC19/WriteHeaderArrayAndUpdate.c +++ /dev/null @@ -1,90 +0,0 @@ -#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/Lectures/LC19/WriteHeaderArrayAndUpdateAndSeek.c b/Lectures/LC19/WriteHeaderArrayAndUpdateAndSeek.c deleted file mode 100644 index fb0ef03..0000000 --- a/Lectures/LC19/WriteHeaderArrayAndUpdateAndSeek.c +++ /dev/null @@ -1,99 +0,0 @@ -#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/Lectures/LC19/WriteHeaderWithArray.c b/Lectures/LC19/WriteHeaderWithArray.c deleted file mode 100644 index 4724d3f..0000000 --- a/Lectures/LC19/WriteHeaderWithArray.c +++ /dev/null @@ -1,77 +0,0 @@ -#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/Lectures/LC19/enumdefine.c b/Lectures/LC19/enumdefine.c deleted file mode 100644 index 84a41e7..0000000 --- a/Lectures/LC19/enumdefine.c +++ /dev/null @@ -1,41 +0,0 @@ -/* 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/Lectures/LC19/swap.c b/Lectures/LC19/swap.c deleted file mode 100644 index eb2fb4a..0000000 --- a/Lectures/LC19/swap.c +++ /dev/null @@ -1,72 +0,0 @@ -#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/Lectures/LC19/union.c b/Lectures/LC19/union.c deleted file mode 100644 index cc0fe09..0000000 --- a/Lectures/LC19/union.c +++ /dev/null @@ -1,31 +0,0 @@ -#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/Lectures/LC20/AreaFunc.c b/Lectures/LC20/AreaFunc.c deleted file mode 100644 index 3e5b3c9..0000000 --- a/Lectures/LC20/AreaFunc.c +++ /dev/null @@ -1,17 +0,0 @@ -#include -#include - - -float CalculateAreaOfCircle ( float radius ) -{ - float area; - area = M_PI * radius * radius ; - return (area) ; -} - -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) ; -} \ No newline at end of file diff --git a/Lectures/LC20/AreaFunc.h b/Lectures/LC20/AreaFunc.h deleted file mode 100644 index 8f4ce63..0000000 --- a/Lectures/LC20/AreaFunc.h +++ /dev/null @@ -1,2 +0,0 @@ -float CalculateAreaOfCircle ( float radius ); -float CalculateSurfaceAreaOfCylinder ( float radius, float length ); \ No newline at end of file diff --git a/Lectures/LC20/Main.c b/Lectures/LC20/Main.c deleted file mode 100644 index 27cbebb..0000000 --- a/Lectures/LC20/Main.c +++ /dev/null @@ -1,34 +0,0 @@ -#include -#include - -#include"AreaFunc.h" - -/* Show use of function */ -int main (void) -{ - // Declare variables - no need to initialise as values will be read in / calculated - float rad, len, Area, SurfaceArea; - - // Prompt for and obtain value - printf ("Please enter the raduis of the circle: "); - scanf ("%f", &rad); - - printf ("Please enter the length of the cylinder: "); - scanf ("%f", &len); - - // Calculate the area of the circle - Area = CalculateAreaOfCircle(rad); - - // And display the answer on the screen - printf ("The area of a circle of radius %f is %f\n", rad, Area ); - - // Calculate the surface area of the cylinder - 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 ); - - // All done - return 0; -} - diff --git a/Lectures/LC21/Cond/cond.c b/Lectures/LC21/Cond/cond.c deleted file mode 100644 index d3409f3..0000000 --- a/Lectures/LC21/Cond/cond.c +++ /dev/null @@ -1,37 +0,0 @@ -#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/Lectures/LC21/GetThem/getthem.c b/Lectures/LC21/GetThem/getthem.c deleted file mode 100644 index abd4026..0000000 --- a/Lectures/LC21/GetThem/getthem.c +++ /dev/null @@ -1,24 +0,0 @@ -#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/Lectures/LC21/Simple/simple.c b/Lectures/LC21/Simple/simple.c deleted file mode 100644 index 7c013bb..0000000 --- a/Lectures/LC21/Simple/simple.c +++ /dev/null @@ -1,19 +0,0 @@ -#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/Lectures/LC21/use.c b/Lectures/LC21/use.c deleted file mode 100644 index e127221..0000000 --- a/Lectures/LC21/use.c +++ /dev/null @@ -1,29 +0,0 @@ -#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/Lectures/LC4/hello_world.c b/Lectures/LC4/hello_world.c deleted file mode 100644 index d089699..0000000 --- a/Lectures/LC4/hello_world.c +++ /dev/null @@ -1,9 +0,0 @@ -#include -#include - -int main( void ) -{ - /* My first program in C */ - printf ("Hello World \n"); - return 0; // Return from prog -} diff --git a/Lectures/LC4/hello_world.exe b/Lectures/LC4/hello_world.exe deleted file mode 100644 index 35f237e..0000000 Binary files a/Lectures/LC4/hello_world.exe and /dev/null differ diff --git a/Lectures/LC5/printf_example.c b/Lectures/LC5/printf_example.c deleted file mode 100644 index c54b7c1..0000000 --- a/Lectures/LC5/printf_example.c +++ /dev/null @@ -1,14 +0,0 @@ -#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/Lectures/LC7/FgetsString/fgets_string.c b/Lectures/LC7/FgetsString/fgets_string.c deleted file mode 100644 index 677f8c0..0000000 --- a/Lectures/LC7/FgetsString/fgets_string.c +++ /dev/null @@ -1,22 +0,0 @@ -/* EEEE1001/H61CAE*/ - -/* A program to show how to use fgets to safely read in a string */ - -#include -#include - -int main() -{ - - char MyName[11]; /* Define a string of 10 characters */ - - - /* A string */ - - printf("\nPlease enter your name & press return "); - fgets(MyName, 11, stdin); - printf("\nHello %s ",MyName); - - return 0; /* End of the program */ - -} diff --git a/Lectures/LC7/Getch/getch.c b/Lectures/LC7/Getch/getch.c deleted file mode 100644 index d92a07f..0000000 --- a/Lectures/LC7/Getch/getch.c +++ /dev/null @@ -1,28 +0,0 @@ -/* - 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 */ - - printf("Input a character: "); - x = getch(); /* Use getchar to read a keypress and store the */ - /* result in 'x' */ - printf("\nDisplaying the char with putchar: "); - putchar(x); /* Display the character stored in 'x' on the */ - /* screen using putchar */ - - - return 0; - -} diff --git a/Lectures/LC7/GetsString/gets_string.c b/Lectures/LC7/GetsString/gets_string.c deleted file mode 100644 index c4f15a8..0000000 --- a/Lectures/LC7/GetsString/gets_string.c +++ /dev/null @@ -1,22 +0,0 @@ -/* 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[10]; /* 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/Lectures/LC7/ScanfString/scanf_string.c b/Lectures/LC7/ScanfString/scanf_string.c deleted file mode 100644 index 12874b5..0000000 --- a/Lectures/LC7/ScanfString/scanf_string.c +++ /dev/null @@ -1,23 +0,0 @@ -/* 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/Lectures/LC7/getchar.c b/Lectures/LC7/getchar.c deleted file mode 100644 index 9826d10..0000000 --- a/Lectures/LC7/getchar.c +++ /dev/null @@ -1,27 +0,0 @@ -/* - 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/Lectures/LC7/scanf_examples.c b/Lectures/LC7/scanf_examples.c deleted file mode 100644 index 9e676cc..0000000 --- a/Lectures/LC7/scanf_examples.c +++ /dev/null @@ -1,36 +0,0 @@ -/* 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/Lectures/LC8/ComplexIf/complex_if.c b/Lectures/LC8/ComplexIf/complex_if.c deleted file mode 100644 index 8839daa..0000000 --- a/Lectures/LC8/ComplexIf/complex_if.c +++ /dev/null @@ -1,35 +0,0 @@ -/* MMME3085 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/Lectures/LC8/IfElseIfElse/if_else_if_else.c b/Lectures/LC8/IfElseIfElse/if_else_if_else.c deleted file mode 100644 index d3dcf7d..0000000 --- a/Lectures/LC8/IfElseIfElse/if_else_if_else.c +++ /dev/null @@ -1,35 +0,0 @@ -/* Chapter 8 */ - -/* Example of a simple if statements */ - - -#include /* Standard include files */ -#include -#include - - -int main( void ) -{ - - int x = 3; // 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/Lectures/LC8/IfEquals/if_equals_example.c b/Lectures/LC8/IfEquals/if_equals_example.c deleted file mode 100644 index f3047e1..0000000 --- a/Lectures/LC8/IfEquals/if_equals_example.c +++ /dev/null @@ -1,37 +0,0 @@ -/* MMME3085 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/Lectures/LC8/IfExamples/if_examples.c b/Lectures/LC8/IfExamples/if_examples.c deleted file mode 100644 index 04c8d9e..0000000 --- a/Lectures/LC8/IfExamples/if_examples.c +++ /dev/null @@ -1,34 +0,0 @@ -/* 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/Lectures/LC8/Switch1/switch_1.c b/Lectures/LC8/Switch1/switch_1.c deleted file mode 100644 index 114108e..0000000 --- a/Lectures/LC8/Switch1/switch_1.c +++ /dev/null @@ -1,31 +0,0 @@ -/* MMME3085 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/Lectures/LC8/Switch2/switch_2.c b/Lectures/LC8/Switch2/switch_2.c deleted file mode 100644 index 4a0c08f..0000000 --- a/Lectures/LC8/Switch2/switch_2.c +++ /dev/null @@ -1,34 +0,0 @@ -/* 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 */ - - 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/Lectures/LC8/Switch3/switch_3.c b/Lectures/LC8/Switch3/switch_3.c deleted file mode 100644 index 7b6ed51..0000000 --- a/Lectures/LC8/Switch3/switch_3.c +++ /dev/null @@ -1,33 +0,0 @@ -/* 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/Lectures/LC9/DoWhile/do_while_example.c b/Lectures/LC9/DoWhile/do_while_example.c deleted file mode 100644 index 7af177c..0000000 --- a/Lectures/LC9/DoWhile/do_while_example.c +++ /dev/null @@ -1,42 +0,0 @@ -/* - 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/Lectures/LC9/ForLoops/for_loops.c b/Lectures/LC9/ForLoops/for_loops.c deleted file mode 100644 index 3ee4e10..0000000 --- a/Lectures/LC9/ForLoops/for_loops.c +++ /dev/null @@ -1,44 +0,0 @@ -/* 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 ); - - printf("\nThe value of j after increment loop is %d",j); - - /* The count down loop */ - - printf ("\nCounting Down .. "); - for ( j = 10 ; j > 0 ; j-- ) - printf("\nThe value of j is %d",j ); - - printf("\nThe value of j after decrement loop is %d",j); - - return 0; - -} diff --git a/Lectures/LC9/While1/while_example_version_1.c b/Lectures/LC9/While1/while_example_version_1.c deleted file mode 100644 index 1dde928..0000000 --- a/Lectures/LC9/While1/while_example_version_1.c +++ /dev/null @@ -1,47 +0,0 @@ -/* - 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/Lectures/LC9/While2/while_example_version_2.c b/Lectures/LC9/While2/while_example_version_2.c deleted file mode 100644 index 5978fb2..0000000 --- a/Lectures/LC9/While2/while_example_version_2.c +++ /dev/null @@ -1,37 +0,0 @@ -/* - 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; - -} diff --git a/Makefile b/Makefile index 01e2cdf..fcd6f8b 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,11 @@ CFLAGS=-lm + +build: + zip Lab1PrepAAR.zip *.c *.ino *.pdf *.png + %.c: $(CC) $(CFLAGS) -c $< -o $* + +clean: + rm -f Lab1PrepAAR.zip TestEncoderAAR TwoSensorsAAR diff --git a/Labs/Lab_1/MotorEncoderAAR.ino b/MotorEncoderAAR.ino similarity index 100% rename from Labs/Lab_1/MotorEncoderAAR.ino rename to MotorEncoderAAR.ino diff --git a/Labs/Lab_1/TestEncoderAAR.c b/TestEncoderAAR.c similarity index 100% rename from Labs/Lab_1/TestEncoderAAR.c rename to TestEncoderAAR.c diff --git a/Labs/Lab_1/TestEncoderAAR.png b/TestEncoderAAR.png similarity index 100% rename from Labs/Lab_1/TestEncoderAAR.png rename to TestEncoderAAR.png diff --git a/Labs/Lab_1/TwoSensorsAAR.c b/TwoSensorsAAR.c similarity index 100% rename from Labs/Lab_1/TwoSensorsAAR.c rename to TwoSensorsAAR.c diff --git a/Labs/Lab_1/TwoSensorsAAR.ino b/TwoSensorsAAR.ino similarity index 100% rename from Labs/Lab_1/TwoSensorsAAR.ino rename to TwoSensorsAAR.ino diff --git a/Labs/Lab_1/TwoSensorsAAR.png b/TwoSensorsAAR.png similarity index 100% rename from Labs/Lab_1/TwoSensorsAAR.png rename to TwoSensorsAAR.png diff --git a/Labs/Lab_1/UpdateStateMachineAAR.drawio b/UpdateStateMachineAAR.drawio similarity index 100% rename from Labs/Lab_1/UpdateStateMachineAAR.drawio rename to UpdateStateMachineAAR.drawio diff --git a/Labs/Lab_1/UpdateStateMachineAAR.pdf b/UpdateStateMachineAAR.pdf similarity index 100% rename from Labs/Lab_1/UpdateStateMachineAAR.pdf rename to UpdateStateMachineAAR.pdf diff --git a/Labs/Lab_1/res/Marking Template for MMME3085 Lab 1 Programming.pdf b/res/Marking Template for MMME3085 Lab 1 Programming.pdf similarity index 100% rename from Labs/Lab_1/res/Marking Template for MMME3085 Lab 1 Programming.pdf rename to res/Marking Template for MMME3085 Lab 1 Programming.pdf diff --git a/Labs/Lab_1/res/lab_prep_sheet.pdf b/res/lab_prep_sheet.pdf similarity index 100% rename from Labs/Lab_1/res/lab_prep_sheet.pdf rename to res/lab_prep_sheet.pdf diff --git a/Labs/Lab_1/skeleton_flowcharts/InitialiseEncoderStateMachineSkeleton.drawio b/skeleton_flowcharts/InitialiseEncoderStateMachineSkeleton.drawio similarity index 100% rename from Labs/Lab_1/skeleton_flowcharts/InitialiseEncoderStateMachineSkeleton.drawio rename to skeleton_flowcharts/InitialiseEncoderStateMachineSkeleton.drawio diff --git a/Labs/Lab_1/skeleton_flowcharts/InitialiseEncoderStateMachineSkeleton.pdf b/skeleton_flowcharts/InitialiseEncoderStateMachineSkeleton.pdf similarity index 100% rename from Labs/Lab_1/skeleton_flowcharts/InitialiseEncoderStateMachineSkeleton.pdf rename to skeleton_flowcharts/InitialiseEncoderStateMachineSkeleton.pdf diff --git a/Labs/Lab_1/skeleton_flowcharts/UpdateStateMachineSkeleton.drawio b/skeleton_flowcharts/UpdateStateMachineSkeleton.drawio similarity index 100% rename from Labs/Lab_1/skeleton_flowcharts/UpdateStateMachineSkeleton.drawio rename to skeleton_flowcharts/UpdateStateMachineSkeleton.drawio diff --git a/Labs/Lab_1/skeleton_flowcharts/UpdateStateMachineSkeleton.pdf b/skeleton_flowcharts/UpdateStateMachineSkeleton.pdf similarity index 100% rename from Labs/Lab_1/skeleton_flowcharts/UpdateStateMachineSkeleton.pdf rename to skeleton_flowcharts/UpdateStateMachineSkeleton.pdf diff --git a/Labs/Lab_1/skeleton_programs/MotorEncoderSkeleton.ino b/skeleton_programs/MotorEncoderSkeleton.ino similarity index 100% rename from Labs/Lab_1/skeleton_programs/MotorEncoderSkeleton.ino rename to skeleton_programs/MotorEncoderSkeleton.ino diff --git a/Labs/Lab_1/skeleton_programs/TestEncoder.c b/skeleton_programs/TestEncoder.c similarity index 100% rename from Labs/Lab_1/skeleton_programs/TestEncoder.c rename to skeleton_programs/TestEncoder.c diff --git a/Labs/Lab_1/skeleton_programs/TestEncoder.ino b/skeleton_programs/TestEncoder.ino similarity index 100% rename from Labs/Lab_1/skeleton_programs/TestEncoder.ino rename to skeleton_programs/TestEncoder.ino diff --git a/Labs/Lab_1/skeleton_programs/TwoSensorsSkeleton.c b/skeleton_programs/TwoSensorsSkeleton.c similarity index 100% rename from Labs/Lab_1/skeleton_programs/TwoSensorsSkeleton.c rename to skeleton_programs/TwoSensorsSkeleton.c diff --git a/Labs/Lab_1/skeleton_programs/TwoSensorsSkeleton.ino b/skeleton_programs/TwoSensorsSkeleton.ino similarity index 100% rename from Labs/Lab_1/skeleton_programs/TwoSensorsSkeleton.ino rename to skeleton_programs/TwoSensorsSkeleton.ino