Initial commit of lecture code
This commit is contained in:
commit
5b205092a7
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
.vscode
|
||||||
|
build/
|
47
LC10/simple_functions_1.c
Normal file
47
LC10/simple_functions_1.c
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <math.h> // Needed to give access to M_PI
|
||||||
|
|
||||||
|
/* FUNCTION: CalculateAreaOfCircle
|
||||||
|
|
||||||
|
INPUTS: radius float
|
||||||
|
RETURNS: area float
|
||||||
|
|
||||||
|
Description
|
||||||
|
Calculate area using area = M_PI * radius * radius
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
float CalculateAreaOfCircle ( float radius )
|
||||||
|
{
|
||||||
|
float area;
|
||||||
|
area = M_PI * radius * radius ;
|
||||||
|
return (area) ;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/* Show use of function */
|
||||||
|
int main (void)
|
||||||
|
{
|
||||||
|
// Declare variables - no need to initialise as values will be read in / calculated
|
||||||
|
float rad, Area;
|
||||||
|
|
||||||
|
// Prompt for and obtain value
|
||||||
|
printf ("Please enter the raduis of the circle: ");
|
||||||
|
scanf ("%f", &rad);
|
||||||
|
|
||||||
|
// Use our function to calculate the area
|
||||||
|
Area = CalculateAreaOfCircle(rad);
|
||||||
|
|
||||||
|
// And display the answer on the screen
|
||||||
|
printf ("The area of a circle of radius %f is %f\n", rad, Area );
|
||||||
|
|
||||||
|
// Note: As the function returns a value, if we did not need to store it
|
||||||
|
// we could calculate & display within the printf statement
|
||||||
|
|
||||||
|
//printf ("The area of a circle of radius %f is %f\n", rad, CalculateAreaOfCircle(r) );
|
||||||
|
|
||||||
|
// All done
|
||||||
|
return 0;
|
||||||
|
}
|
51
LC10/simple_functions_2.c
Normal file
51
LC10/simple_functions_2.c
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <math.h> // Needed to give access to M_PI
|
||||||
|
|
||||||
|
/* FUNCTION: CalculateSurfaceAreaOfCylinder
|
||||||
|
|
||||||
|
INPUTS: radius float
|
||||||
|
length float
|
||||||
|
|
||||||
|
RETURNS: SurfaceArea float
|
||||||
|
|
||||||
|
Description
|
||||||
|
Calculate the surface area of a cylinder given radius and length
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
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) ;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/* Show use of function */
|
||||||
|
int main (void)
|
||||||
|
{
|
||||||
|
// Declare variables - no need to initialise as values will be read in / calculated
|
||||||
|
float rad, len, SurfaceArea;
|
||||||
|
|
||||||
|
// Prompt for and obtain values
|
||||||
|
printf ("Please enter the radius of the cylinder: ");
|
||||||
|
scanf ("%f", &rad);
|
||||||
|
ad
|
||||||
|
printf ("Please enter the length of the cylinder: ");
|
||||||
|
scanf ("%f", &len);
|
||||||
|
|
||||||
|
// Use our function to calculate the area
|
||||||
|
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 );
|
||||||
|
|
||||||
|
// Note: As the function returns a value, if we did not need to store it
|
||||||
|
// we could calculate & display within the printf statement
|
||||||
|
//printf ("The surface area of a cylinder of radius %f and length %f is %f\n", rad, len, CalculateSurfaceAreaOfCylinder(r,l) );
|
||||||
|
|
||||||
|
// All done
|
||||||
|
return 0;
|
||||||
|
}
|
26
LC11/loop_into_array.c
Normal file
26
LC11/loop_into_array.c
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
|
||||||
|
/* A very simple program that defines an array of ten integers */
|
||||||
|
/* And puts a different value into each */
|
||||||
|
|
||||||
|
#include <stdio.h> /* Usual includes */
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
int main( void )
|
||||||
|
{
|
||||||
|
|
||||||
|
int Values[10]; /* Define an array of 10 integers */
|
||||||
|
int j; /* Define an integer 'j' */
|
||||||
|
|
||||||
|
/* Use a loop to put some values into the array, displaying the */
|
||||||
|
/* vaules stored on the screen */
|
||||||
|
|
||||||
|
for ( j = 0 ; j < 10 ; j++ )
|
||||||
|
{
|
||||||
|
Values[j] = j * 2;
|
||||||
|
printf("\nThe value in array index %d is %d ",j,Values[j]);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
return 0; /* End of the program */
|
||||||
|
|
||||||
|
}
|
28
LC12/global_ex1.c
Normal file
28
LC12/global_ex1.c
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
/*
|
||||||
|
This makes global variables look like a good idea
|
||||||
|
|
||||||
|
THEY ARE NOT - DO NOT USE THEM
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
int y,k,ans; /* Define GLOBAL variables */
|
||||||
|
|
||||||
|
void NastyGlobalFunction (void ) /* Define function */
|
||||||
|
{
|
||||||
|
ans = ( y * k ); /* y, k and ans are defined globally above */
|
||||||
|
return ;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main( void )
|
||||||
|
{
|
||||||
|
y = 2; /* Set value of y */
|
||||||
|
k = 3; /* Set value of k */
|
||||||
|
|
||||||
|
NastyGlobalFunction(); /* call the function */
|
||||||
|
|
||||||
|
printf("%d multiplied by %d is %d " ,y ,k ,ans ); /* Display values */
|
||||||
|
return 0;
|
||||||
|
}
|
37
LC12/global_ex2.c
Normal file
37
LC12/global_ex2.c
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
/*
|
||||||
|
In this example, we do not have a problem as although
|
||||||
|
we have a global variable 'i', we do not corrupt it in
|
||||||
|
the function
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
int i; /* Nasty global variable ! */
|
||||||
|
|
||||||
|
/*
|
||||||
|
This function returns the sum of the number 1->10
|
||||||
|
Note that although we use 'i' it is not defined in
|
||||||
|
the function its self.
|
||||||
|
*/
|
||||||
|
|
||||||
|
int SumToTen ( void )
|
||||||
|
{
|
||||||
|
|
||||||
|
int Result = 0;
|
||||||
|
|
||||||
|
for ( i = 10 ; i >= 0 ; i-- ) // Count down as more effecient
|
||||||
|
Result = Result + i;
|
||||||
|
|
||||||
|
return Result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* This is our main function */
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
printf ("\nThe sum of the values 0 to 10 is %d ",SumToTen() );
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
57
LC12/global_ex3.c
Normal file
57
LC12/global_ex3.c
Normal file
@ -0,0 +1,57 @@
|
|||||||
|
/*
|
||||||
|
|
||||||
|
This time we have a real problem. We are using the variable
|
||||||
|
'i' in the main loop to count and again in the function.
|
||||||
|
|
||||||
|
Thus the function will change the value seen by the 'main'
|
||||||
|
routine - and will cause *Serious* problems
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
int i; /* Again, the nasty global variable */
|
||||||
|
|
||||||
|
/*
|
||||||
|
|
||||||
|
This function returns the sum of the number 0 -> i, but
|
||||||
|
summing from the limit back to zero ( to prove a point ! )
|
||||||
|
|
||||||
|
The problem this will cause is that at the end of the routine
|
||||||
|
the value of 'i' will always be '1' which will stop the loop
|
||||||
|
in 'main' working !
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
int SumToValue ( int Limit )
|
||||||
|
{
|
||||||
|
|
||||||
|
int Result = 0;
|
||||||
|
|
||||||
|
printf("\nIn function ");
|
||||||
|
|
||||||
|
for ( i = Limit ; i > 0 ; i-- )
|
||||||
|
Result = Result + i;
|
||||||
|
|
||||||
|
return Result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
This is our main function - this time we are aiming to print out
|
||||||
|
at table of the sums of values from 1 to 10
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
int main( void )
|
||||||
|
{
|
||||||
|
|
||||||
|
for ( i = 0 ; i < 10 ; i++ )
|
||||||
|
{
|
||||||
|
printf ("\nThe sum of the values 0 to %d is %d ",i,SumToValue(i));
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
32
LC14/pointer_function_example_1.c
Normal file
32
LC14/pointer_function_example_1.c
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <math.h>
|
||||||
|
|
||||||
|
// As we are using the function BEFORE it is actually written we need to provide the
|
||||||
|
// prototype so that the compiler can verify we are calling it correctly
|
||||||
|
|
||||||
|
void CalculateArea ( double Radius, double *pArea); // note the ‘*’
|
||||||
|
|
||||||
|
// This is the main code for our application
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
double radius, area;
|
||||||
|
radius = 1.0;
|
||||||
|
CalculateArea (radius, &area);
|
||||||
|
|
||||||
|
printf ("The area of circle of radius %f is %f\n", radius, area);
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// And here is our function
|
||||||
|
|
||||||
|
// Note: Pi is written out simply to match the notes, M_PI could also be used
|
||||||
|
|
||||||
|
void CalculateArea ( double Radius, double *pArea )
|
||||||
|
{
|
||||||
|
*pArea = 3.14159265 * Radius * Radius;
|
||||||
|
return;
|
||||||
|
}
|
31
LC14/pointer_function_example_2.c
Normal file
31
LC14/pointer_function_example_2.c
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <math.h>
|
||||||
|
|
||||||
|
// As we are using the function BEFORE it is actually written we need to provide the
|
||||||
|
// prototype so that the compiler can verify we are calling it correctly
|
||||||
|
|
||||||
|
void CalculateVolumeAndSA ( double Radius, double Length, double *Volume, double *SurfaceArea); // note the ‘*’
|
||||||
|
|
||||||
|
// This is the main code for our application
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
double radius = 3.4, length = 7.3, volume, SurfaceArea;
|
||||||
|
|
||||||
|
CalculateVolumeAndSA(radius, length, &volume, &SurfaceArea);
|
||||||
|
|
||||||
|
printf("The volume is %f \n", volume);
|
||||||
|
printf( "The surface area is %f\n", SurfaceArea);
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// And here is our function
|
||||||
|
|
||||||
|
void CalculateVolumeAndSA ( double Radius, double Length, double *Volume, double *SurfaceArea) // note the ‘*’
|
||||||
|
{
|
||||||
|
*Volume = M_PI * Radius * Radius * Length;
|
||||||
|
*SurfaceArea = ( 2 * M_PI * Radius * Radius * Length ) +( 2 * M_PI * Radius * Length );
|
||||||
|
}
|
33
LC15/pointer_to_array_1.c
Normal file
33
LC15/pointer_to_array_1.c
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
int main(void) // Main : Execution starts here...
|
||||||
|
{
|
||||||
|
// Define variables - pre-populate the array
|
||||||
|
int MyArray[10] = {12,34,23,11,8,19,6,44,9,16};
|
||||||
|
int *pArray = &MyArray[0];
|
||||||
|
int i;
|
||||||
|
|
||||||
|
// Direct from the array
|
||||||
|
printf ("Directly from the array\n");
|
||||||
|
for ( i = 0 ; i < 10 ; i++ )
|
||||||
|
printf ("Item %d contains value %d\n",i,MyArray[i]);
|
||||||
|
|
||||||
|
|
||||||
|
// Pointer approach 1
|
||||||
|
printf ("Pointer method 1\n");
|
||||||
|
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;
|
||||||
|
}
|
33
LC15/pointer_to_array_2.c
Normal file
33
LC15/pointer_to_array_2.c
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
int main(void) // Main : Execution starts here...
|
||||||
|
{
|
||||||
|
// Define variables - pre-populate the array
|
||||||
|
int MyArray[10] = {12,34,23,11,8,19,6,44,9,16};
|
||||||
|
int *pArray = &MyArray[0];
|
||||||
|
int i;
|
||||||
|
|
||||||
|
// Direct from the array
|
||||||
|
printf ("Directly from the array\n");
|
||||||
|
for ( i = 0 ; i < 10 ; i++ )
|
||||||
|
printf ("Item %d contains value %d\n",i,MyArray[i]);
|
||||||
|
|
||||||
|
|
||||||
|
// Pointer approach 1
|
||||||
|
printf ("Pointer method 1\n");
|
||||||
|
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;
|
||||||
|
}
|
33
LC15/pointer_to_array_examples.c
Normal file
33
LC15/pointer_to_array_examples.c
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
int main(void) // Main : Execution starts here...
|
||||||
|
{
|
||||||
|
// Define variables - pre-populate the array
|
||||||
|
int MyArray[10] = {12,34,23,11,8,19,6,44,9,16};
|
||||||
|
int *pArray = &MyArray[0];
|
||||||
|
int i;
|
||||||
|
|
||||||
|
// Direct from the array
|
||||||
|
printf ("Directly from the array\n");
|
||||||
|
for ( i = 0 ; i < 10 ; i++ )
|
||||||
|
printf ("Item %d contains value %d\n",i,MyArray[i]);
|
||||||
|
|
||||||
|
|
||||||
|
// Pointer approach 1
|
||||||
|
printf ("Pointer method 1\n");
|
||||||
|
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;
|
||||||
|
}
|
46
LC16/Dynamic1.c
Normal file
46
LC16/Dynamic1.c
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
/* An example of
|
||||||
|
|
||||||
|
Creating dynamically an array
|
||||||
|
Populating the array
|
||||||
|
Displaying the contents
|
||||||
|
Freeing up the memory
|
||||||
|
|
||||||
|
Example 1 : No sucessful allocation checking
|
||||||
|
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
int *ipArray = NULL; /* Create the pointer and set */
|
||||||
|
/* to null to start with */
|
||||||
|
int iSize = 0; /* Define our 'size' variable */
|
||||||
|
|
||||||
|
int i; /* A Loop variables */
|
||||||
|
|
||||||
|
/* Prompt for array size */
|
||||||
|
|
||||||
|
printf("\nHow big is the array to be ? ");
|
||||||
|
scanf("%d",&iSize);
|
||||||
|
|
||||||
|
/* Allocate the memory */
|
||||||
|
ipArray = (int *)calloc(iSize, sizeof(int));
|
||||||
|
|
||||||
|
/* Populate the array (Method 1) */
|
||||||
|
for ( i = 0 ; i < iSize ; i++ )
|
||||||
|
ipArray[i] = iSize - i;
|
||||||
|
|
||||||
|
/* display the data */
|
||||||
|
for ( i = 0 ; i < iSize ; i++ )
|
||||||
|
printf("Value %d is %d\n",i,ipArray[i] );
|
||||||
|
|
||||||
|
/* free memory */
|
||||||
|
free(ipArray);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
54
LC16/Dynamic2.c
Normal file
54
LC16/Dynamic2.c
Normal file
@ -0,0 +1,54 @@
|
|||||||
|
/* An example of
|
||||||
|
|
||||||
|
Creating dynamically an array
|
||||||
|
Populating the array
|
||||||
|
Displaying the contents
|
||||||
|
Freeing up the memory
|
||||||
|
|
||||||
|
Version 2 : Check memory has been assigned
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
int *ipArray = NULL; /* Create the pointer and set */
|
||||||
|
/* to null to start with */
|
||||||
|
int iSize = 0; /* Define our 'size' variable */
|
||||||
|
|
||||||
|
int i; /* A Loop variables */
|
||||||
|
|
||||||
|
/* Prompt for array size */
|
||||||
|
|
||||||
|
printf("\nHow big is the array to be ? ");
|
||||||
|
scanf("%d",&iSize);
|
||||||
|
|
||||||
|
/* Allocate the memory */
|
||||||
|
ipArray = (int *)calloc(iSize, sizeof(int));
|
||||||
|
|
||||||
|
if ( ipArray == NULL )
|
||||||
|
{
|
||||||
|
printf("\nUnable to allocate the memory requested");
|
||||||
|
printf("\n ** Program terminating ** \n");
|
||||||
|
exit (1);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Populate the array (Method 1) */
|
||||||
|
for ( i = 0 ; i < iSize ; i++ )
|
||||||
|
ipArray[i] = iSize - i;
|
||||||
|
|
||||||
|
/* display the data */
|
||||||
|
for ( i = 0 ; i < iSize ; i++ )
|
||||||
|
printf("Value %d is %d\n",i,ipArray[i] );
|
||||||
|
|
||||||
|
/* free memory */
|
||||||
|
free(ipArray);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
66
LC16/Dynamic3.c
Normal file
66
LC16/Dynamic3.c
Normal file
@ -0,0 +1,66 @@
|
|||||||
|
/* An example of
|
||||||
|
|
||||||
|
Creating dynamically an array
|
||||||
|
Populating the array
|
||||||
|
Displaying the contents
|
||||||
|
Freeing up the memory
|
||||||
|
|
||||||
|
Version 3 : Read and Write values using pointers
|
||||||
|
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
int *ipArray = NULL; /* Create the pointer and set */
|
||||||
|
/* to null to start with */
|
||||||
|
|
||||||
|
int *ipStartValue = NULL; /* A place to store ipArray's */
|
||||||
|
/* initial value ,ie ipArray[0] */
|
||||||
|
int iSize = 0; /* Define our 'size' variable */
|
||||||
|
|
||||||
|
int i; /* A Loop variables */
|
||||||
|
|
||||||
|
/* Prompt for array size */
|
||||||
|
|
||||||
|
printf("\nHow big is the array to be ? ");
|
||||||
|
scanf("%d",&iSize);
|
||||||
|
|
||||||
|
/* Allocate the memory */
|
||||||
|
ipArray = (int *)calloc(iSize, sizeof(int));
|
||||||
|
|
||||||
|
if ( ipArray == NULL )
|
||||||
|
{
|
||||||
|
printf("\nUnable to allocate the memory requested");
|
||||||
|
printf("\n ** Program terminating ** \n");
|
||||||
|
exit (1);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Store the base memory address for use later */
|
||||||
|
ipStartValue = ipArray;
|
||||||
|
|
||||||
|
/* 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;
|
||||||
|
|
||||||
|
/* display the data */
|
||||||
|
for ( i = 0 ; i < iSize ; i++ )
|
||||||
|
printf("Value %d is %d\n",i,*ipArray++ );
|
||||||
|
|
||||||
|
/* free memory : Again, reset ipArray to its origin */
|
||||||
|
|
||||||
|
ipArray = ipStartValue;
|
||||||
|
free(ipArray);
|
||||||
|
|
||||||
|
/* The above two lines could be replaced with free (ipStartValue) */
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
79
LC17/DynamicFunction.c
Normal file
79
LC17/DynamicFunction.c
Normal file
@ -0,0 +1,79 @@
|
|||||||
|
/* An example of
|
||||||
|
|
||||||
|
Creating dynamically an array
|
||||||
|
Populating the array
|
||||||
|
Displaying the contents
|
||||||
|
Freeing up the memory
|
||||||
|
|
||||||
|
Version 4 : Check memory has been assigned
|
||||||
|
Read and Write values using pointers
|
||||||
|
Pass array data to a function
|
||||||
|
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
|
||||||
|
int Sum ( int *Data, int n ) /* Define our function */
|
||||||
|
{
|
||||||
|
int Sum = 0,i; /* Define working variables */
|
||||||
|
|
||||||
|
for ( i = 0 ; i < n ; i++ )
|
||||||
|
{
|
||||||
|
printf("Item %d=%d\n", i, Data[i]);
|
||||||
|
Sum += Data[i]; /* This means Sum = Sum + Data[i] */
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
return Sum; /* Return the value */
|
||||||
|
|
||||||
|
}
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
int *ipArray = NULL; /* Create the pointer and set */
|
||||||
|
/* to null to start with */
|
||||||
|
|
||||||
|
int *ipStartValue = NULL; /* A place to store ipArray's */
|
||||||
|
/* initial value ,ie ipArray[0] */
|
||||||
|
int iSize = 0; /* Define our 'size' variable */
|
||||||
|
|
||||||
|
int i; /* A Loop variables */
|
||||||
|
|
||||||
|
/* Prompt for array size */
|
||||||
|
|
||||||
|
printf("\nHow big is the array to be ? ");
|
||||||
|
scanf("%d",&iSize);
|
||||||
|
|
||||||
|
/* Allocate the memory */
|
||||||
|
ipArray = (int *)calloc(iSize, sizeof(int));
|
||||||
|
|
||||||
|
if ( ipArray == NULL )
|
||||||
|
{
|
||||||
|
printf("\nUnable to allocate the memory requested");
|
||||||
|
printf("\n ** Program terminating ** \n");
|
||||||
|
exit (1);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Store the base memory address for use later */
|
||||||
|
ipStartValue = ipArray;
|
||||||
|
|
||||||
|
/* Populate the array (Method 2 - Use Pointers: This is much faster !) */
|
||||||
|
for ( i = 0 ; i < iSize ; i++ )
|
||||||
|
ipArray[i] = i;
|
||||||
|
|
||||||
|
/* Display the sum of the values in the array */
|
||||||
|
|
||||||
|
printf("\nThe sum of the array values is %d ",Sum(ipArray,iSize) );
|
||||||
|
|
||||||
|
/* free memory : Again, reset ipArray to its origin */
|
||||||
|
|
||||||
|
ipArray = ipStartValue;
|
||||||
|
free(ipArray);
|
||||||
|
|
||||||
|
/* The above two lines could be replaced with free (ipStartValue) */
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
61
LC17/Quadratic_with_Pointers.c
Normal file
61
LC17/Quadratic_with_Pointers.c
Normal file
@ -0,0 +1,61 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <math.h>
|
||||||
|
|
||||||
|
|
||||||
|
// This function will return
|
||||||
|
//
|
||||||
|
// 0 if the equation can be solved,
|
||||||
|
// -1 if it is not a quadratic
|
||||||
|
// -2 if it is complex
|
||||||
|
|
||||||
|
int SolveQuadratic ( int a, int b, int c, float *x1, float *x2)
|
||||||
|
{
|
||||||
|
int d;
|
||||||
|
|
||||||
|
// If the equations is not a quadratic return -1
|
||||||
|
if ( a == 0 )
|
||||||
|
return (-1);
|
||||||
|
|
||||||
|
// Calculate the value that will be square rooted
|
||||||
|
d = (b*b) - (4*a*c);
|
||||||
|
|
||||||
|
// If less than zero then it would be complex - give up!
|
||||||
|
if ( d < 0 )
|
||||||
|
return (-2);
|
||||||
|
|
||||||
|
// If we got to here we are OK to solve things
|
||||||
|
*x1 = (-b - sqrt(d)) / (2*a);
|
||||||
|
*x2 = (-b + sqrt(d)) / (2*a);
|
||||||
|
|
||||||
|
// This is are 'OK' return value
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
int A,B,C, r;
|
||||||
|
float X1, X2;
|
||||||
|
|
||||||
|
printf ("Please enter a b & c separated by a space: ");
|
||||||
|
|
||||||
|
scanf("%d %d %d",&A, &B, &C);
|
||||||
|
|
||||||
|
r = SolveQuadratic(A,B,C, &X1, &X2);
|
||||||
|
|
||||||
|
// The value returned lets us know if we have values in X1 and X2 we can use
|
||||||
|
switch (r)
|
||||||
|
{
|
||||||
|
case 0 :
|
||||||
|
printf ("The solutions are %f and %f", X1, X2 );
|
||||||
|
break;
|
||||||
|
case -1 :
|
||||||
|
printf ("The equation provided was not a quadratic" );
|
||||||
|
break;
|
||||||
|
case -2 :
|
||||||
|
printf ("The solutions had complex roots and could not be solved");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
45
LC18/filemove.c
Normal file
45
LC18/filemove.c
Normal file
@ -0,0 +1,45 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
/* Example 3 - jumping & getting element count */
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
|
||||||
|
int ValRead;
|
||||||
|
int Item;
|
||||||
|
int Locn;
|
||||||
|
FILE *fptr;
|
||||||
|
/* Open the file from example 1 */
|
||||||
|
|
||||||
|
fptr = fopen ("data.dat","rb");
|
||||||
|
if ( fptr == NULL )
|
||||||
|
{
|
||||||
|
printf ("\nError opening input file - aborting ");
|
||||||
|
exit (0);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Ask the user which value to jump to */
|
||||||
|
printf ("\nWhich value do you wish to view ? ");
|
||||||
|
scanf ("%d",&Item);
|
||||||
|
|
||||||
|
/* Jump to this item */
|
||||||
|
/* notice we move in steps of item size */
|
||||||
|
|
||||||
|
Locn = Item * sizeof(int);
|
||||||
|
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);
|
||||||
|
|
||||||
|
/* And close */
|
||||||
|
fclose (fptr);
|
||||||
|
|
||||||
|
/* All Done */
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
38
LC18/filesize.c
Normal file
38
LC18/filesize.c
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
/* Example 3 - jumping & getting element count */
|
||||||
|
|
||||||
|
main()
|
||||||
|
{
|
||||||
|
|
||||||
|
long FileBytes;
|
||||||
|
FILE *fptr;
|
||||||
|
/* Open the file from example 1 */
|
||||||
|
|
||||||
|
fptr = fopen ("data.dat","rb");
|
||||||
|
if ( fptr == NULL )
|
||||||
|
{
|
||||||
|
printf ("\nError opening input file - aborting ");
|
||||||
|
exit (0);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Move to the end of file */
|
||||||
|
fseek (fptr, 0, SEEK_END);
|
||||||
|
|
||||||
|
/* Get the byte size */
|
||||||
|
FileBytes = ftell(fptr);
|
||||||
|
|
||||||
|
/* Convert to size based on fact all int's */
|
||||||
|
FileBytes = FileBytes / sizeof(int);
|
||||||
|
|
||||||
|
/* Display the read value */
|
||||||
|
printf ("No. of items in file is %d\n",FileBytes);
|
||||||
|
|
||||||
|
/* And close */
|
||||||
|
fclose (fptr);
|
||||||
|
|
||||||
|
/* All Done */
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
25
LC19/ConstHashDefine.c
Normal file
25
LC19/ConstHashDefine.c
Normal 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
45
LC19/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
LC19/Structure_1.c
Normal file
27
LC19/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
LC19/Structure_2.c
Normal file
39
LC19/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 : 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
59
LC19/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 ",MyPoints[i].x);
|
||||||
|
printf("\t Y = %d ",MyPoints[i].y);
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
77
LC19/WriteHeader.c
Normal file
77
LC19/WriteHeader.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(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;
|
||||||
|
}
|
90
LC19/WriteHeaderArrayAndUpdate.c
Normal file
90
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
LC19/WriteHeaderArrayAndUpdateAndSeek.c
Normal file
99
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
LC19/WriteHeaderWithArray.c
Normal file
77
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;
|
||||||
|
}
|
24
LC19/enum.c
Normal file
24
LC19/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;
|
||||||
|
}
|
41
LC19/enumdefine.c
Normal file
41
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;
|
||||||
|
}
|
90
LC19/file_header.c
Normal file
90
LC19/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.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
99
LC19/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;
|
||||||
|
}
|
72
LC19/swap.c
Normal file
72
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
LC19/union.c
Normal file
31
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;
|
||||||
|
|
||||||
|
}
|
37
LC21/cond.c
Normal file
37
LC21/cond.c
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
/* We define one thing - we need not give it a value, but we can */
|
||||||
|
#define BUILD1
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
|
||||||
|
|
||||||
|
printf ("\nThis code is common to all ");
|
||||||
|
|
||||||
|
/* We check for the existance ob 'BUILD1' and act accordingly */
|
||||||
|
|
||||||
|
#ifdef BUILD1
|
||||||
|
|
||||||
|
printf ("\nWe had BUILD1 defined ");
|
||||||
|
|
||||||
|
#else
|
||||||
|
|
||||||
|
printf ("\nBUILD1 was not defined ");
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* Can can see if somthing was not defined too */
|
||||||
|
|
||||||
|
#ifndef BUILD2
|
||||||
|
|
||||||
|
printf ("\nBUILD2 was *NOT* defined ");
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
printf ("\n\n");
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
}
|
24
LC21/getthem.c
Normal file
24
LC21/getthem.c
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
int main(int argc, char *argv[])
|
||||||
|
{
|
||||||
|
int age;
|
||||||
|
char name[10];
|
||||||
|
|
||||||
|
/* we check the argument count is 3 ( prog name plus 2 params ) */
|
||||||
|
if ( argc != 3 )
|
||||||
|
{
|
||||||
|
printf ("\nProgram use %s name age ",argv[0]);
|
||||||
|
exit (0);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Copy the command parameters into suitable variables */
|
||||||
|
sscanf(argv[1],"%s",name);
|
||||||
|
sscanf(argv[2],"%d",&age);
|
||||||
|
|
||||||
|
/* And display for all to see */
|
||||||
|
printf ("\nHello %s, you are %d ",name,age);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
19
LC21/simple.c
Normal file
19
LC21/simple.c
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
/* Note the new type of main */
|
||||||
|
|
||||||
|
int main(int argc, char *argv[])
|
||||||
|
{
|
||||||
|
int x;
|
||||||
|
|
||||||
|
/* Print the argument count */
|
||||||
|
printf("Arguments -> %d\n",argc);
|
||||||
|
|
||||||
|
/* And the arguments themselves */
|
||||||
|
for (x=0; x<argc; x++)
|
||||||
|
printf("%s\n",argv[x]);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
23
LC21/sprints.c
Normal file
23
LC21/sprints.c
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
char buffer[100];
|
||||||
|
int age=6;
|
||||||
|
char name[6];
|
||||||
|
|
||||||
|
/* This command copies "James" into the character array 'Name' */
|
||||||
|
|
||||||
|
strcpy(name,"James");
|
||||||
|
|
||||||
|
/* Print to the string (rather than screen) */
|
||||||
|
|
||||||
|
sprintf(buffer,"Name: %s, Age %d ",name,age);
|
||||||
|
|
||||||
|
/* And output the string created */
|
||||||
|
printf("\n->%s\n",buffer);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
29
LC21/use.c
Normal file
29
LC21/use.c
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
#define BUILD1 23
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
|
||||||
|
int MyValue = 0;
|
||||||
|
|
||||||
|
printf ("\nThis code is common to all ");
|
||||||
|
|
||||||
|
#ifdef BUILD1
|
||||||
|
|
||||||
|
printf ("\nWe had BUILD1 defined, will adjust value accordingly ");
|
||||||
|
MyValue = BUILD1;
|
||||||
|
|
||||||
|
#else
|
||||||
|
|
||||||
|
printf ("\nBUILD1 was not defined, leaving MyValue alone ");
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
printf ("\nThe value in MyValue is %d ",MyValue);
|
||||||
|
printf ("\n\n");
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
}
|
9
LC4/hello_world.c
Normal file
9
LC4/hello_world.c
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
int main( void )
|
||||||
|
{
|
||||||
|
/* My first program in C */
|
||||||
|
printf ("Hello World \n");
|
||||||
|
return 0; // Return from prog
|
||||||
|
}
|
BIN
LC4/hello_world.exe
Normal file
BIN
LC4/hello_world.exe
Normal file
Binary file not shown.
14
LC5/printf_example.c
Normal file
14
LC5/printf_example.c
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
int a,b,c,sum; /* Define variables */
|
||||||
|
a = 1; /* Assign values */
|
||||||
|
b = 2;
|
||||||
|
c = 3;
|
||||||
|
sum = a + b + c ; /* Calculate sum & Display */
|
||||||
|
|
||||||
|
printf ("\nThe sum of %d + %d + %d is %d \n", a, b, c, sum);
|
||||||
|
|
||||||
|
return 0; /* Return from prog */
|
||||||
|
}
|
BIN
LC5/printf_example.exe
Normal file
BIN
LC5/printf_example.exe
Normal file
Binary file not shown.
22
LC7/fgets_string.c
Normal file
22
LC7/fgets_string.c
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
/* EEEE1001/H61CAE*/
|
||||||
|
|
||||||
|
/* A program to show how to use fgets to safely read in a string */
|
||||||
|
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
|
||||||
|
char MyName[50]; /* Define a string of 10 characters */
|
||||||
|
|
||||||
|
|
||||||
|
/* A string */
|
||||||
|
|
||||||
|
printf("\nPlease enter your name & press return ");
|
||||||
|
fgets(MyName, 80, stdin);
|
||||||
|
printf("\nHello %s ",MyName);
|
||||||
|
|
||||||
|
return 0; /* End of the program */
|
||||||
|
|
||||||
|
}
|
27
LC7/getch.c
Normal file
27
LC7/getch.c
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
/*
|
||||||
|
Chapter 7: example of getch
|
||||||
|
|
||||||
|
This program reads a single keystroke and displays it
|
||||||
|
on the screen
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <stdio.h> /* Standard include files */
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <conio.h>
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
|
||||||
|
char x; /* Define a variable of type char */
|
||||||
|
|
||||||
|
x = getch(); /* Use getchar to read a keypress and store the */
|
||||||
|
/* result in 'x' */
|
||||||
|
|
||||||
|
putchar(x); /* Display the character stored in 'x' on the */
|
||||||
|
/* screen using putchar */
|
||||||
|
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
}
|
27
LC7/getchar.c
Normal file
27
LC7/getchar.c
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
/*
|
||||||
|
Chapter 7: example of getchar
|
||||||
|
|
||||||
|
This program reads a single keystroke and displays it
|
||||||
|
on the screen
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <stdio.h> /* Standard include files */
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
|
||||||
|
char x; /* Define a variable of type char */
|
||||||
|
|
||||||
|
x = getchar(); /* Use getchar to read a keypress and store the */
|
||||||
|
/* result in 'x' */
|
||||||
|
|
||||||
|
putchar(x); /* Display the character stored in 'x' on the */
|
||||||
|
/* screen using putchar */
|
||||||
|
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
}
|
22
LC7/gets_string.c
Normal file
22
LC7/gets_string.c
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
/* EEEE1001/H61CAE - Chapter 7 */
|
||||||
|
|
||||||
|
/* A program to show how to use gets to read a string (that can include spaces) */
|
||||||
|
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
int main( void )
|
||||||
|
{
|
||||||
|
|
||||||
|
char MyName[50]; /* Define a string of 10 characters */
|
||||||
|
|
||||||
|
|
||||||
|
/* A string */
|
||||||
|
|
||||||
|
printf("\nPlease enter your name & press return ");
|
||||||
|
gets(MyName);
|
||||||
|
printf("\nHello %s ",MyName);
|
||||||
|
|
||||||
|
return 0; /* End of the program */
|
||||||
|
|
||||||
|
}
|
36
LC7/scanf_examples.c
Normal file
36
LC7/scanf_examples.c
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
/* Chapter 7 : Examples of using scanf */
|
||||||
|
|
||||||
|
/* A program to show how to use scanf to read various types of values (float/integer)*/
|
||||||
|
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
|
||||||
|
int main( void )
|
||||||
|
{
|
||||||
|
|
||||||
|
int v1; /* An integer */
|
||||||
|
float fv; /* A float */
|
||||||
|
|
||||||
|
|
||||||
|
/* Reading an integer */
|
||||||
|
printf("\nPlease enter an integer and press return ");
|
||||||
|
scanf("%d",&v1);
|
||||||
|
printf("\nThe value you entered was %d ",v1);
|
||||||
|
|
||||||
|
/* Reading a floating point number */
|
||||||
|
printf("\nPlease enter a floating point number & press return ");
|
||||||
|
scanf("%f",&fv);
|
||||||
|
printf("\nThe value you typed in was %f ",fv);
|
||||||
|
|
||||||
|
|
||||||
|
/* Reading all three in one go */
|
||||||
|
printf("\nPlease enter your age and salary\n");
|
||||||
|
scanf("%d %f", &v1, &fv);
|
||||||
|
|
||||||
|
printf("\nYou are %d years old ",v1);
|
||||||
|
printf("\nAnd earn %f per year ",fv);
|
||||||
|
|
||||||
|
return 0; /* End of the program */
|
||||||
|
|
||||||
|
}
|
23
LC7/scanf_string.c
Normal file
23
LC7/scanf_string.c
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
/* EEEE1001/H61CAE - Chapter 7 */
|
||||||
|
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
|
||||||
|
char MyName[50]; /* Define a string of 50 characters */
|
||||||
|
|
||||||
|
|
||||||
|
/* Prompt for and read in a string */
|
||||||
|
printf("\nPlease enter your name & press return ");
|
||||||
|
scanf("%s",MyName);
|
||||||
|
|
||||||
|
/* Display fixed text and string on the screen */
|
||||||
|
printf("\nHello %s ",MyName);
|
||||||
|
|
||||||
|
|
||||||
|
return 0; /* End of the program */
|
||||||
|
|
||||||
|
}
|
35
LC8/complex_if.c
Normal file
35
LC8/complex_if.c
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
/* EEEE1001/H61CAE Chapter 8 */
|
||||||
|
|
||||||
|
/* Example of a simple if statements */
|
||||||
|
|
||||||
|
|
||||||
|
#include <stdio.h> /* Standard include files */
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <conio.h>
|
||||||
|
|
||||||
|
|
||||||
|
int main( void )
|
||||||
|
{
|
||||||
|
|
||||||
|
int a = 1, b = 6; // Define and intialise variables
|
||||||
|
|
||||||
|
|
||||||
|
if ( (a == 1 ) && ( b == 6 ) )
|
||||||
|
{
|
||||||
|
printf("a is 1 AND b is 6\n" );
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( (a == 1 ) || ( b == 99 ) )
|
||||||
|
{
|
||||||
|
printf("a is 1 OR b is 99\n" );
|
||||||
|
}
|
||||||
|
|
||||||
|
if (( b < a ) || ( a == 7 ) )
|
||||||
|
{
|
||||||
|
printf ("The value of b is less than a OR a is 7\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
}
|
35
LC8/if_else_if_else.c
Normal file
35
LC8/if_else_if_else.c
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
/* Chapter 8 */
|
||||||
|
|
||||||
|
/* Example of a simple if statements */
|
||||||
|
|
||||||
|
|
||||||
|
#include <stdio.h> /* Standard include files */
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <conio.h>
|
||||||
|
|
||||||
|
|
||||||
|
int main( void )
|
||||||
|
{
|
||||||
|
|
||||||
|
int x = 55; // Define and intialise variable
|
||||||
|
|
||||||
|
|
||||||
|
if ( x == 2 )
|
||||||
|
{
|
||||||
|
printf ("The value of x was 2\n");
|
||||||
|
printf ("I will now do something\n");
|
||||||
|
}
|
||||||
|
else if ( x == 3 )
|
||||||
|
{
|
||||||
|
printf ("The value of x was 3\n");
|
||||||
|
printf ("I will now do something else\n");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
printf ("x is neither 2 or 3; I will do this instead!\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
}
|
37
LC8/if_equals_example.c
Normal file
37
LC8/if_equals_example.c
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
/* EEEE1001/H61CAE Chapter 8 */
|
||||||
|
|
||||||
|
/* Incorrect use of a single equals sign in an if statement */
|
||||||
|
|
||||||
|
|
||||||
|
#include <stdio.h> /* Standard include files */
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <conio.h>
|
||||||
|
|
||||||
|
|
||||||
|
int main( void )
|
||||||
|
{
|
||||||
|
|
||||||
|
int c = 6; // Define variable as initialise with value
|
||||||
|
|
||||||
|
printf ("\nThe current value in c is %d\n",c); // Display value in c
|
||||||
|
|
||||||
|
// This is correct
|
||||||
|
if ( c == 1 )
|
||||||
|
{
|
||||||
|
printf("\nThe value of c is 1\n" );
|
||||||
|
}
|
||||||
|
|
||||||
|
printf ("\nThe current value in c is %d\n",c); // Display value in c
|
||||||
|
|
||||||
|
// This is WRONG - it is setting c to one, this value is then tested
|
||||||
|
// and, as non-zero, is considered to be TRUE
|
||||||
|
if ( c = 1 )
|
||||||
|
{
|
||||||
|
printf("\nThe value of c is 1\n" );
|
||||||
|
}
|
||||||
|
|
||||||
|
printf ("\nThe current value in c is %d\n",c); // Display value in c
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
}
|
34
LC8/if_examples.c
Normal file
34
LC8/if_examples.c
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
/* Chapter 8 */
|
||||||
|
|
||||||
|
/* Example of a simple if statements */
|
||||||
|
|
||||||
|
|
||||||
|
#include <stdio.h> /* Standard include files */
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <conio.h>
|
||||||
|
|
||||||
|
|
||||||
|
int main( void )
|
||||||
|
{
|
||||||
|
|
||||||
|
int a = 1, b = 6; // Define and intialise variables
|
||||||
|
|
||||||
|
|
||||||
|
if ( a == 1 )
|
||||||
|
{
|
||||||
|
printf("\nThe value of a is 1\n" );
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( a < b )
|
||||||
|
{
|
||||||
|
printf ("The value of a is less than b\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( b < a )
|
||||||
|
{
|
||||||
|
printf ("The value of b is less than a\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
}
|
31
LC8/switch_1.c
Normal file
31
LC8/switch_1.c
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
/* EEEE1001/H61CAE Chapter 8 */
|
||||||
|
|
||||||
|
/* Example of a simple if statements */
|
||||||
|
|
||||||
|
|
||||||
|
#include <stdio.h> /* Standard include files */
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
int main( void )
|
||||||
|
{
|
||||||
|
|
||||||
|
int a; /* Define an int */
|
||||||
|
scanf ("%d",&a); /* Get value */
|
||||||
|
switch (a) /* Start of switch */
|
||||||
|
{
|
||||||
|
case 1:
|
||||||
|
printf("Hi"); /* Case 1 */
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 2:
|
||||||
|
printf("Bye"); /* Case 2 */
|
||||||
|
break;
|
||||||
|
|
||||||
|
default :
|
||||||
|
printf("Err"); /* Default */
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
}
|
34
LC8/switch_2.c
Normal file
34
LC8/switch_2.c
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
/* Chapter 8 */
|
||||||
|
|
||||||
|
/* Example of a simple if statements */
|
||||||
|
|
||||||
|
|
||||||
|
#include <stdio.h> /* Standard include files */
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
#include <conio.h> /* Required for getch() */
|
||||||
|
|
||||||
|
int main( void )
|
||||||
|
{
|
||||||
|
|
||||||
|
char c; /* Define an char */
|
||||||
|
c = getch(); /* Get value - no return required*/
|
||||||
|
|
||||||
|
switch (c) /* Start of switch */
|
||||||
|
{
|
||||||
|
case '1':
|
||||||
|
printf("Hi"); /* Case 1 */
|
||||||
|
break;
|
||||||
|
|
||||||
|
case '2':
|
||||||
|
printf("Bye"); /* Case 2 */
|
||||||
|
break;
|
||||||
|
|
||||||
|
default :
|
||||||
|
printf("Err"); /* Default */
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
}
|
33
LC8/switch_3.c
Normal file
33
LC8/switch_3.c
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
/* EEEE1001/H61CAE Chapter 8 */
|
||||||
|
|
||||||
|
/* Example of a simple if statements */
|
||||||
|
|
||||||
|
|
||||||
|
#include <stdio.h> /* Standard include files */
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
#include <conio.h> /* Required for getch() */
|
||||||
|
|
||||||
|
int main( void )
|
||||||
|
{
|
||||||
|
|
||||||
|
char c; /* Define an char */
|
||||||
|
c = getch(); /* Get value - no return required*/
|
||||||
|
|
||||||
|
switch (c) /* Start of switch */
|
||||||
|
{
|
||||||
|
case '1':
|
||||||
|
printf("Hi"); /* Case 1 */
|
||||||
|
|
||||||
|
case '2':
|
||||||
|
printf("Bye"); /* Case 2 */
|
||||||
|
break;
|
||||||
|
|
||||||
|
default :
|
||||||
|
printf("Err"); /* Default */
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
}
|
42
LC9/do_while_example.c
Normal file
42
LC9/do_while_example.c
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
/*
|
||||||
|
This program shows how to use a DO/WHILE statement to keep displaying
|
||||||
|
the key pressed on the screen. It terminates when the 'q' key is
|
||||||
|
pressed
|
||||||
|
|
||||||
|
James Bonnyman
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <stdio.h> /* Standard include files */
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <conio.h>
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
|
||||||
|
char x; /* Define a variable of type char */
|
||||||
|
|
||||||
|
|
||||||
|
/* We now use the while statement. The 'loop' continues to execute */
|
||||||
|
/* until such time as the expression in the brackets becomes false */
|
||||||
|
|
||||||
|
/* we use != , meaning 'Not Equal to' as the test, as we wish the */
|
||||||
|
/* the statements to be executed every time we press a key other */
|
||||||
|
/* than 'q'. Once we press 'q' the code continues on to the next */
|
||||||
|
/* statement */
|
||||||
|
|
||||||
|
/* Note: This time the getch is in the while expression */
|
||||||
|
|
||||||
|
|
||||||
|
do
|
||||||
|
{
|
||||||
|
x = getch();
|
||||||
|
/* Display a message on the screen */
|
||||||
|
printf("\nThe key you pressed was the %c key",x );
|
||||||
|
}
|
||||||
|
while ( x != 'q' );
|
||||||
|
|
||||||
|
// All done :-)
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
}
|
42
LC9/for_loops.c
Normal file
42
LC9/for_loops.c
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
/* Example Program */
|
||||||
|
|
||||||
|
/*
|
||||||
|
This program shows how to write a simple loop that counts up
|
||||||
|
from zero to 9.
|
||||||
|
|
||||||
|
A second loop then counts from 10 down to 1
|
||||||
|
|
||||||
|
Remember
|
||||||
|
|
||||||
|
j++ means j = j + 1
|
||||||
|
j-- means j = j - 1
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <stdio.h> /* Standard include files */
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
|
||||||
|
char j; /* Define a variable of type integer */
|
||||||
|
|
||||||
|
|
||||||
|
/* The count up loop */
|
||||||
|
printf("\nCounting Up ... ");
|
||||||
|
|
||||||
|
for ( j = 0 ; j < 10 ; j++ )
|
||||||
|
printf("\nThe value of j is %d",j );
|
||||||
|
|
||||||
|
|
||||||
|
/* The count down loop */
|
||||||
|
|
||||||
|
printf ("\nCounting Down .. ");
|
||||||
|
for ( j = 10 ; j > 0 ; j-- )
|
||||||
|
printf("\nThe value of j is %d",j );
|
||||||
|
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
}
|
47
LC9/while_example_version_1.c
Normal file
47
LC9/while_example_version_1.c
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
/*
|
||||||
|
This program shows how to use a WHILE statment to keep displaying
|
||||||
|
the key pressed on the screen. It terminates when the 'q' key is
|
||||||
|
pressed
|
||||||
|
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <stdio.h> /* Standard include files */
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <conio.h>
|
||||||
|
|
||||||
|
|
||||||
|
int main( void )
|
||||||
|
{
|
||||||
|
|
||||||
|
char x; /* Define a variable of type char */
|
||||||
|
|
||||||
|
printf("Press a key ");
|
||||||
|
x = getch(); /* Use getch to read a keypress and store the */
|
||||||
|
/* result in 'x'. We do this initially so that */
|
||||||
|
/* there is a value in 'x' for the while statment */
|
||||||
|
/* to consider the first time round */
|
||||||
|
|
||||||
|
/* We now use the while statement. The 'loop' continues to execute */
|
||||||
|
/* until such time as the expression in the brackets becomes false */
|
||||||
|
|
||||||
|
/* we use != , meaning 'Not Equal to' as the test, as we wish the */
|
||||||
|
/* the statements to be executed every time we press a key other */
|
||||||
|
/* than 'q'. Once we press 'q' the code continues on to the next */
|
||||||
|
/* statement */
|
||||||
|
|
||||||
|
while ( x != 'q' )
|
||||||
|
{
|
||||||
|
/* Display a message on the screen */
|
||||||
|
printf("\nThe key you pressed was the %c key\n",x );
|
||||||
|
|
||||||
|
/* Get a new value for X, if we do not the 'expression' will never */
|
||||||
|
/* change, and the loop will go for ever */
|
||||||
|
|
||||||
|
printf( "Press another key or q to quit \n");
|
||||||
|
x = getch();
|
||||||
|
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
}
|
37
LC9/while_example_version_2.c
Normal file
37
LC9/while_example_version_2.c
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
/*
|
||||||
|
This program shows how to use a WHILE statment to keep displaying
|
||||||
|
the key pressed on the screen. It terminates when the 'q' key is
|
||||||
|
pressed
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <stdio.h> /* Standard include files */
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <conio.h>
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
|
||||||
|
char x; /* Define a variable of type char */
|
||||||
|
|
||||||
|
|
||||||
|
/* We now use the while statement. The 'loop' continues to execute */
|
||||||
|
/* until such time as the expression in the brackets becomes false */
|
||||||
|
|
||||||
|
/* we use != , meaning 'Not Equal to' as the test, as we wish the */
|
||||||
|
/* the statements to be executed every time we press a key other */
|
||||||
|
/* than 'q'. Once we press 'q' the code continues on to the next */
|
||||||
|
/* statement */
|
||||||
|
|
||||||
|
/* Note: This time the getch is in the while expression */
|
||||||
|
|
||||||
|
|
||||||
|
while ( ( x = getch() ) != 'q' )
|
||||||
|
{
|
||||||
|
/* Display a message on the screen */
|
||||||
|
printf("\nThe key you pressed was the %c key",x );
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
}
|
Reference in New Issue
Block a user