Changes to file structure for VSCode and bug fixes

This commit is contained in:
Louise Brown
2022-11-14 13:51:40 +00:00
parent 7fd908108a
commit caee190d05
41 changed files with 346 additions and 124 deletions

View File

@@ -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;
}

View File

@@ -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);

BIN
LC19/FileHeader/strdata.dat Normal file

Binary file not shown.

Binary file not shown.

View File

@@ -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);
}

View File

@@ -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;

View 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 );
}

View File

@@ -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;
}