Added book examples
This commit is contained in:
20
C4/correct_answer.c
Normal file
20
C4/correct_answer.c
Normal 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
13
C4/declare_a_string.c
Normal 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;
|
||||
}
|
4
C4/difficult_to_read_code.c
Normal file
4
C4/difficult_to_read_code.c
Normal 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
4
C4/poor_layout.c
Normal 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
19
C4/reformatted_code.c
Normal 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
8
C4/simple_claculation.c
Normal 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
8
C4/well_laid_out_code.c
Normal 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
17
C4/wrong_answer.c
Normal 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;
|
||||
}
|
Reference in New Issue
Block a user