Compare commits

...

10 Commits

203 changed files with 898 additions and 5417 deletions

View File

@@ -1,18 +0,0 @@
#include <stdio.h>
#include <assert.h>
int main()
{
int x = 7;
/* Some big code in between and let's say x
       is accidentally changed to 9 */
x = 9;
// Programmer assumes x to be 7 in rest of the code
assert(x==7);
/* Rest of the code */
return 0;
}

View File

@@ -1,45 +0,0 @@
#include<stdio.h>
#define BLOCKSIZE (8)
int main(void)
{
int i = 0;
int limit = 19; /* could be anything */
int blocklimit;
/* The limit may not be divisible by BLOCKSIZE,
* go as near as we can first, then tidy up.
*/
blocklimit = (limit / BLOCKSIZE) * BLOCKSIZE;
/* unroll the loop in blocks of 8 */
while( i < blocklimit )
{
printf("process(%d)\n", i);
printf("process(%d)\n", i+1);
printf("process(%d)\n", i+2);
printf("process(%d)\n", i+3);
printf("process(%d)\n", i+4);
printf("process(%d)\n", i+5);
printf("process(%d)\n", i+6);
printf("process(%d)\n", i+7);
/* update the counter */
i += 8;
}
if( i < limit )
{
/* Jump into the case at the place that will allow
* us to finish off the appropriate number of items. */
switch( limit - i )
{
case 7 : printf("process(%d)\n", i); i++;
case 6 : printf("process(%d)\n", i); i++;
case 5 : printf("process(%d)\n", i); i++;
case 4 : printf("process(%d)\n", i); i++;
case 3 : printf("process(%d)\n", i); i++;
case 2 : printf("process(%d)\n", i); i++;
case 1 : printf("process(%d)\n", i);
}
}
}

View File

@@ -1,11 +0,0 @@
# define NDEBUG
#include <stdio.h>
# include <assert.h>
int main()
{
int x = 7;
assert (x==5);
printf("x = %d\n", x);
return 0;
}

View File

@@ -1,19 +0,0 @@
#include <stdio.h>
#include <math.h>
float degrees_to_radians(float degrees) {
return M_PI * degrees / 180.0;
}
int main() {
float input, output;
printf("Enter an angle in degrees: ");
scanf("%f", &input);
output = degrees_to_radians(input);
printf("%f radians\n", output);
return 0;
}

View File

@@ -1,36 +0,0 @@
#include <stdio.h>
#include <math.h>
float degrees_to_radians(float degrees) {
return M_PI * degrees / 180.0;
}
int main() {
int rc, start, end;
printf("Start: ");
rc = scanf("%d", &start);
if (rc != 1) {
printf("Please enter a real integer\n");
return 1;
}
printf("End: ");
rc = scanf("%d", &end);
if (rc != 1) {
printf("Please enter a real integer\n");
return 1;
}
if (start > end) {
printf("Please ensure that start value is smaller than end value\n");
return 1;
}
for (int i = start; i <= end; i++) {
printf("%d %f\n", i, degrees_to_radians((float)i));
}
return 0;
}

View File

@@ -1,42 +0,0 @@
#include <stdio.h>
int date_to_day(int year, int month, int day) {
if (month < 3) {
month += 12;
year -= 1;
}
return ((13*month+3)/5 + day + year + year/4 - year/100 + year/400) % 7;
}
int main() {
int rc, year, month, day, nd;
printf("Enter a day (yyyy-mm-dd): ");
rc = scanf("%d-%d-%d", &year, &month, &day);
if (rc != 3) {
printf("Failed to parse date\n");
return 1;
}
nd = date_to_day(year, month, day);
switch (nd) {
case 0:
printf("Monday\n"); break;
case 1:
printf("Tuesday\n"); break;
case 2:
printf("Wednesday\n"); break;
case 3:
printf("Thursday\n"); break;
case 4:
printf("Friday\n"); break;
case 5:
printf("Saturday\n"); break;
case 6:
printf("Sunday\n"); break;
}
return 0;
}

View File

@@ -1,45 +0,0 @@
#include <stdio.h>
int date_to_day(int year, int month, int day) {
if (month < 3) {
month += 12;
year -= 1;
}
return ((13*month+3)/5 + day + year + year/4 - year/100 + year/400) % 7;
}
void print_day_of_week(int day) {
switch (day) {
case 0:
printf("Monday\n"); break;
case 1:
printf("Tuesday\n"); break;
case 2:
printf("Wednesday\n"); break;
case 3:
printf("Thursday\n"); break;
case 4:
printf("Friday\n"); break;
case 5:
printf("Saturday\n"); break;
case 6:
printf("Sunday\n"); break;
}
}
int main() {
int rc, year, month, day, nd;
printf("Enter a day (yyyy-mm-dd): ");
rc = scanf("%d-%d-%d", &year, &month, &day);
if (rc != 3) {
printf("Failed to parse date\n");
return 1;
}
nd = date_to_day(year, month, day);
print_day_of_week(nd);
return 0;
}

View File

@@ -1,43 +0,0 @@
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
// Define HOW the function is to be used, code comes later
float CalculateSurfaceAreaOfCylinder ( float R, float L );
// Function to calculate the volume of a cylinder
float CalculateVolumneOfCylinder ( float R, float L )
{
// Calculate \& return value
float Result;
Result = (M_PI * R * R * L);
return (Result);
}
int main(void)
{
// Declare variables
float r, l, SurfaceArea, Volume;
// Obtain values
printf("\nPlease enter the radius");
scanf("%f", &r);
printf("\nPlease enter the length");
scanf("%f", &l);
// Get and display the volume
Volume = CalculateVolumneOfCylinder(r, l);
printf ("\nThe volume is %f", Volume);
// Get and display the surface area
SurfaceArea = CalculateSurfaceAreaOfCylinder(r, l);
printf ("\nThe surface area is %f", SurfaceArea);
return 0;
}
// Calculate the surface areas of a cylinder
float CalculateSurfaceAreaOfCylinder ( float R, float L )
{
// Calculate \& return value
return (2 * M_PI * R * R ) + ( 2 * M_PI * R * L);
}

View File

@@ -1,38 +0,0 @@
#include <stdio.h>
#include <stdlib.h>
// Define HOW the function is to be used, code comes later
void DisplayDayOfTheWeek ( int Day );
int main(void) // Execution starts here
{
int d; // Declare variable
// Obtain values
printf("\nPlease enter a number betwwen 0 and 6");
scanf("%d", &d);
// Use a function to display the day of the week
DisplayDayOfTheWeek(d);
return 0;
}
// Function to display day of week - nothing is returned
void DisplayDayOfTheWeek ( int Day )
{
// Display date based on value
// Case values on one line as easier to cut/paste :-)
switch (Day)
{
case 0 : printf ("Sunday") ; break;
case 1 : printf ("Monday") ; break;
case 2 : printf ("Tuesday") ; break;
/* etc. for other days of the week */
default:
printf ("Invaid day provided");
}
return; // No value needed as the return type is void
}

View File

@@ -1,16 +0,0 @@
#include <stdio.h>
#include <stdlib.h>
int main(void) // Main : Execution starts here...
{
// Declare variables - pre-populate the array
int Ages[10] = {12,34,23,11,8,19,6,44,9,16};
int i;
// Loop from 0 to 9 inclusive
for ( i = 0 ; i < 10 ; i++ )
printf ("Item %d contains value %d\n",i ,Ages[i]);
// Exit the application
return 0;
}

View File

@@ -1,25 +0,0 @@
#include <stdio.h>
#include <stdlib.h>
// Main () - execution starts here
int main (void)
{
// Declare variables
int SampleData[10];
int a=0;
// Show initial value of a
printf ("Values in a is %d \n", a);
// Change an array item that IS NOT DEFINED
SampleData[10] = 20;
// See how thi affect the value in a
printf ("Values in a is %d \n", a);
// Note: we can retieive this 'invalid' value - it is however 'a' we are getting
printf ("Values in SampleData[10] is %d \n", SampleData[10]);
return (0); // Exit indicating sucess
}

View File

@@ -1,15 +0,0 @@
#include <stdio.h>
#define ARRAY_LEN 90
int main() {
int i;
float arr[ARRAY_LEN];
for (i = 0; i < ARRAY_LEN; i++) {
arr[i] = (float)i;
}
for (i = 0; i < ARRAY_LEN; i++) {
printf("%d %f\n", i, arr[i]);
}
}

View File

@@ -1,21 +0,0 @@
#include <stdio.h>
#include <math.h>
#define ARRAY_LEN 90
float degrees_to_radians(float degrees) {
return M_PI * degrees / 180.0;
}
int main() {
int i;
float arr[ARRAY_LEN];
for (i = 0; i < ARRAY_LEN; i++) {
arr[i] = degrees_to_radians((float)i);
}
for (i = 0; i < ARRAY_LEN; i++) {
printf("%d %f\n", i, arr[i]);
}
}

View File

@@ -1,16 +0,0 @@
#include <stdio.h>
#include <stdlib.h>
// Main : Execution starts here...
int main(void)
{
// Declare variable - pre-populate the array
char msg[] = {'H','i',' ','W','o','r','l','d','\0'};
// Output with printf
printf ("The text is: %s\n", msg);
puts(msg); // We could also use puts to display the string
return 0; // Exit the application
}

View File

@@ -1,15 +0,0 @@
#include <stdio.h>
#include <stdlib.h>
int main(void) // Main : Execution starts here...
{
// Declare variable - pre-populate the array
char msg[] = "Hello World";
// Output with printf
printf ("The text is: %s\n", msg);
puts(msg); // We could also use puts to display the string
return 0; // Exit the application
}

View File

@@ -1,20 +0,0 @@
void SampleFunction1 ( int x )
{
int i,j,k;
return;
}
void SampleFunction2 ( int x )
{
int i,j,k;
SampleFunction1(x);
return;
}
// Main : Execution starts here...
int main(void)
{
int i = 1;
SampleFunction2(i);
return 0;
}

View File

@@ -1,25 +0,0 @@
#include <stdio.h>
#include <stdlib.h>
int main (void )
{
// Declare a in integer
int c,d;
// Declar and integer pointer
int *ptrC;
// Some assgnments
c = 10; // C now contains the value 10
ptrC = &c; // ptrC now 'Points' to c
// Get the value of c via the pointer and store in d
d = *ptrC; // d now contains 10
printf ("\nThe value in d is %d", d);
// Change the value of c via the pointer ptrC
*ptrC = 1; //c now contains 1
printf ("\nThe value in c is %d", c);
return 0; // exit
}

View File

@@ -1,24 +0,0 @@
#include <stdio.h>
#include <stdlib.h>
int main (void )
{
// Integer variables
int a, ValueB, d;
// integer pointers
int *ptrA=&a, *B=&ValueB, *Data=&d;
// Float variables
float f,y,z;
// Float pointers
float *pf=&f , *q=&y, *Zvalue=&z;
// We could also do this on separate lines e.g.
int SomeData;
int *Another;
Another = &SomeData;
return 0;
}

View File

@@ -1,27 +0,0 @@
#include <stdio.h>
#include <stdlib.h>
int main (void )
{
// Declare a in integer
int c,d;
// Declar and integer pointer
int *ptrC;
// Some assgnments
c = 10; // C now contains the value 10
ptrC = &c; // ptrC now 'Points' to c
*ptrC = 20;
// Get the value of c via the pointer and store in d
d = *ptrC; // d now contains 10
printf ("The value in d is %d\n", d);
// Change the value of c via the pointer ptrC
*ptrC = 1; //c now contains 1
printf ("The value in c is %d\n", c);
return 0; // exit
}

View File

@@ -1,27 +0,0 @@
#include <stdio.h>
#include <stdlib.h>
int main (void )
{
// Declare a in integer
float c,d;
// Declar and integer pointer
float *ptrC;
// Some assgnments
c = 10; // C now contains the value 10
ptrC = &c; // ptrC now 'Points' to c
*ptrC = 20;
// Get the value of c via the pointer and store in d
d = *ptrC; // d now contains 10
printf ("The value in d is %f\n", d);
// Change the value of c via the pointer ptrC
*ptrC = 1; //c now contains 1
printf ("The value in c is %f\n", c);
return 0; // exit
}

View File

@@ -1 +0,0 @@
include ../Makefile

View File

@@ -1,32 +0,0 @@
#include <stdio.h>
#include <math.h>
void cartesian_to_polar(float x, float y, float *r, float *theta) {
*r = sqrtf(x*x + y*y);
*theta = y/x;
}
int main() {
int rc;
float x, y, r, theta;
printf("x: ");
rc = scanf("%f", &x);
// check that scanf was successful by checking if it returned 1 (1 successfully scanned item)
if (rc != 1) {
printf("Please enter a real number\n");
return 1;
}
printf("y: ");
rc = scanf("%f", &y);
if (rc != 1) {
printf("Please enter a real number\n");
return 1;
}
cartesian_to_polar(x, y, &r, &theta);
printf("r=%f theta=%f\n", r, theta);
return 0;
}

View File

@@ -1,29 +0,0 @@
#include <stdio.h>
#include <math.h>
float degrees_to_radians(float degrees) {
return M_PI * degrees / 180.0;
}
void process(float degrees, float* radians, float* sin_val, float* cos_val, float* tan_val) {
*radians = degrees_to_radians(degrees);
*sin_val = sin(*radians);
*cos_val = cos(*radians);
*tan_val = tan(*radians);
}
int main() {
float degrees, radians, sin_val, cos_val, tan_val;
printf("Degrees: ");
scanf("%f", &degrees);
process(degrees, &radians, &sin_val, &cos_val, &tan_val);
printf("Radians: %f\n", radians);
printf("Sine: %f\n", sin_val);
printf("Cosine: %f\n", cos_val);
printf("Tangent: %f\n", tan_val);
}

View File

@@ -1,33 +0,0 @@
#include <stdio.h>
#include <math.h>
float degrees_to_radians(float degrees) {
return M_PI * degrees / 180.0;
}
void process(float degrees, float* radians, float* sin_val, float* cos_val, float* tan_val) {
*radians = degrees_to_radians(degrees);
*sin_val = sin(*radians);
*cos_val = cos(*radians);
*tan_val = tan(*radians);
}
int main() {
int degrees_start, degrees_end;
float radians, sin_val, cos_val, tan_val;
printf("Start: ");
scanf("%d", &degrees_start);
printf("End: ");
scanf("%d", &degrees_end);
printf("Degs\tRad\tsin\tcos\ttan\n");
for (int i = degrees_start; i <= degrees_end; i++) {
process((float)i, &radians, &sin_val, &cos_val, &tan_val);
printf("%d\t%0.3f\t%0.3f\t%0.3f\t%0.3f\n", i, radians, sin_val, cos_val, tan_val);
}
}

View File

@@ -1,55 +0,0 @@
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int SolveQuadraticEquation(float a, float b, float c, float *x1, float *x2)
{
float d; // For storing b^2 - 4*a*c
if ( a == 0) // Not a quadratie
{
return -1;
}
// calculate and store b*b-4*a*c for testing ans use later (if OK)
d = b*b - 4*a*c;
if ( d < 0 )
{
return -1; // Complex
}
// If we have got to here, we can calculate x1 and x2
*x1 = ( -b + sqrt (d)) / (2 * a); // Note the use of the * before x1 & x2
*x2 = ( -b - sqrt (d)) / (2 * a); // to write to the relevant memory locations
// As we got here OK, return 0 to indicate all is OK
return 0;
}
int main (void )
{
float A,B,C,x1,x2;
int retval;
printf ("Please enter coefficients A,B and C separated by a space\n");
scanf ("%f %f %f", &A, &B, &C);
// Make use of the function
retval = SolveQuadraticEquation(A, B, C, &x1, &x2);
// Use the retval to determine if we can display the answers or an derror message
if ( retval == -1 )
{
printf ("Not a quadratic\n");
}
else if (retval == -1)
{
printf ("The solution is complex - I cannot solve these\n");
}
else
{
printf("\nThe solutions are x1=%f, x2=%f", x1, x2);
}
return 0; // exit
}

View File

@@ -1,24 +0,0 @@
int SolveQuadraticEquation(float a, float b, float c, float *x1, float *x2)
{
float d; // For storing b^2 - 4*a*c
if ( a == 0) // Not a quadratie
{
return -1;
}
// calculate and store b*b-4*a*c for testing ans use later (if OK)
d = b*b - 4*a*c;
if ( d < 0 )
{
return -1; // Complex
}
// If we have got to here, we can calculate x1 and x2
*x1 = ( -b + sqrt (d)) / (2 * a); // Note the use of the * before x1 & x2
*x2 = ( -b - sqrt (d)) / (2 * a); // to write to the relevant memory locations
// As we got here OK, return 0 to indicate all is OK
return 0;
}

View File

@@ -1,20 +0,0 @@
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
// Declare and populate an integer array
int MyArray[10] = {2,4,6,8,10,12,14,18,20};
// Declate an integer pointer
int *pI;
// Get the start address by asking for the address iof array item [0]
pI = &MyArray[0];
// Or, use the fact the array name on its own is the start address of the array
pI = MyArray;
return 0; // Exit
}

View File

@@ -1,22 +0,0 @@
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
// Declare an integer array and an integer pointer
int MyArray[10] = {2,4,6,8,10,12,14,16,18,20};
int *pI;
// Get the start address by asking for the address iof array item [0]
pI = &MyArray[0]; // Or use: pI = MyArray;
// Display the 1st item in the array, first be accessing rhe array
printf("The value at array item [0] is %d\n", MyArray[0]);
// Since the pointer points to the address of the 1st item we can
// access it as we would for a pointer pointing to any single variable
printf("The value at the memory address held in pI is %d\n", *pI);
return 0; // Exit
}

View File

@@ -1,23 +0,0 @@
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
// Declare an integer array and an integer pointer
int MyArray[10] = {2,4,6,8,10,12,14,16,18,20};
int *pI;
int i;
// Get the start address by asking for the address iof array item [0]
pI = &MyArray[0]; // or use: pI = MyArray;
// Use loop to display values
for ( i = 0 ; i < 10 ; i++ )
{
printf ("Value at index %d (direct access to the arrays) is: %d\n", i, MyArray[i]);
printf ("Value at index %d (access via the pointer) is: %d\n", i, pI[i]);
}
return 0; // Exit
}

View File

@@ -1,33 +0,0 @@
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
// Declare an integer array and an integer pointer
int MyArray[10];
int *pI;
int i;
// Get the start address by asking for the address iof array item [0]
pI = &MyArray[0]; // or use: pI = MyArray;
// Use loop to display values
for ( i = 0 ; i < 10 ; i++ )
{
// Set the value then use the increment operator to move the pointer
// to the next memory location. you can picture this as two steps: '
//
// *pI = 5 + 4*i;
// then
// *pI++;
*pI++ = 5 + 4*i; // set value at index[i] to 5+4*i
}
// Display the values placed in the array
for ( i = 0 ; i < 10 ; i++)
{
printf("%d ", MyArray[i]);
}
return 0; // Exit
}

View File

@@ -1,17 +0,0 @@
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
// Declare an integer array and an integer pointer
int *pData;
// Using malloc
pData = malloc ( 10000 * sizeof (int));
// Using calloc
pData = calloc ( 10000 , sizeof (int));
return 0; // Exit
}

View File

@@ -1,14 +0,0 @@
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
// Declare an integer array and an integer pointer
int *pData;
pData = calloc ( 10000 , sizeof (float)); // No warning
pData = (float *)calloc ( 10000 , sizeof (float)); // Warning
return 0; // Exit
}

View File

@@ -1,22 +0,0 @@
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
// Declare an integer array and an integer pointer
int *pData;
// Using calloc (same approach malloc)
pData = calloc ( 10000 , sizeof (int));
if ( pData == NULL)
{
printf ("\nMemory could not be allocated - terminating");
return -1; // Use minus one as we did not exit successfully
}
// We have our memory, make use of it here!
return 0; // Exit successfully
}

View File

@@ -1,25 +0,0 @@
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
// Declare an integer array and an integer pointer
int *pData;
// Using calloc (same approach malloc)
pData = calloc ( 10000 , sizeof (int));
if ( pData == NULL)
{
printf ("\nMemory could not be allocated - terminating");
return -1; // Use minus one as we did not exit sucesfully
}
// We have our memory, make use of it here!
// Free up the allocated memoey
free (pData);
return 0; // Exit sucesfully
}

View File

@@ -1,18 +0,0 @@
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
// Declare an integer array and an integer pointer
int *pData;
int datlen;
printf("Length of data to be allocated: ");
scanf("%d", &datlen);
// Using malloc
pData = malloc ( datlen * sizeof (int));
free(pData);
return 0; // Exit
}

View File

@@ -1,23 +0,0 @@
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
// Declare an integer array and an integer pointer
int *pData;
int datlen;
printf("Length of data to be allocated: ");
scanf("%d", &datlen);
// Using malloc
pData = malloc ( datlen * sizeof (int));
for (int i = 0; i < datlen; i++) {
pData[i] = i;
printf("%d %d\n", i, i);
}
free(pData);
return 0; // Exit
}

View File

@@ -1,40 +0,0 @@
#include <stdio.h>
#include <stdlib.h>
// Simple function to populate an integer array
void PopulateTheArray ( int Size, int ArrayData[])
{
int i; // Variable to use in our loop
for ( i = 0 ; i < Size ; i++)
{
ArrayData[i] = 2*i + 1; // Treat it like a normal array
}
}
// Simple function to display contents of an integer array
void DisplayTheArray ( int Size, int ArrayData[])
{
int i; // Variable to use in our loop
for ( i = 0 ; i < Size ; i++)
{
printf ("Item %d of the array contains %d\n", i, ArrayData[i]);
}
}
// Main () - execution starts here
int main (void)
{
int Data[10];
// Pass the size of the array and the array to our function -
// remembering that the array name on its own is the base address,
// and so is the same as passing &Data[0]
PopulateTheArray(10, Data);
DisplayTheArray(10, Data);
return (0); // Exit indicating sucess
}

View File

@@ -1,55 +0,0 @@
#include <stdio.h>
#include <stdlib.h>
// Simple function to populate an integer array
void PopulateTheArray ( int Size, int ArrayData[])
{
int i; // Variable to use in our loop
for ( i = 0 ; i < Size ; i++)
{
ArrayData[i] = 2*i + 1; // Treat it like a normal array
}
}
// Simple function do display contents an integer array
void DisplayTheArray ( int Size, int ArrayData[])
{
int i; // Variable to use in our loop
for ( i = 0 ; i < Size ; i++)
{
printf ("Item %d of the array contains %d\n", i, ArrayData[i]);
}
}
// Main () - execution starts here
int main (void)
{
int iSizeForArray;
int *pData; // A pointer to hold the base address of out array
// Ask for the size of the array and store result
printf("\nPlease enter the size of the array to dynamically allocate");
scanf ("%d", &iSizeForArray);
// Use calloc with checking
pData = calloc ( iSizeForArray, sizeof (int));
// Check we got the memory
if ( pData == NULL)
{
printf ("\nSorry, I could not allocate the memory, bye!");
return -1;
}
// Pass the size, iSizeForArray) and the pointer created
// which points to the start of the sucesfully allocated memory
PopulateTheArray(iSizeForArray, pData);
DisplayTheArray(iSizeForArray, pData);
free (pData); // Free up the memory before exiting
return (0); // Exit indicating sucess
}

View File

@@ -1,62 +0,0 @@
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
float degrees_to_radians(float degrees) {
return M_PI * degrees / 180.0;
}
// Simple function to populate an integer array
void PopulateTheArray ( int Size, float ArrayData[])
{
int i; // Variable to use in our loop
for ( i = 0 ; i < Size ; i++)
{
ArrayData[i] = degrees_to_radians(i); // Treat it like a normal array
}
}
// Simple function do display contents an integer array
void DisplayTheArray ( int Size, float ArrayData[])
{
int i; // Variable to use in our loop
for ( i = 0 ; i < Size ; i++)
{
printf ("%d\t%0.3f\n", i, ArrayData[i]);
}
}
// Main () - execution starts here
int main (void)
{
int iSizeForArray;
float *pData; // A pointer to hold the base address of out array
// Ask for the size of the array and store result
printf("Please enter the size of the array to dynamically allocate: ");
scanf ("%d", &iSizeForArray);
// Use calloc with checking
pData = calloc( iSizeForArray, sizeof (float));
// Check we got the memory
if ( pData == NULL)
{
printf ("Sorry, I could not allocate the memory, bye!\n");
return -1;
}
// Pass the size, iSizeForArray) and the pointer created
// which points to the start of the sucesfully allocated memory
PopulateTheArray(iSizeForArray, pData);
DisplayTheArray(iSizeForArray, pData);
free(pData); // Free up the memory before exiting
return (0); // Exit indicating sucess
}

View File

@@ -1,64 +0,0 @@
#include <stdio.h>
#include <stdlib.h>
// Main () - execution starts here
int main (void)
{
// Declate file stream variables
FILE *fInput, *fOutput;
// Other variables needed
int i;
int SampleArray[10] = {1,2,3,4,5,6,7,8,9,10};
float f = 23.4;
// Try and open the binary "numbers.dat" (in the current directory) file for writing
fOutput = fopen ("numbers.dat", "wb");
// Check we were able to open the file
if ( fOutput == NULL)
{
printf ("\nThe file could not be opened for writing, exiting");
return -1;
}
// Write out a single float to the binary file
fwrite ( &f, sizeof(float), 1 , fOutput);
// Now the entire array on one go
fwrite ( SampleArray, sizeof(int), 10 , fOutput);
// And close the file
fclose (fOutput);
// Try and open the binary "numbers.dat" (in the current directory) file for reading
fInput = fopen ("numbers.dat", "rb");
// Check we were able to open the file
if ( fInput== NULL)
{
printf ("\nthe file could not be opened for reading, exiting");
return -1;
}
// Read a single float from the binary file into f
fread ( &f, sizeof(float), 1 , fOutput);
// Now read the entire array on one go
fread ( SampleArray, sizeof(int), 10 , fOutput);
// Display the values read from the file on the screen
printf ("The value read into f is %f\n", f);;
for ( i = 0 ; i < 10 ; i++)
{
printf ("Item %d of the array contains %d\n",i, SampleArray[i]);
}
// And close the file
fclose (fInput);
return (0); // Exit indicating sucess
}

Binary file not shown.

View File

@@ -1,52 +0,0 @@
#include <stdio.h>
#include <stdlib.h>
// Main () - execution starts here
int main (void)
{
// Declate file stream variables
FILE *fInput, *fOutput, *fRecords;
// Try and open the text "sample.txt" (in the current directory) file for reading
fInput = fopen ("sample.txt", "r");
// Check we were able to open the file
if ( fInput == NULL)
{
printf ("\nthe file could not be opened");
return -1; // Exit as unsuccessful
}
fclose (fInput); // Close the file
// Try and open the binary "samples.dat" (in the current directory) file for writing
// if a file of this name already exists it will be deleted
fOutput = fopen ("samples.dat", "wb");
// Check we were able to open the file
if ( fOutput == NULL)
{
printf ("\nthe file could not be opened");
return -1; // Exit as unsuccessful
}
fclose (fOutput); // Close the file
// Open, for appending, the text file "records.txt". If the file does not already
// exists, a new one of this name will be created (as if "w") were the mocde
fRecords = fopen ("records.txt", "a");
// Check we were able to open the file
if ( fRecords == NULL)
{
printf ("\nthe file could not be opened");
return -1; // Exit as unsuccessful
}
fclose (fRecords);
return (0); // Exit indicating sucess
}

View File

@@ -1,10 +0,0 @@
1
2
3
4
5
6
7
8
9
10

View File

@@ -1,55 +0,0 @@
#include <stdio.h>
#include <stdlib.h>
// Main () - execution starts here
int main (void)
{
// Declate file stream variables
FILE *fInput, *fOutput;
// Other variables needed
int i,d;
// Try and open the text "sample.txt" (in the current directory) file for writing
fOutput = fopen ("numbers.txt", "w");
// Check we were able to open the file
if ( fOutput == NULL)
{
printf ("\nthe file could not be opened for writing, exiting");
return -1;
}
// Use a loop to write values to the newly created file
for ( i = 1 ; i <= 10 ; i++)
{
fprintf (fOutput, "%d\n", i);
}
// And close the file
fclose (fOutput);
// Try and open the binary "numbers " (in the current directory) file for reading
fInput = fopen ("numbers.txt", "r");
// Check we were able to open the file
if ( fInput == NULL)
{
printf ("\nthe file could not be opened for reading, exiting");
return -1;
}
// Read, line by line the 10 values written into variable d
// and then display the contents of d on the screen
for ( i = 1 ; i <= 10 ; i++)
{
fscanf (fInput, "%d", &d);
printf ("Value read from file %d\n",d);
}
// And close the file
fclose (fInput);
return (0); // Exit indicating success
}

View File

@@ -1,55 +0,0 @@
#include <stdio.h>
#include <stdlib.h>
// Main () - execution starts here
int main (void)
{
// Declate file stream variables
FILE *fInput, *fOutput;
// Other variables needed
int i, d, d2;
// Try and open the text "sample.txt" (in the current directory) file for writing
fOutput = fopen ("numbers.txt", "w");
// Check we were able to open the file
if ( fOutput == NULL)
{
printf ("The file could not be opened for writing, exiting\n");
return -1;
}
// Use a loop to write values to the newly created file
for ( i = 1 ; i <= 10 ; i++)
{
fprintf (fOutput, "%d %d\n", i, i*i);
}
// And close the file
fclose (fOutput);
// Try and open the binary "numbers " (in the current directory) file for reading
fInput = fopen ("numbers.txt", "r");
// Check we were able to open the file
if ( fInput == NULL)
{
printf ("The file could not be opened for reading, exiting\n");
return -1;
}
// Read, line by line the 10 values written into variable d
// and then display the contents of d on the screen
for ( i = 1 ; i <= 10 ; i++)
{
fscanf (fInput, "%d %d", &d, &d2);
printf ("Value read from file: %d %d\n", d, d2);
}
// And close the file
fclose (fInput);
return (0); // Exit indicating success
}

View File

@@ -1,60 +0,0 @@
#include <stdio.h>
#include <stdlib.h>
// Main () - execution starts here
int main (void)
{
// Declate file stream variables
FILE *fInput, *fOutput;
// 8x the maximum filename length on most filesystems should be a decent size
char filename[2049] = { 0 };
printf("Enter filename: ");
scanf("%2048s", filename);
// Other variables needed
int i, d, d2;
// Try and open the text "sample.txt" (in the current directory) file for writing
fOutput = fopen (filename, "w");
// Check we were able to open the file
if ( fOutput == NULL)
{
printf ("The file could not be opened for writing, exiting\n");
return -1;
}
// Use a loop to write values to the newly created file
for ( i = 1 ; i <= 10 ; i++)
{
fprintf (fOutput, "%d %d\n", i, i*i);
}
// And close the file
fclose (fOutput);
// Try and open the binary "numbers " (in the current directory) file for reading
fInput = fopen ("numbers.txt", "r");
// Check we were able to open the file
if ( fInput == NULL)
{
printf ("The file could not be opened for reading, exiting\n");
return -1;
}
// Read, line by line the 10 values written into variable d
// and then display the contents of d on the screen
for ( i = 1 ; i <= 10 ; i++)
{
fscanf (fInput, "%d %d", &d, &d2);
printf ("Value read from file: %d %d\n", d, d2);
}
// And close the file
fclose (fInput);
return (0); // Exit indicating success
}

View File

@@ -1,92 +0,0 @@
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
float degrees_to_radians(float degrees) {
return M_PI * degrees / 180.0;
}
// Simple function to populate an integer array
void PopulateTheArray ( int Size, float ArrayData[])
{
int i; // Variable to use in our loop
for ( i = 0 ; i < Size ; i++)
{
ArrayData[i] = degrees_to_radians(i); // Treat it like a normal array
}
}
// Simple function do display contents an integer array
void DisplayTheArray ( int Size, float ArrayData[])
{
int i; // Variable to use in our loop
for ( i = 0 ; i < Size ; i++)
{
printf ("%d\t%0.3f\n", i, ArrayData[i]);
}
}
int SaveArray (char *filename, int Size, float ArrayData[])
{
FILE *fp; // File Pointer
int i; // Variable to use in our loop
fp = fopen(filename, "w");
// if file open failed, return error to caller
if (fp == NULL) {
return 1;
}
for ( i = 0 ; i < Size ; i++)
{
fprintf(fp, "%d\t%0.3f\n", i, ArrayData[i]);
}
// close file, returns 0 on successful closing (therefore function has executed sucessfully)
return fclose(fp);
}
// Main () - execution starts here
int main (void)
{
char filename[2049];
int rc, iSizeForArray;
float *pData; // A pointer to hold the base address of out array
// Ask for the size of the array and store result
printf("Please enter the size of the array to dynamically allocate: ");
scanf ("%d", &iSizeForArray);
// Use calloc with checking
pData = calloc( iSizeForArray, sizeof (float));
// Check we got the memory
if ( pData == NULL)
{
printf ("Sorry, I could not allocate the memory, bye!\n");
return -1;
}
// Pass the size, iSizeForArray) and the pointer created
// which points to the start of the sucesfully allocated memory
PopulateTheArray(iSizeForArray, pData);
DisplayTheArray(iSizeForArray, pData);
// Exercise doesn't ask to use function but kind of pointless without
printf("Enter filename: ");
scanf("%2048s", filename);
rc = SaveArray(filename, iSizeForArray, pData);
if (rc != 0) {
printf("Error saving file\n");
free(pData);
return rc;
}
free(pData); // Free up the memory before exiting
return (0); // Exit indicating sucess
}

View File

@@ -1,88 +0,0 @@
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
float degrees_to_radians(float degrees) {
return M_PI * degrees / 180.0;
}
// Simple function to populate an integer array
void PopulateTheArray ( int Size, float ArrayData[])
{
int i; // Variable to use in our loop
for ( i = 0 ; i < Size ; i++)
{
ArrayData[i] = degrees_to_radians(i); // Treat it like a normal array
}
}
// Simple function do display contents an integer array
void DisplayTheArray ( int Size, float ArrayData[])
{
int i; // Variable to use in our loop
for ( i = 0 ; i < Size ; i++)
{
printf ("%d\t%0.3f\n", i, ArrayData[i]);
}
}
int SaveArrayBinary (char *filename, int Size, float ArrayData[])
{
FILE *fp; // File Pointer
int i; // Variable to use in our loop
fp = fopen(filename, "wb");
// if file open failed, return error to caller
if (fp == NULL) {
return 1;
}
// write data
fwrite(ArrayData, sizeof(float), Size, fp);
// close file, returns 0 on successful closing (therefore function has executed sucessfully)
return fclose(fp);
}
// Main () - execution starts here
int main (void)
{
char filename[2049];
int rc, iSizeForArray;
float *pData; // A pointer to hold the base address of out array
// Ask for the size of the array and store result
printf("Please enter the size of the array to dynamically allocate: ");
scanf ("%d", &iSizeForArray);
// Use calloc with checking
pData = calloc( iSizeForArray, sizeof (float));
// Check we got the memory
if ( pData == NULL)
{
printf ("Sorry, I could not allocate the memory, bye!\n");
return -1;
}
// Pass the size, iSizeForArray) and the pointer created
// which points to the start of the sucesfully allocated memory
PopulateTheArray(iSizeForArray, pData);
DisplayTheArray(iSizeForArray, pData);
// Exercise doesn't ask to use function but kind of pointless without
printf("Enter filename: ");
scanf("%2048s", filename);
rc = SaveArrayBinary(filename, iSizeForArray, pData);
if (rc != 0) {
printf("Error saving file\n");
free(pData);
return rc;
}
free(pData); // Free up the memory before exiting
return (0); // Exit indicating sucess
}

View File

@@ -1,51 +0,0 @@
#include <stdio.h>
#include <stdlib.h>
int main() {
FILE *fp; // file pointer
float *data; // data pointer (array of floats)
int i, file_size, arr_size;
char filename[2049];
printf("Enter filename to read: ");
scanf("%2048s", filename);
fp = fopen(filename, "rb");
if (fp == NULL) {
printf("Failed to open file\n");
return 1;
}
// get file size (technically isn't the best way because SEEK_END is undefined behaviour:
//
// "Library implementations are allowed to not meaningfully support SEEK_END
// (therefore, code using it has no real standard portability)."
// ~ https://cplusplus.com/reference/cstdio/fseek/
//
// " Setting the file position indicator to end-of-file, as with fseek(file, 0, SEEK_END),
// has undefined behavior for a binary stream (because of possible trailing null characters) or
// for any stream with state-dependent encoding that does not assuredly end in the initial shift
// state. "
// ~ https://port70.net/~nsz/c/c11/n1570.html#note268
//
// it is also limited to 2GB files as it returns a signed integer
// better to use something like this probably:
// https://stackoverflow.com/a/238609
//
// but louise uses fseek so i will too
fseek(fp, 0, SEEK_END);
file_size = ftell(fp);
arr_size = file_size/sizeof(float);
rewind(fp);
// allocate memory and read data
data = malloc(file_size);
fread(data, sizeof(float), arr_size, fp);
for (i = 0; i < arr_size; i++) {
printf("%d %0.3f\n", i, data[i]);
}
free(data);
}

View File

@@ -1,18 +0,0 @@
#define UP 1
#define DOWN 2
int main()
{
int i = 1;
if (i == UP )
{
// Do something
}
if ( i == DOWN)
{
// Doe something else
}
return 0;
}

View File

@@ -1,22 +0,0 @@
#include <stdio.h>
#include <stdlib.h>
enum DOW { sun, mon, tue, wed, thu, fri, sat } ;
// Main () - execution starts here
int main (void)
{
enum DOW day;
/* Code that get a value for 'day' */
day = tue;
switch (day)
{
case sun : printf ("Sunday\n") ; break ;
case mon : printf ("Monday\n") ; break ;
case tue : printf ("Tuesday\n") ; break ;
/* etc. */
}
return (0); // Exit indicating success
}

View File

@@ -1,20 +0,0 @@
#include <stdio.h>
struct Person {
int age;
char forename[30];
char surname[50];
};
int main() {
struct Person p;
printf("Enter age: "); scanf("%d", &p.age);
printf("Enter forename: "); scanf("%29s", p.forename);
printf("Enter surname: "); scanf("%49s", p.surname);
printf("p.age=%d p.forename=%s p.surname=%s\n", p.age, p.forename, p.surname);
return 0;
}

View File

@@ -1,25 +0,0 @@
#include <stdio.h>
struct Person {
int age;
char forename[30];
char surname[50];
};
void print_person(struct Person p) {
printf("p.age=%d p.forename=%s p.surname=%s\n", p.age, p.forename, p.surname);
}
int main() {
struct Person p;
printf("Enter age: "); scanf("%d", &p.age);
printf("Enter forename: "); scanf("%29s", p.forename);
printf("Enter surname: "); scanf("%49s", p.surname);
print_person(p);
return 0;
}

View File

@@ -1,27 +0,0 @@
#include <stdio.h>
struct Person {
int age;
int year_of_birth;
char forename[30];
char surname[50];
};
void print_person(struct Person p) {
printf("p.age=%d p.year_of_birth=%d p.forename=%s p.surname=%s\n", p.age, p.year_of_birth, p.forename, p.surname);
}
int main() {
struct Person p;
printf("Enter age: "); scanf("%d", &p.age);
printf("Enter birth year: "); scanf("%d", &p.year_of_birth);
printf("Enter forename: "); scanf("%29s", p.forename);
printf("Enter surname: "); scanf("%49s", p.surname);
print_person(p);
return 0;
}

View File

@@ -1,50 +0,0 @@
#include <stdio.h>
struct Person {
int age;
int year_of_birth;
char forename[30];
char surname[50];
};
void print_person(struct Person p) {
printf("p.age=%d p.year_of_birth=%d p.forename=%s p.surname=%s\n", p.age, p.year_of_birth, p.forename, p.surname);
}
int main() {
struct Person p;
FILE *fp;
char choice;
char filename[4097];
printf("Read or write a file? (r/w) "); scanf("%c", &choice);
if (choice == 'w') {
printf("Enter age: "); scanf("%d", &p.age);
printf("Enter birth year: "); scanf("%d", &p.year_of_birth);
printf("Enter forename: "); scanf("%29s", p.forename);
printf("Enter surname: "); scanf("%49s", p.surname);
printf("Enter filename to save to: "); scanf("%4096s", filename);
fp = fopen(filename, "wb");
if (fp == NULL) {
printf("Failed to open file for writing\n");
return 1;
}
fwrite(&p, 1, sizeof(p), fp);
} else {
printf("Enter file to read from: "); scanf("%4096s", filename);
fp = fopen(filename, "rb");
if (fp == NULL) {
printf("Failed to open file for reading\n");
return 1;
}
fread(&p, 1, sizeof(p), fp);
}
print_person(p);
return 0;
}

View File

@@ -1,26 +0,0 @@
#include <stdio.h>
#include <stdlib.h>
void DisplayHelloWorld (void)
{
static int k = 0; // Counter for how many times the function is called
printf ("Hello World\n");
// Increment counter and display value
k = k + 1;
printf ("I have now said this %d times\n",k);
}
// Main () - execution starts here
int main (void)
{
int i;
// Loop calling out function 10 times
for ( i =0 ; i < 10 ; i++ )
{
DisplayHelloWorld();
}
return (0); // Exit indicating success
}

View File

@@ -1,15 +0,0 @@
#include <stdio.h>
#include <conio.h>
#define DEBUG_ON 0
int main(void)
{
#if DEBUG_ON == 1
printf("Debug mode - about to do something\n");
#else
printf("Running in standard mode");
#endif
return 0;
}

View File

@@ -1,15 +0,0 @@
#include <stdio.h>
#include <conio.h>
//#define DEBUG_ON 0
int main(void)
{
#ifdef DEBUG_ON
printf("Debug mode - about to do something\n");
#else
printf("Running in standard mode");
#endif
return 0;
}

View File

@@ -1,8 +0,0 @@
#include <stdio.h>
#define MIN(a,b) ((a)<(b)?(a):(b))
int main(void)
{
printf("The minimum value of 10 and 20 is: %d\n", MIN(10,20));
return 0;
}

View File

@@ -1,19 +0,0 @@
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i;
char FileName[100];
for ( i = 1 ; i < 10 ; i++)
{
//'Print' text into string
sprintf(FileName , "file%d.dat" , i);
// Sidplay the name created
printf("Current file name: %s\n", FileName);
}
return 0;
}

View File

@@ -1,10 +0,0 @@
#include <stdlib.h>
#include <stdio.h>
/* My first program */
int main(void)
{
// This is a single line of comment
printf("Hello World\n");
return 0;
}

View File

@@ -1,20 +0,0 @@
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
// Declare and initialise as required
int a = 1,b = 4;
float ans;
// Perform calculation
// This time a & b are treated as if they are floats
// the result of this float calculation is stored in ans
ans = (float)a / (float)b;
// Display answer
printf ("\nThe answer is %f",ans);
return 0;
}

View File

@@ -1,20 +0,0 @@
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
// Declare and initialise as required
int a = 1,b = 4;
float ans;
// Perform calculation
// a and b are integers so the answer will be the result of the integer division
ans = a / b;
// Display answer
printf ("\nThe answer is %f",ans);
return 0;
}

View File

@@ -1,13 +0,0 @@
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
// A variable to store a name (maximum 49 characters)
char Name[50];
// Declare multiple strings on the same line
char AddressLine1[100], AddressLine2[100], PostCode[10];
// all done
return 0;
}

View File

@@ -1,4 +0,0 @@
#include <stdio.h>
int main(void) { int revenue = 80; int cost = 50; int roi;
roi = (100 * (revenue - cost)) / cost; if (roi >= 0) {
printf ("%d\n", roi); } return 0; }

View File

@@ -1,4 +0,0 @@
#include <stdio.h>
/* My first program */
int main(void) { printf("Hello World"); return 0; }

View File

@@ -1,19 +0,0 @@
#include <stdio.h>
int main(void)
{
// Declare variables and give initial values
int revenue = 80;
int cost = 50;
int roi;
// Perform calculation
roi = (100 * (revenue - cost)) / cost;
// Make decision based on value of roi
if (roi >= 0)
{
printf ("%d\n", roi);
}
return 0; // Exit indicating success
}

View File

@@ -1,8 +0,0 @@
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int a = 6,b = 7,c; // Declare and initialiase as required
c = a + b; // Add the values and store in c
}

View File

@@ -1,8 +0,0 @@
#include <stdio.h>
/* My first program */
int main(void)
{
printf("Hello World");
return 0;
}

View File

@@ -1,24 +0,0 @@
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
// Declare some variables
int a = 123;
float f = 12.456f;
// Use printf display text on the screen
printf ("Examples of integer formatting\n");
printf ("a = %d (no modifier)\n", a);
printf ("a = %6d (w:6, justify:right)\n", a);
printf ("a = %-6d (w:6 )\n", a);
// Use printf display text on the screen
printf ("Examples of float formatting\n");
printf ("f = %f (no modifier)\n", f);
printf ("f = %6.2f (w:6, 2dp, justify:right)\n", f);
printf ("f = %-6.1f (w:6, 1dp)\n", f);
// Exit from main
return 0;
}

View File

@@ -1,15 +0,0 @@
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
// Declare some variables
int a = 1, b = 2;
float f = 1.23f;
// Use printf display text on the screen
printf ("The variables are\na = %d\nb=%d\nf=%f", a, b, f);
// Exit from main
return 0;
}

View File

@@ -1,11 +0,0 @@
#include <stdio.h>
#include <math.h>
int main() {
float r;
printf("Enter radius: ");
scanf("%f", &r);
printf("Area of sphere: %f\n", 4.0*M_PI*r*r);
printf("Volume of sphere: %f\n", M_PI*r*r*r*4.0/3.0);
}

View File

@@ -1,11 +0,0 @@
#include <stdio.h>
#include <math.h>
int main() {
float r;
printf("Enter radius: ");
scanf("%f", &r);
printf("Area of circle: %0.2f\n", M_PI*r*r);
printf("Volume of sphere: %.02f\n", M_PI*r*r*r*4.0/3.0);
}

View File

@@ -1,17 +0,0 @@
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
// Declare some variables
int a = 1, b = 2;
float f = 1.23f;
// Use printf display text on the screen
printf ("The variables are a, b and f");
// Exit from main
return 0;
}

View File

@@ -1,12 +0,0 @@
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
printf ("This is my first computer program");
printf ("Hello World");
return 0;
}

View File

@@ -1,12 +0,0 @@
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
printf ("This is my first computer program\n");
printf ("Hello World\n");
return 0;
}

View File

@@ -1,28 +0,0 @@
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
// Declare some variables
int a, b;
// Note: Order of a,b indicates the order in which operations are carried out on execution
// Increment operators
b = 3;
printf("a=%d b=%d\n", a, b);
a = ++b; // b is now 4, a is also 4
printf("a=%d b=%d\n", a, b);
a = b++; // a is 4, b is now 5,
printf("a=%d b=%d\n", a, b);
// Decrement operators (reset a back to 3)
b = 3;
printf("a=%d b=%d\n", a, b);
a = b--; // a is 3, b is now 2
printf("a=%d b=%d\n", a, b);
a = --b; // b is now 1, a is also 1
printf("a=%d b=%d\n", a, b);
return 0; // Exit from main
}

View File

@@ -1,14 +0,0 @@
#include <stdio.h>
int main() {
unsigned int a = 60;
unsigned int b = 13;
unsigned int r;
r = a & b;
printf("bitwise AND of 60 and 13: %d\n", r);
r = a | b;
printf("bitwise OR of 60 and 13: %d\n", r);
r = a ^ b;
printf("bitwise XOR of 60 and 13: %d\n", r);
}

View File

@@ -1,14 +0,0 @@
#include <stdio.h>
int main() {
unsigned int a = 0x60;
unsigned int b = 0x13;
unsigned int r;
r = a & b;
printf("bitwise AND of 0x60 and 0x13: 0x%02x\n", r);
r = a | b;
printf("bitwise OR of 0x60 and 0x13: 0x%02x\n", r);
r = a ^ b;
printf("bitwise XOR of 0x60 and 0x13: 0x%02x\n", r);
}

View File

@@ -1,22 +0,0 @@
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
// Declare some variables
int a, b;
// Note: Order of a,b indicates the order in which operations are carried out on execution
// Increment operators
b = 3;
a = ++b; // b is now 4, a is also 4
a = b++; // a is 4, b is now 5,
// Decrement operators (reset a back to 3)
b = 3;
a = b--; // a is 3, b is now 2
a = --b; // b is now 1, a is also 1
return 0; // Exit from main
}

View File

@@ -1,20 +0,0 @@
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
// declare variable
char c;
printf("Input a character: ");
// Wait for a kepypress, store result in c
c = getchar();
// Display on the screen using printf
printf ("The charcter pressed was %c\n", c);
// Display the charcter using putchar()
putchar(c);
return 0; // Exit from main
}

View File

@@ -1,28 +0,0 @@
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int a; // Declare some variables
float f;
// Use printf to prompt the use to enter an integer
printf ("Please enter an integer\n");
// use scanf with %d to read into 'a'
scanf ("%d",&a); // note the important &
// And display on the screen
printf ("The value you entered for a is %d\n", a);
// Use printf to prompt the use to enter an float
printf ("Please enter a float\n");
// use scanf with %f to read into 'f'
scanf ("%f",&f); // note the important &
// And display on the screen
printf ("The value you entered for f is %f\n", f);
return 0; // Exit from main
}

View File

@@ -1,28 +0,0 @@
#include <stdio.h>
int main(void)
{
// Declare some variables
int a,b,c;
float f,g;
// Use printf to prompt the use to enter 3 integers
printf ("Please enter three integer\n");
// use scanf with %d to read into a, b and c
scanf ("%d %d %d",&a, &b, &c); // note the important &
// And display on the screen
printf ("The values entered were %d %d %d\n", a, b, c);
// Use printf to prompt the use to enter an float
printf ("Please enter two floats\n");
// use scanf with %f to read into f ang g
scanf ("%f %f",&f, &g); // note the important &
// And display on the screen
printf ("The value you entered were %f and %f \n", f,g);
return 0; // Exit from main
}

View File

@@ -1,11 +0,0 @@
#include <stdio.h>
#include <math.h>
int main() {
float r;
printf("Enter radius: ");
scanf("%f", &r);
printf("Area of circle: %f\n", M_PI*r*r);
printf("Volume of sphere: %f\n", M_PI*r*r*r*4.0/3.0);
}

View File

@@ -1,13 +0,0 @@
#include <stdio.h>
#include <math.h>
int main() {
float radius, height;
printf("Enter radius: ");
scanf("%f", &radius);
printf("Enter height: ");
scanf("%f", &height);
printf("Surface area: %f\n", 2*M_PI*radius*radius + 2*M_PI*radius*height);
}

View File

@@ -1,11 +0,0 @@
#include <stdio.h>
int main() {
char name[200] = { 0 };
printf("What is your name? ");
scanf("%s", name);
printf("Hello %s\n", name);
}

View File

@@ -1,15 +0,0 @@
#include <stdio.h>
int main() {
char name[200] = { 0 };
int age = 0;
printf("What is your name? ");
scanf("%s", name);
printf("What is your age? ");
scanf("%ud", &age);
printf("Hello %s\nYou are %d years old\n", name, age);
}

View File

@@ -1,19 +0,0 @@
#include <stdio.h>
#include <stdlib.h>
int main()
{
// Declare variable - maximum 99 characters
char str[100];
// Prompt for text
printf("enter some text\n");
// Store intput in str
gets(str);
// Display a message on the screen
printf("you entered : %s\n", str);
// Exit from main
return 0;
}

View File

@@ -1,20 +0,0 @@
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
// Declare some variables
char name[50];
// Wait for a kepypress, store result in c
printf ("Please enter your name");
// read in using scanf with %s
scanf ("%s",name);
// Display on the screen using printf
printf ("Hello %s\n", name);
// Exit from main
return 0;
}

View File

@@ -1,14 +0,0 @@
#include <stdio.h>
int main() {
int input;
printf("Enter an integer: ");
scanf("%d", &input);
if (input >= 0 && input <= 10) {
printf("The number is in range\n");
}
return 0;
}

View File

@@ -1,14 +0,0 @@
#include <stdio.h>
int main() {
int input;
printf("Enter an integer: ");
scanf("%d", &input);
if (!(input >= 0 && input <= 10)) {
printf("The number is not in range\n");
}
return 0;
}

View File

@@ -1,21 +0,0 @@
#include <stdio.h>
int main() {
int age;
printf("Enter your age: ");
scanf("%d", &age);
// c only matches one branch so don't need to specify lower limits of each age range
if ( age <= 0 ) {
printf("Still a baby\n");
} else if ( age <= 12 ) {
printf("The junior years\n");
} else if ( age < 20 ) {
printf("Teenage years\n");
} else {
printf("Downhill all the way now!\n");
}
return 0;
}

View File

@@ -1,25 +0,0 @@
#include <stdio.h>
#include <math.h>
int main() {
int rc; // used to check return codes of scanf
float radius, height;
printf("Enter radius: ");
rc = scanf("%f", &radius);
// scanf returns number of scanned items. If input is invalid then 0 values will have been scanned.
if (rc != 1) {
printf("Please enter an positive real integer or decimal\n");
return 1;
}
printf("Enter height: ");
rc = scanf("%f", &height);
// scanf returns number of scanned items. If input is invalid then 0 values will have been scanned.
if (rc != 1) {
printf("Please enter an positive real integer or decimal\n");
return 1;
}
printf("Surface area: %f\n", 2*M_PI*radius*radius + 2*M_PI*radius*height);
}

View File

@@ -1,27 +0,0 @@
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
// Declare some variables
int a = 7, b=6;
printf("Enter a value for a: ");
scanf("%d", &a);
printf("Enter a value for b: ");
scanf("%d", &b);
// A single line of code conditional on the value of a
if ( a == 7 )
printf ("The value of a is 7 - so I will do this\n");
// Multiple lines of code conditional on b not equalling 4
// so then need to be placed inside { and }
if ( b != 4 )
{
printf ("The value of b is not 4\n");
printf ("So I will do multiple tasks\n");
}
return 0; // Exit from main
}

View File

@@ -1,29 +0,0 @@
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
// Declare some variables
int a = 1;
switch ( a )
{
case 0 :
printf ("Sunday\n");
break ; // end of the lines of code to execute
case 1:
printf ("Monday\n");
break ; // end of the lines of code to execute
case 2:
printf ("Tuesday\n");
break ; // end of the lines of code to execute
// etc...
default: // If no case is met (OPTIONAL)
printf ("\nThe value supplied is out of range\n");
}
return 0;
}

View File

@@ -1,25 +0,0 @@
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
// Declare some variables
int a = 1;
switch ( a )
{
case 1 :
case 2 :
case 3 :
// Code to do if a is 1, 2 or 3
break ; // end of the lines of code to execute
case 4:
case 5:
// Code to do if a is 4 or 5
break ; // end of the lines of code to execute
default: // If no case is met (OPTIONAL)
// Code to do if no case is met
}
return 0;
}

View File

@@ -1,23 +0,0 @@
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
// Declare some variables
int a = 1;
switch ( a )
{
case 1 :
printf ("This is case 1\n");
case 2 :
printf ("This is case 2\n");
case 3 :
printf ("This is case 3\n");
break ; // end of the lines of code to execute
default: // If no case is met
printf ("This default case\n");
// Code to do if no case is met
}
return 0;
}

View File

@@ -1,24 +0,0 @@
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int age; // Declare variable, no need to initialise this time
// as we read into it before it is tested against
// The loop is always entered as the test is at the end
do
{
// Code must be executed at least once
printf ("\nPlease enter your age");
scanf("%d", &age);
printf ("You are %d years old\n", age);
// Test is now made and the code
// repeats if the test equates as non-zerp
// (i.e. is age is not zero)
}
while ( age != 0);
return 0;
}

View File

@@ -1,23 +0,0 @@
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int age = 1; // Declare variable and initialise to 1
while ( age != 0) // Loop as long as age is not zero
{
// Code in {} executed if condition is true (non-zero)
printf ("\nPlease enter your age: ");
scanf("%d", &age);
printf ("You are %d years old\n", age);
if (age == 18 || age == 21) {
printf("You have come of age\n");
}
// Code now goes back and repeats the test with the value of age just entered
}
return 0; // Exit code
}

View File

@@ -1,14 +0,0 @@
#include <stdio.h>
#define MAX_NAME_LEN 200
int main() {
char name[MAX_NAME_LEN] = { 0 };
printf("Please enter your name: ");
scanf("%s", name);
while (1) {
printf("%s\n", name);
}
}

Some files were not shown because too many files have changed in this diff Show More