From caee190d0588c694edaf08f773d933e742d99eb7 Mon Sep 17 00:00:00 2001 From: Louise Brown Date: Mon, 14 Nov 2022 13:51:40 +0000 Subject: [PATCH] Changes to file structure for VSCode and bug fixes --- .../arrays_to_functions_example_1.c | 2 +- .../arrays_to_functions_example_2.c | 0 C18/{ => BinaryFile}/binary_file_example.c | 0 C18/BinaryFile/numbers.dat | Bin 0 -> 44 bytes C18/{ => FileOpen}/file_open_example.c | 0 C18/TextFile/numbers.txt | 10 +++ C18/{ => TextFile}/text_file_example.c | 0 C19/{ => DefineExamples}/define_example.c | 0 .../conditional_directive_example.c | 4 +- .../formatting_directive_example.c | 4 +- LC15/PointerToArray1/pointer_to_array_1.c | 9 -- LC15/PointerToArray2/pointer_to_array_2.c | 3 +- LC16/{ => Dynamic1}/Dynamic1.c | 2 +- LC16/{ => Dynamic2}/Dynamic2.c | 0 LC16/{ => Dynamic3}/Dynamic3.c | 1 + LC17/{ => DynamicFunction}/DynamicFunction.c | 8 +- LC18/EndOfFile/EndOfFile.c | 55 +++++++++++++ LC18/EndOfFile/numbers.txt | 10 +++ LC18/EndOfFileBreak/EndOfFileBreak.c | 57 +++++++++++++ LC18/EndOfFileBreak/numbers.txt | 10 +++ LC18/EndOfFileScanf/EndOfFileScanf.c | 54 ++++++++++++ LC18/EndOfFileScanf/numbers.txt | 10 +++ LC18/FileMove/data.dat | Bin 0 -> 44 bytes LC18/{ => FileMove}/filemove.c | 11 ++- LC18/FileSize/data.dat | Bin 0 -> 44 bytes LC18/{ => FileSize}/filesize.c | 4 +- LC19/{ => ConstHashDefine}/ConstHashDefine.c | 16 +++- LC19/{ => Enum}/enum.c | 0 LC19/{ => FileHeader}/file_header.c | 4 +- LC19/FileHeader/strdata.dat | Bin 0 -> 412 bytes LC19/{ => FileHeaderMove}/file_header_move.c | 0 LC19/FileHeaderMove/strdata.dat | Bin 0 -> 412 bytes LC19/{ => Static}/Static.c | 0 LC19/{ => Structure1}/Structure_1.c | 0 LC19/{ => Structure2}/Structure_2.c | 12 +-- LC19/{ => Structure3}/Structure_3.c | 18 ++-- LC19/StructureFunction/StructureFunc.c | 36 ++++++++ LC19/WriteHeader.c | 77 ------------------ LC20/AreaFunc.c | 17 ++++ LC20/AreaFunc.h | 2 + LC20/Main.c | 34 ++++++++ 41 files changed, 346 insertions(+), 124 deletions(-) rename C17/{ => ArraysToFunctions1}/arrays_to_functions_example_1.c (93%) rename C17/{ => ArraysToFunctions2}/arrays_to_functions_example_2.c (100%) rename C18/{ => BinaryFile}/binary_file_example.c (100%) create mode 100644 C18/BinaryFile/numbers.dat rename C18/{ => FileOpen}/file_open_example.c (100%) create mode 100644 C18/TextFile/numbers.txt rename C18/{ => TextFile}/text_file_example.c (100%) rename C19/{ => DefineExamples}/define_example.c (100%) rename C20/{ => ConditionalDirective}/conditional_directive_example.c (73%) rename C20/{ => FormattingDirective}/formatting_directive_example.c (73%) rename LC16/{ => Dynamic1}/Dynamic1.c (95%) rename LC16/{ => Dynamic2}/Dynamic2.c (100%) rename LC16/{ => Dynamic3}/Dynamic3.c (99%) rename LC17/{ => DynamicFunction}/DynamicFunction.c (91%) create mode 100644 LC18/EndOfFile/EndOfFile.c create mode 100644 LC18/EndOfFile/numbers.txt create mode 100644 LC18/EndOfFileBreak/EndOfFileBreak.c create mode 100644 LC18/EndOfFileBreak/numbers.txt create mode 100644 LC18/EndOfFileScanf/EndOfFileScanf.c create mode 100644 LC18/EndOfFileScanf/numbers.txt create mode 100644 LC18/FileMove/data.dat rename LC18/{ => FileMove}/filemove.c (80%) create mode 100644 LC18/FileSize/data.dat rename LC18/{ => FileSize}/filesize.c (91%) rename LC19/{ => ConstHashDefine}/ConstHashDefine.c (67%) rename LC19/{ => Enum}/enum.c (100%) rename LC19/{ => FileHeader}/file_header.c (93%) create mode 100644 LC19/FileHeader/strdata.dat rename LC19/{ => FileHeaderMove}/file_header_move.c (100%) create mode 100644 LC19/FileHeaderMove/strdata.dat rename LC19/{ => Static}/Static.c (100%) rename LC19/{ => Structure1}/Structure_1.c (100%) rename LC19/{ => Structure2}/Structure_2.c (72%) rename LC19/{ => Structure3}/Structure_3.c (73%) create mode 100644 LC19/StructureFunction/StructureFunc.c delete mode 100644 LC19/WriteHeader.c create mode 100644 LC20/AreaFunc.c create mode 100644 LC20/AreaFunc.h create mode 100644 LC20/Main.c diff --git a/C17/arrays_to_functions_example_1.c b/C17/ArraysToFunctions1/arrays_to_functions_example_1.c similarity index 93% rename from C17/arrays_to_functions_example_1.c rename to C17/ArraysToFunctions1/arrays_to_functions_example_1.c index e222e30..5d31e47 100644 --- a/C17/arrays_to_functions_example_1.c +++ b/C17/ArraysToFunctions1/arrays_to_functions_example_1.c @@ -13,7 +13,7 @@ void PopulateTheArray ( int Size, int ArrayData[]) } -// Simple function do display contents an integer array +// Simple function to display contents of an integer array void DisplayTheArray ( int Size, int ArrayData[]) { int i; // Variable to use in our loop diff --git a/C17/arrays_to_functions_example_2.c b/C17/ArraysToFunctions2/arrays_to_functions_example_2.c similarity index 100% rename from C17/arrays_to_functions_example_2.c rename to C17/ArraysToFunctions2/arrays_to_functions_example_2.c diff --git a/C18/binary_file_example.c b/C18/BinaryFile/binary_file_example.c similarity index 100% rename from C18/binary_file_example.c rename to C18/BinaryFile/binary_file_example.c diff --git a/C18/BinaryFile/numbers.dat b/C18/BinaryFile/numbers.dat new file mode 100644 index 0000000000000000000000000000000000000000..a7a19b2851848a743fa87e7b01ccebeeaa5f2142 GIT binary patch literal 44 lcmXpu-tEZ9z`(!+#LPg<0>rF9%m&2lK+FNeoIuP4!~i}B0h#~+ literal 0 HcmV?d00001 diff --git a/C18/file_open_example.c b/C18/FileOpen/file_open_example.c similarity index 100% rename from C18/file_open_example.c rename to C18/FileOpen/file_open_example.c diff --git a/C18/TextFile/numbers.txt b/C18/TextFile/numbers.txt new file mode 100644 index 0000000..f00c965 --- /dev/null +++ b/C18/TextFile/numbers.txt @@ -0,0 +1,10 @@ +1 +2 +3 +4 +5 +6 +7 +8 +9 +10 diff --git a/C18/text_file_example.c b/C18/TextFile/text_file_example.c similarity index 100% rename from C18/text_file_example.c rename to C18/TextFile/text_file_example.c diff --git a/C19/define_example.c b/C19/DefineExamples/define_example.c similarity index 100% rename from C19/define_example.c rename to C19/DefineExamples/define_example.c diff --git a/C20/conditional_directive_example.c b/C20/ConditionalDirective/conditional_directive_example.c similarity index 73% rename from C20/conditional_directive_example.c rename to C20/ConditionalDirective/conditional_directive_example.c index 3f94286..c2fffe2 100644 --- a/C20/conditional_directive_example.c +++ b/C20/ConditionalDirective/conditional_directive_example.c @@ -1,14 +1,14 @@ #include #include -#define DEBUG_ON 1 +#define DEBUG_ON 0 int main(void) { #if DEBUG_ON == 1 printf("Debug mode - about to do something\n"); #else - print("Running in standard mode"); + printf("Running in standard mode"); #endif return 0; diff --git a/C20/formatting_directive_example.c b/C20/FormattingDirective/formatting_directive_example.c similarity index 73% rename from C20/formatting_directive_example.c rename to C20/FormattingDirective/formatting_directive_example.c index 13f000b..1973848 100644 --- a/C20/formatting_directive_example.c +++ b/C20/FormattingDirective/formatting_directive_example.c @@ -1,14 +1,14 @@ #include #include -#define DEBUG_ON 1 +//#define DEBUG_ON 0 int main(void) { #ifdef DEBUG_ON printf("Debug mode - about to do something\n"); #else - print("Running in standard mode"); + printf("Running in standard mode"); #endif return 0; diff --git a/LC15/PointerToArray1/pointer_to_array_1.c b/LC15/PointerToArray1/pointer_to_array_1.c index 078a657..944f8b1 100644 --- a/LC15/PointerToArray1/pointer_to_array_1.c +++ b/LC15/PointerToArray1/pointer_to_array_1.c @@ -19,15 +19,6 @@ int main(void) // Main : Execution starts here... for ( i = 0 ; i < 10 ; i++ ) printf ("Item %d contains value %d\n", i, pArray[i] ); - printf ("Pointer method 1\n"); - for ( i = 0 ; i < 10 ; i++ ) - printf ("Item %d contains value %d\n", i, *(pArray+i) ); - - printf ("Pointer method 2 (only works for moving sequentially\n"); - for ( i = 0 ; i < 10 ; i++ ) - printf ("Item %d contains value %d\n", i, *pArray++ ); - - // Exit the application return 0; } diff --git a/LC15/PointerToArray2/pointer_to_array_2.c b/LC15/PointerToArray2/pointer_to_array_2.c index 078a657..638cd79 100644 --- a/LC15/PointerToArray2/pointer_to_array_2.c +++ b/LC15/PointerToArray2/pointer_to_array_2.c @@ -19,7 +19,7 @@ int main(void) // Main : Execution starts here... for ( i = 0 ; i < 10 ; i++ ) printf ("Item %d contains value %d\n", i, pArray[i] ); - printf ("Pointer method 1\n"); + printf ("Pointer method 2 using index\n"); for ( i = 0 ; i < 10 ; i++ ) printf ("Item %d contains value %d\n", i, *(pArray+i) ); @@ -27,7 +27,6 @@ int main(void) // Main : Execution starts here... for ( i = 0 ; i < 10 ; i++ ) printf ("Item %d contains value %d\n", i, *pArray++ ); - // Exit the application return 0; } diff --git a/LC16/Dynamic1.c b/LC16/Dynamic1/Dynamic1.c similarity index 95% rename from LC16/Dynamic1.c rename to LC16/Dynamic1/Dynamic1.c index 8d1e0e9..bf57a95 100644 --- a/LC16/Dynamic1.c +++ b/LC16/Dynamic1/Dynamic1.c @@ -17,7 +17,7 @@ int main(void) { int *ipArray = NULL; /* Create the pointer and set */ - /* to null to start with */ + /* to null to start with */ int iSize = 0; /* Define our 'size' variable */ int i; /* A Loop variables */ diff --git a/LC16/Dynamic2.c b/LC16/Dynamic2/Dynamic2.c similarity index 100% rename from LC16/Dynamic2.c rename to LC16/Dynamic2/Dynamic2.c diff --git a/LC16/Dynamic3.c b/LC16/Dynamic3/Dynamic3.c similarity index 99% rename from LC16/Dynamic3.c rename to LC16/Dynamic3/Dynamic3.c index e883720..a745531 100644 --- a/LC16/Dynamic3.c +++ b/LC16/Dynamic3/Dynamic3.c @@ -46,6 +46,7 @@ int main(void) /* 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; diff --git a/LC17/DynamicFunction.c b/LC17/DynamicFunction/DynamicFunction.c similarity index 91% rename from LC17/DynamicFunction.c rename to LC17/DynamicFunction/DynamicFunction.c index 8dace68..449c9a5 100644 --- a/LC17/DynamicFunction.c +++ b/LC17/DynamicFunction/DynamicFunction.c @@ -59,10 +59,12 @@ int main(void) /* Store the base memory address for use later */ ipStartValue = ipArray; - /* Populate the array (Method 2 - Use Pointers: This is much faster !) */ + /* Populate the array ( Use Pointers: This is much faster !) */ for ( i = 0 ; i < iSize ; i++ ) - ipArray[i] = 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) ); diff --git a/LC18/EndOfFile/EndOfFile.c b/LC18/EndOfFile/EndOfFile.c new file mode 100644 index 0000000..4051455 --- /dev/null +++ b/LC18/EndOfFile/EndOfFile.c @@ -0,0 +1,55 @@ +#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/LC18/EndOfFile/numbers.txt b/LC18/EndOfFile/numbers.txt new file mode 100644 index 0000000..f00c965 --- /dev/null +++ b/LC18/EndOfFile/numbers.txt @@ -0,0 +1,10 @@ +1 +2 +3 +4 +5 +6 +7 +8 +9 +10 diff --git a/LC18/EndOfFileBreak/EndOfFileBreak.c b/LC18/EndOfFileBreak/EndOfFileBreak.c new file mode 100644 index 0000000..385bf61 --- /dev/null +++ b/LC18/EndOfFileBreak/EndOfFileBreak.c @@ -0,0 +1,57 @@ +#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/LC18/EndOfFileBreak/numbers.txt b/LC18/EndOfFileBreak/numbers.txt new file mode 100644 index 0000000..f00c965 --- /dev/null +++ b/LC18/EndOfFileBreak/numbers.txt @@ -0,0 +1,10 @@ +1 +2 +3 +4 +5 +6 +7 +8 +9 +10 diff --git a/LC18/EndOfFileScanf/EndOfFileScanf.c b/LC18/EndOfFileScanf/EndOfFileScanf.c new file mode 100644 index 0000000..d24b0b6 --- /dev/null +++ b/LC18/EndOfFileScanf/EndOfFileScanf.c @@ -0,0 +1,54 @@ +#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/LC18/EndOfFileScanf/numbers.txt b/LC18/EndOfFileScanf/numbers.txt new file mode 100644 index 0000000..f00c965 --- /dev/null +++ b/LC18/EndOfFileScanf/numbers.txt @@ -0,0 +1,10 @@ +1 +2 +3 +4 +5 +6 +7 +8 +9 +10 diff --git a/LC18/FileMove/data.dat b/LC18/FileMove/data.dat new file mode 100644 index 0000000000000000000000000000000000000000..a7a19b2851848a743fa87e7b01ccebeeaa5f2142 GIT binary patch literal 44 lcmXpu-tEZ9z`(!+#LPg<0>rF9%m&2lK+FNeoIuP4!~i}B0h#~+ literal 0 HcmV?d00001 diff --git a/LC18/filemove.c b/LC18/FileMove/filemove.c similarity index 80% rename from LC18/filemove.c rename to LC18/FileMove/filemove.c index b8108c1..3cb4589 100644 --- a/LC18/filemove.c +++ b/LC18/FileMove/filemove.c @@ -30,10 +30,13 @@ int main(void) fseek (fptr, Locn, SEEK_SET); /* And read a single integer in */ - fread (&ValRead, sizeof(int), 1, fptr ); - - /* Display the read value */ - printf ("Item %d is %d\n",Item,ValRead); + 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); diff --git a/LC18/FileSize/data.dat b/LC18/FileSize/data.dat new file mode 100644 index 0000000000000000000000000000000000000000..a7a19b2851848a743fa87e7b01ccebeeaa5f2142 GIT binary patch literal 44 lcmXpu-tEZ9z`(!+#LPg<0>rF9%m&2lK+FNeoIuP4!~i}B0h#~+ literal 0 HcmV?d00001 diff --git a/LC18/filesize.c b/LC18/FileSize/filesize.c similarity index 91% rename from LC18/filesize.c rename to LC18/FileSize/filesize.c index 3c0f676..c5d9771 100644 --- a/LC18/filesize.c +++ b/LC18/FileSize/filesize.c @@ -3,7 +3,7 @@ /* Example 3 - jumping & getting element count */ -main() +int main() { long FileBytes; @@ -27,7 +27,7 @@ main() FileBytes = FileBytes / sizeof(int); /* Display the read value */ - printf ("No. of items in file is %d\n",FileBytes); + printf ("No. of items in file is %ld\n",FileBytes); /* And close */ fclose (fptr); diff --git a/LC19/ConstHashDefine.c b/LC19/ConstHashDefine/ConstHashDefine.c similarity index 67% rename from LC19/ConstHashDefine.c rename to LC19/ConstHashDefine/ConstHashDefine.c index 4f253b7..51736a4 100644 --- a/LC19/ConstHashDefine.c +++ b/LC19/ConstHashDefine/ConstHashDefine.c @@ -6,6 +6,8 @@ //global integer constant const int Y = 10; +void func(); + int main() { //local integer constant` @@ -15,11 +17,17 @@ int main() printf("Value of Y: %d\n", Y); printf("Value of Z: %d\n", Z); -// #undef X -// #define X 300 -// printf("Value of X: %d\n", X); + func(); -// Y = 30; + //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/LC19/enum.c b/LC19/Enum/enum.c similarity index 100% rename from LC19/enum.c rename to LC19/Enum/enum.c diff --git a/LC19/file_header.c b/LC19/FileHeader/file_header.c similarity index 93% rename from LC19/file_header.c rename to LC19/FileHeader/file_header.c index 074828f..83c1b54 100644 --- a/LC19/file_header.c +++ b/LC19/FileHeader/file_header.c @@ -55,11 +55,11 @@ int main() /* Update the values in the structure */ MyRecord.NoItems = items; - MyRecord.NoItems = (float)sum / (float)items; + MyRecord.average = (float)sum / (float)items; /* rewind the file to write the structure again */ fseek(fptr, SEEK_SET, 0); - rewind (fptr); + //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); diff --git a/LC19/FileHeader/strdata.dat b/LC19/FileHeader/strdata.dat new file mode 100644 index 0000000000000000000000000000000000000000..68cf19d2ef7dd3b1bafdfa553b76bd283bd866a7 GIT binary patch literal 412 zcmWmARYC$#006;f7pT~cg#mV=SSVt5q8NbpKZC=^>;d*Kgis-b{BgB4)KW)14K&h3 zGcB~zMuaHsbkIqRF1qQVmp=L#V2~k(8DW$$;*2xFBvVY2V1`-dm?ue!1r}LinH5%9 zW1S5)*TyV)1*W7T+9rrx&$P>@J@X8zS6!_qi OB42#-!!ISuRQLx%J`|<^ literal 0 HcmV?d00001 diff --git a/LC19/file_header_move.c b/LC19/FileHeaderMove/file_header_move.c similarity index 100% rename from LC19/file_header_move.c rename to LC19/FileHeaderMove/file_header_move.c diff --git a/LC19/FileHeaderMove/strdata.dat b/LC19/FileHeaderMove/strdata.dat new file mode 100644 index 0000000000000000000000000000000000000000..68cf19d2ef7dd3b1bafdfa553b76bd283bd866a7 GIT binary patch literal 412 zcmWmARYC$#006;f7pT~cg#mV=SSVt5q8NbpKZC=^>;d*Kgis-b{BgB4)KW)14K&h3 zGcB~zMuaHsbkIqRF1qQVmp=L#V2~k(8DW$$;*2xFBvVY2V1`-dm?ue!1r}LinH5%9 zW1S5)*TyV)1*W7T+9rrx&$P>@J@X8zS6!_qi OB42#-!!ISuRQLx%J`|<^ literal 0 HcmV?d00001 diff --git a/LC19/Static.c b/LC19/Static/Static.c similarity index 100% rename from LC19/Static.c rename to LC19/Static/Static.c diff --git a/LC19/Structure_1.c b/LC19/Structure1/Structure_1.c similarity index 100% rename from LC19/Structure_1.c rename to LC19/Structure1/Structure_1.c diff --git a/LC19/Structure_2.c b/LC19/Structure2/Structure_2.c similarity index 72% rename from LC19/Structure_2.c rename to LC19/Structure2/Structure_2.c index ad9748a..50d45e5 100644 --- a/LC19/Structure_2.c +++ b/LC19/Structure2/Structure_2.c @@ -13,7 +13,7 @@ int main(void) struct DataSet MyPoints[5]; /* Define array of structure, */ /* type DataSet */ - /* Elements : 10 */ + /* Elements : 5 */ int i; /* Define an integer */ @@ -21,17 +21,17 @@ int main(void) for ( i = 0 ; i < 5 ; i++ ) /* Populate structure */ { - MyPoints[i].x = i; - MyPoints[i].y = i*i; + 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); + printf("\n\t Set %d : ",i); + printf("\t X = %d ",MyPoints[i].x); + printf("\t Y = %d ",MyPoints[i].y); } diff --git a/LC19/Structure_3.c b/LC19/Structure3/Structure_3.c similarity index 73% rename from LC19/Structure_3.c rename to LC19/Structure3/Structure_3.c index 834d680..13ae5e8 100644 --- a/LC19/Structure_3.c +++ b/LC19/Structure3/Structure_3.c @@ -25,23 +25,23 @@ int main(void) for ( i = 0 ; i < 5 ; i++ ) /* Populate structure */ { - MyPoints[i].x = i; - MyPoints[i].y = i*i; + 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); + 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]; + CpPoints[i] = MyPoints[i]; /* And print it out again */ @@ -50,9 +50,9 @@ int main(void) 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); + printf("\n\t Set %d : ",i); + printf("\t X = %d ",CpPoints[i].x); + printf("\t Y = %d ",CpPoints[i].y); } return 0; diff --git a/LC19/StructureFunction/StructureFunc.c b/LC19/StructureFunction/StructureFunc.c new file mode 100644 index 0000000..2ab3ac4 --- /dev/null +++ b/LC19/StructureFunction/StructureFunc.c @@ -0,0 +1,36 @@ +#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/LC19/WriteHeader.c b/LC19/WriteHeader.c deleted file mode 100644 index 3ae28c6..0000000 --- a/LC19/WriteHeader.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(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/LC20/AreaFunc.c b/LC20/AreaFunc.c new file mode 100644 index 0000000..3e5b3c9 --- /dev/null +++ b/LC20/AreaFunc.c @@ -0,0 +1,17 @@ +#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/LC20/AreaFunc.h b/LC20/AreaFunc.h new file mode 100644 index 0000000..8f4ce63 --- /dev/null +++ b/LC20/AreaFunc.h @@ -0,0 +1,2 @@ +float CalculateAreaOfCircle ( float radius ); +float CalculateSurfaceAreaOfCylinder ( float radius, float length ); \ No newline at end of file diff --git a/LC20/Main.c b/LC20/Main.c new file mode 100644 index 0000000..27cbebb --- /dev/null +++ b/LC20/Main.c @@ -0,0 +1,34 @@ +#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; +} +