Changes to file structure for VSCode and bug fixes
This commit is contained in:
parent
7fd908108a
commit
caee190d05
@ -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[])
|
void DisplayTheArray ( int Size, int ArrayData[])
|
||||||
{
|
{
|
||||||
int i; // Variable to use in our loop
|
int i; // Variable to use in our loop
|
BIN
C18/BinaryFile/numbers.dat
Normal file
BIN
C18/BinaryFile/numbers.dat
Normal file
Binary file not shown.
10
C18/TextFile/numbers.txt
Normal file
10
C18/TextFile/numbers.txt
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
1
|
||||||
|
2
|
||||||
|
3
|
||||||
|
4
|
||||||
|
5
|
||||||
|
6
|
||||||
|
7
|
||||||
|
8
|
||||||
|
9
|
||||||
|
10
|
@ -1,14 +1,14 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <conio.h>
|
#include <conio.h>
|
||||||
|
|
||||||
#define DEBUG_ON 1
|
#define DEBUG_ON 0
|
||||||
|
|
||||||
int main(void)
|
int main(void)
|
||||||
{
|
{
|
||||||
#if DEBUG_ON == 1
|
#if DEBUG_ON == 1
|
||||||
printf("Debug mode - about to do something\n");
|
printf("Debug mode - about to do something\n");
|
||||||
#else
|
#else
|
||||||
print("Running in standard mode");
|
printf("Running in standard mode");
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
return 0;
|
return 0;
|
@ -1,14 +1,14 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <conio.h>
|
#include <conio.h>
|
||||||
|
|
||||||
#define DEBUG_ON 1
|
//#define DEBUG_ON 0
|
||||||
|
|
||||||
int main(void)
|
int main(void)
|
||||||
{
|
{
|
||||||
#ifdef DEBUG_ON
|
#ifdef DEBUG_ON
|
||||||
printf("Debug mode - about to do something\n");
|
printf("Debug mode - about to do something\n");
|
||||||
#else
|
#else
|
||||||
print("Running in standard mode");
|
printf("Running in standard mode");
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
return 0;
|
return 0;
|
@ -19,15 +19,6 @@ int main(void) // Main : Execution starts here...
|
|||||||
for ( i = 0 ; i < 10 ; i++ )
|
for ( i = 0 ; i < 10 ; i++ )
|
||||||
printf ("Item %d contains value %d\n", i, pArray[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
|
// Exit the application
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -19,7 +19,7 @@ int main(void) // Main : Execution starts here...
|
|||||||
for ( i = 0 ; i < 10 ; i++ )
|
for ( i = 0 ; i < 10 ; i++ )
|
||||||
printf ("Item %d contains value %d\n", i, pArray[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++ )
|
for ( i = 0 ; i < 10 ; i++ )
|
||||||
printf ("Item %d contains value %d\n", i, *(pArray+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++ )
|
for ( i = 0 ; i < 10 ; i++ )
|
||||||
printf ("Item %d contains value %d\n", i, *pArray++ );
|
printf ("Item %d contains value %d\n", i, *pArray++ );
|
||||||
|
|
||||||
|
|
||||||
// Exit the application
|
// Exit the application
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -17,7 +17,7 @@
|
|||||||
int main(void)
|
int main(void)
|
||||||
{
|
{
|
||||||
int *ipArray = NULL; /* Create the pointer and set */
|
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 iSize = 0; /* Define our 'size' variable */
|
||||||
|
|
||||||
int i; /* A Loop variables */
|
int i; /* A Loop variables */
|
@ -46,6 +46,7 @@ int main(void)
|
|||||||
/* Populate the array (Method 2 - Use Pointers: This is much faster !) */
|
/* Populate the array (Method 2 - Use Pointers: This is much faster !) */
|
||||||
for ( i = 0 ; i < iSize ; i++ )
|
for ( i = 0 ; i < iSize ; i++ )
|
||||||
*ipArray++ = iSize - i;
|
*ipArray++ = iSize - i;
|
||||||
|
|
||||||
|
|
||||||
/* Reset the pointer to the origin of the array */
|
/* Reset the pointer to the origin of the array */
|
||||||
ipArray = ipStartValue;
|
ipArray = ipStartValue;
|
@ -59,10 +59,12 @@ int main(void)
|
|||||||
/* Store the base memory address for use later */
|
/* Store the base memory address for use later */
|
||||||
ipStartValue = ipArray;
|
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++ )
|
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 */
|
/* Display the sum of the values in the array */
|
||||||
|
|
||||||
printf("\nThe sum of the array values is %d ",Sum(ipArray,iSize) );
|
printf("\nThe sum of the array values is %d ",Sum(ipArray,iSize) );
|
55
LC18/EndOfFile/EndOfFile.c
Normal file
55
LC18/EndOfFile/EndOfFile.c
Normal file
@ -0,0 +1,55 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
// 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
|
||||||
|
}
|
10
LC18/EndOfFile/numbers.txt
Normal file
10
LC18/EndOfFile/numbers.txt
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
1
|
||||||
|
2
|
||||||
|
3
|
||||||
|
4
|
||||||
|
5
|
||||||
|
6
|
||||||
|
7
|
||||||
|
8
|
||||||
|
9
|
||||||
|
10
|
57
LC18/EndOfFileBreak/EndOfFileBreak.c
Normal file
57
LC18/EndOfFileBreak/EndOfFileBreak.c
Normal file
@ -0,0 +1,57 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
// 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
|
||||||
|
}
|
10
LC18/EndOfFileBreak/numbers.txt
Normal file
10
LC18/EndOfFileBreak/numbers.txt
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
1
|
||||||
|
2
|
||||||
|
3
|
||||||
|
4
|
||||||
|
5
|
||||||
|
6
|
||||||
|
7
|
||||||
|
8
|
||||||
|
9
|
||||||
|
10
|
54
LC18/EndOfFileScanf/EndOfFileScanf.c
Normal file
54
LC18/EndOfFileScanf/EndOfFileScanf.c
Normal file
@ -0,0 +1,54 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
// 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
|
||||||
|
}
|
10
LC18/EndOfFileScanf/numbers.txt
Normal file
10
LC18/EndOfFileScanf/numbers.txt
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
1
|
||||||
|
2
|
||||||
|
3
|
||||||
|
4
|
||||||
|
5
|
||||||
|
6
|
||||||
|
7
|
||||||
|
8
|
||||||
|
9
|
||||||
|
10
|
BIN
LC18/FileMove/data.dat
Normal file
BIN
LC18/FileMove/data.dat
Normal file
Binary file not shown.
@ -30,10 +30,13 @@ int main(void)
|
|||||||
fseek (fptr, Locn, SEEK_SET);
|
fseek (fptr, Locn, SEEK_SET);
|
||||||
|
|
||||||
/* And read a single integer in */
|
/* And read a single integer in */
|
||||||
fread (&ValRead, sizeof(int), 1, fptr );
|
if (fread (&ValRead, sizeof(int), 1, fptr ) == 1)
|
||||||
|
{
|
||||||
/* Display the read value */
|
/* Display the read value */
|
||||||
printf ("Item %d is %d\n",Item,ValRead);
|
printf ("Item %d is %d\n",Item,ValRead);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
printf("Failed to read at location %d\n", Item);
|
||||||
|
|
||||||
/* And close */
|
/* And close */
|
||||||
fclose (fptr);
|
fclose (fptr);
|
BIN
LC18/FileSize/data.dat
Normal file
BIN
LC18/FileSize/data.dat
Normal file
Binary file not shown.
@ -3,7 +3,7 @@
|
|||||||
|
|
||||||
/* Example 3 - jumping & getting element count */
|
/* Example 3 - jumping & getting element count */
|
||||||
|
|
||||||
main()
|
int main()
|
||||||
{
|
{
|
||||||
|
|
||||||
long FileBytes;
|
long FileBytes;
|
||||||
@ -27,7 +27,7 @@ main()
|
|||||||
FileBytes = FileBytes / sizeof(int);
|
FileBytes = FileBytes / sizeof(int);
|
||||||
|
|
||||||
/* Display the read value */
|
/* 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 */
|
/* And close */
|
||||||
fclose (fptr);
|
fclose (fptr);
|
@ -6,6 +6,8 @@
|
|||||||
//global integer constant
|
//global integer constant
|
||||||
const int Y = 10;
|
const int Y = 10;
|
||||||
|
|
||||||
|
void func();
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
//local integer constant`
|
//local integer constant`
|
||||||
@ -15,11 +17,17 @@ int main()
|
|||||||
printf("Value of Y: %d\n", Y);
|
printf("Value of Y: %d\n", Y);
|
||||||
printf("Value of Z: %d\n", Z);
|
printf("Value of Z: %d\n", Z);
|
||||||
|
|
||||||
// #undef X
|
func();
|
||||||
// #define X 300
|
|
||||||
// printf("Value of X: %d\n", X);
|
|
||||||
|
|
||||||
// Y = 30;
|
//Y = 30;
|
||||||
|
Z = 40;
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void func()
|
||||||
|
{
|
||||||
|
int Z = 40;
|
||||||
|
printf("Value of Z in function = %d\n", Z);
|
||||||
|
//Y = 30;
|
||||||
|
}
|
@ -55,11 +55,11 @@ int main()
|
|||||||
|
|
||||||
/* Update the values in the structure */
|
/* Update the values in the structure */
|
||||||
MyRecord.NoItems = items;
|
MyRecord.NoItems = items;
|
||||||
MyRecord.NoItems = (float)sum / (float)items;
|
MyRecord.average = (float)sum / (float)items;
|
||||||
|
|
||||||
/* rewind the file to write the structure again */
|
/* rewind the file to write the structure again */
|
||||||
fseek(fptr, SEEK_SET, 0);
|
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 */
|
/* This line does the writing of the structure */
|
||||||
fwrite ( &MyRecord, sizeof(struct MyStruct), 1, fptr);
|
fwrite ( &MyRecord, sizeof(struct MyStruct), 1, fptr);
|
BIN
LC19/FileHeader/strdata.dat
Normal file
BIN
LC19/FileHeader/strdata.dat
Normal file
Binary file not shown.
BIN
LC19/FileHeaderMove/strdata.dat
Normal file
BIN
LC19/FileHeaderMove/strdata.dat
Normal file
Binary file not shown.
@ -13,7 +13,7 @@ int main(void)
|
|||||||
|
|
||||||
struct DataSet MyPoints[5]; /* Define array of structure, */
|
struct DataSet MyPoints[5]; /* Define array of structure, */
|
||||||
/* type DataSet */
|
/* type DataSet */
|
||||||
/* Elements : 10 */
|
/* Elements : 5 */
|
||||||
|
|
||||||
int i; /* Define an integer */
|
int i; /* Define an integer */
|
||||||
|
|
||||||
@ -21,17 +21,17 @@ int main(void)
|
|||||||
|
|
||||||
for ( i = 0 ; i < 5 ; i++ ) /* Populate structure */
|
for ( i = 0 ; i < 5 ; i++ ) /* Populate structure */
|
||||||
{
|
{
|
||||||
MyPoints[i].x = i;
|
MyPoints[i].x = i;
|
||||||
MyPoints[i].y = i*i;
|
MyPoints[i].y = i*i;
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("\nThe dataset values are "); /* Display message & Values */
|
printf("\nThe dataset values are "); /* Display message & Values */
|
||||||
|
|
||||||
for ( i = 0 ; i < 5 ; i++ )
|
for ( i = 0 ; i < 5 ; i++ )
|
||||||
{
|
{
|
||||||
printf("\n\t Set %d : ",i);
|
printf("\n\t Set %d : ",i);
|
||||||
printf("\t X = %d ",MyPoints[i].x);
|
printf("\t X = %d ",MyPoints[i].x);
|
||||||
printf("\t Y = %d ",MyPoints[i].y);
|
printf("\t Y = %d ",MyPoints[i].y);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -25,23 +25,23 @@ int main(void)
|
|||||||
|
|
||||||
for ( i = 0 ; i < 5 ; i++ ) /* Populate structure */
|
for ( i = 0 ; i < 5 ; i++ ) /* Populate structure */
|
||||||
{
|
{
|
||||||
MyPoints[i].x = i;
|
MyPoints[i].x = i;
|
||||||
MyPoints[i].y = i*i;
|
MyPoints[i].y = i*i;
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("\nThe dataset values are "); /* Display message & Values */
|
printf("\nThe dataset values are "); /* Display message & Values */
|
||||||
|
|
||||||
for ( i = 0 ; i < 5 ; i++ )
|
for ( i = 0 ; i < 5 ; i++ )
|
||||||
{
|
{
|
||||||
printf("\n\t Set %d : ",i);
|
printf("\n\t Set %d : ",i);
|
||||||
printf("\t X = %d ",MyPoints[i].x);
|
printf("\t X = %d ",MyPoints[i].x);
|
||||||
printf("\t Y = %d ",MyPoints[i].y);
|
printf("\t Y = %d ",MyPoints[i].y);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Make a copy of the data */
|
/* Make a copy of the data */
|
||||||
|
|
||||||
for ( i = 0 ; i < 5 ; i++ )
|
for ( i = 0 ; i < 5 ; i++ )
|
||||||
CpPoints[i] = MyPoints[i];
|
CpPoints[i] = MyPoints[i];
|
||||||
|
|
||||||
|
|
||||||
/* And print it out again */
|
/* And print it out again */
|
||||||
@ -50,9 +50,9 @@ int main(void)
|
|||||||
|
|
||||||
for ( i = 0 ; i < 5 ; i++ )
|
for ( i = 0 ; i < 5 ; i++ )
|
||||||
{
|
{
|
||||||
printf("\n\t Set %d : ",i);
|
printf("\n\t Set %d : ",i);
|
||||||
printf("\t X = %d ",MyPoints[i].x);
|
printf("\t X = %d ",CpPoints[i].x);
|
||||||
printf("\t Y = %d ",MyPoints[i].y);
|
printf("\t Y = %d ",CpPoints[i].y);
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
36
LC19/StructureFunction/StructureFunc.c
Normal file
36
LC19/StructureFunction/StructureFunc.c
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <math.h>
|
||||||
|
|
||||||
|
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 );
|
||||||
|
}
|
@ -1,77 +0,0 @@
|
|||||||
#include <stdio.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
|
17
LC20/AreaFunc.c
Normal file
17
LC20/AreaFunc.c
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <math.h>
|
||||||
|
|
||||||
|
|
||||||
|
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) ;
|
||||||
|
}
|
2
LC20/AreaFunc.h
Normal file
2
LC20/AreaFunc.h
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
float CalculateAreaOfCircle ( float radius );
|
||||||
|
float CalculateSurfaceAreaOfCylinder ( float radius, float length );
|
34
LC20/Main.c
Normal file
34
LC20/Main.c
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
#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;
|
||||||
|
}
|
||||||
|
|
Reference in New Issue
Block a user