move exercises and lectures into subfolders
This commit is contained in:
33
Lectures/LC19/ConstHashDefine/ConstHashDefine.c
Normal file
33
Lectures/LC19/ConstHashDefine/ConstHashDefine.c
Normal file
@@ -0,0 +1,33 @@
|
||||
#include <stdio.h>
|
||||
|
||||
//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;
|
||||
}
|
24
Lectures/LC19/Enum/enum.c
Normal file
24
Lectures/LC19/Enum/enum.c
Normal 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;
|
||||
}
|
90
Lectures/LC19/FileHeader/file_header.c
Normal file
90
Lectures/LC19/FileHeader/file_header.c
Normal 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.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;
|
||||
}
|
BIN
Lectures/LC19/FileHeader/strdata.dat
Normal file
BIN
Lectures/LC19/FileHeader/strdata.dat
Normal file
Binary file not shown.
99
Lectures/LC19/FileHeaderMove/file_header_move.c
Normal file
99
Lectures/LC19/FileHeaderMove/file_header_move.c
Normal 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;
|
||||
}
|
BIN
Lectures/LC19/FileHeaderMove/strdata.dat
Normal file
BIN
Lectures/LC19/FileHeaderMove/strdata.dat
Normal file
Binary file not shown.
45
Lectures/LC19/Static/Static.c
Normal file
45
Lectures/LC19/Static/Static.c
Normal 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
Lectures/LC19/Structure1/Structure_1.c
Normal file
27
Lectures/LC19/Structure1/Structure_1.c
Normal 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
Lectures/LC19/Structure2/Structure_2.c
Normal file
39
Lectures/LC19/Structure2/Structure_2.c
Normal 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 : 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;
|
||||
}
|
59
Lectures/LC19/Structure3/Structure_3.c
Normal file
59
Lectures/LC19/Structure3/Structure_3.c
Normal 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 ",CpPoints[i].x);
|
||||
printf("\t Y = %d ",CpPoints[i].y);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
36
Lectures/LC19/StructureFunction/StructureFunc.c
Normal file
36
Lectures/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 );
|
||||
}
|
90
Lectures/LC19/WriteHeaderArrayAndUpdate.c
Normal file
90
Lectures/LC19/WriteHeaderArrayAndUpdate.c
Normal 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
Lectures/LC19/WriteHeaderArrayAndUpdateAndSeek.c
Normal file
99
Lectures/LC19/WriteHeaderArrayAndUpdateAndSeek.c
Normal 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;
|
||||
}
|
77
Lectures/LC19/WriteHeaderWithArray.c
Normal file
77
Lectures/LC19/WriteHeaderWithArray.c
Normal 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;
|
||||
}
|
41
Lectures/LC19/enumdefine.c
Normal file
41
Lectures/LC19/enumdefine.c
Normal 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;
|
||||
}
|
72
Lectures/LC19/swap.c
Normal file
72
Lectures/LC19/swap.c
Normal 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
Lectures/LC19/union.c
Normal file
31
Lectures/LC19/union.c
Normal 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;
|
||||
|
||||
}
|
Reference in New Issue
Block a user