Initial commit of lecture code

This commit is contained in:
Louise Brown
2022-04-07 16:58:01 +01:00
commit 5b205092a7
60 changed files with 2413 additions and 0 deletions

25
LC19/ConstHashDefine.c Normal file
View File

@@ -0,0 +1,25 @@
#include <stdio.h>
//macro definition
#define X 30
//global integer constant
const int Y = 10;
int main()
{
//local integer constant`
const int Z = 20;
printf("Value of X: %d\n", X);
printf("Value of Y: %d\n", Y);
printf("Value of Z: %d\n", Z);
// #undef X
// #define X 300
// printf("Value of X: %d\n", X);
// Y = 30;
return 0;
}

45
LC19/Static.c Normal file
View File

@@ -0,0 +1,45 @@
/*
Static variables - a variable that is created when a function is first
called and remains until the program terminates
This is to say that in future calls to the function it is not redefined
and can take the value it last had in the function.
This should hopefully show this !
*/
#include <stdio.h>
#include <stdlib.h>
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;
}

27
LC19/Structure_1.c Normal file
View File

@@ -0,0 +1,27 @@
#include <stdio.h>
#include <stdlib.h>
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;
}

39
LC19/Structure_2.c Normal file
View File

@@ -0,0 +1,39 @@
#include <stdio.h>
#include <stdlib.h>
struct DataSet /* Define a struture called DataSet */
{
int x; /* The X data */
int y; /* The Y data */
};
int main(void)
{
struct DataSet MyPoints[5]; /* Define array of structure, */
/* type DataSet */
/* Elements : 10 */
int i; /* Define an integer */
for ( i = 0 ; i < 5 ; i++ ) /* Populate structure */
{
MyPoints[i].x = i;
MyPoints[i].y = i*i;
}
printf("\nThe dataset values are "); /* Display message & Values */
for ( i = 0 ; i < 5 ; i++ )
{
printf("\n\t Set %d : ",i);
printf("\t X = %d ",MyPoints[i].x);
printf("\t Y = %d ",MyPoints[i].y);
}
return 0;
}

59
LC19/Structure_3.c Normal file
View File

@@ -0,0 +1,59 @@
#include <stdio.h>
#include <stdlib.h>
struct DataSet /* Define a struture called DataSet */
{
int x; /* The X data */
int y; /* The Y data */
};
int main(void)
{
struct DataSet MyPoints[5]; /* Define array of structure, */
/* type DataSet */
/* Elements : 10 */
struct DataSet CpPoints[5]; /* Define array of structure, */
/* type DataSet */
/* Elements : 10 */
int i; /* Define an integer */
for ( i = 0 ; i < 5 ; i++ ) /* Populate structure */
{
MyPoints[i].x = i;
MyPoints[i].y = i*i;
}
printf("\nThe dataset values are "); /* Display message & Values */
for ( i = 0 ; i < 5 ; i++ )
{
printf("\n\t Set %d : ",i);
printf("\t X = %d ",MyPoints[i].x);
printf("\t Y = %d ",MyPoints[i].y);
}
/* Make a copy of the data */
for ( i = 0 ; i < 5 ; i++ )
CpPoints[i] = MyPoints[i];
/* And print it out again */
printf("\nThe copied dataset values are "); /* Display message & Values */
for ( i = 0 ; i < 5 ; i++ )
{
printf("\n\t Set %d : ",i);
printf("\t X = %d ",MyPoints[i].x);
printf("\t Y = %d ",MyPoints[i].y);
}
return 0;
}

77
LC19/WriteHeader.c Normal file
View File

@@ -0,0 +1,77 @@
#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;
}

View File

@@ -0,0 +1,90 @@
#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);
}
/* 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;
}

View File

@@ -0,0 +1,99 @@
#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;
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;
}

View File

@@ -0,0 +1,77 @@
#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(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;
}

24
LC19/enum.c Normal file
View File

@@ -0,0 +1,24 @@
#include <stdio.h>
#include <stdlib.h>
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;
}

41
LC19/enumdefine.c Normal file
View File

@@ -0,0 +1,41 @@
/* This program shows the use of an 'enum' statements to make code
more general. This is an 'OK' method but realise that it does
mean that we need to recompile if we change a vlaue.
This method is best used only for fixed constants (eg PI )
While this version acts the same as the previous, it is better
as the compiler handles the 'enum'. Whereas the pre-processor
deals with #defines (performing simple substitutions) this is done
no with little error checking, the 'enum' is dealt with by the
compiler which finds error more easily - hence more stable code.
*/
#include <stdio.h>
#include <stdlib.h>
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;
}

90
LC19/file_header.c Normal file
View File

@@ -0,0 +1,90 @@
#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);
}
/* 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;
}

99
LC19/file_header_move.c Normal file
View File

@@ -0,0 +1,99 @@
#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;
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;
}

72
LC19/swap.c Normal file
View File

@@ -0,0 +1,72 @@
#include <stdio.h>
#include <stdlib.h>
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;
}

31
LC19/union.c Normal file
View File

@@ -0,0 +1,31 @@
#include <stdio.h>
#include <stdlib.h>
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;
}