Added book examples

This commit is contained in:
Louise Brown
2022-04-08 16:12:38 +01:00
parent 5b205092a7
commit 1c0a9e7b6a
59 changed files with 1381 additions and 0 deletions

20
C4/correct_answer.c Normal file
View File

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

13
C4/declare_a_string.c Normal file
View File

@@ -0,0 +1,13 @@
#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

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

4
C4/poor_layout.c Normal file
View File

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

19
C4/reformatted_code.c Normal file
View File

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

8
C4/simple_claculation.c Normal file
View File

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

8
C4/well_laid_out_code.c Normal file
View File

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

17
C4/wrong_answer.c Normal file
View File

@@ -0,0 +1,17 @@
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
// Declare and initialiase as required
int a = 1,b = 4;
float ans;
ans = a / b; // Perform calculation
// Display answer
printf ("\nThe answer is %f",ans);
// Exit from program
return 0;
}