diff --git a/exercises/03_for_statement/README.md b/exercises/03_for_statement/README.md new file mode 100644 index 0000000..efb0ef8 --- /dev/null +++ b/exercises/03_for_statement/README.md @@ -0,0 +1 @@ +For more information please refer to Chapter 1.3 of "The C Programming Language". diff --git a/exercises/03_for_statement/for_statement_01.c b/exercises/03_for_statement/for_statement_01.c new file mode 100644 index 0000000..bc7c452 --- /dev/null +++ b/exercises/03_for_statement/for_statement_01.c @@ -0,0 +1,66 @@ +/** LEARNING + * Your goal is to understand how the for statement works. + * + * The for statement has the following syntax: + * + * for (initialization; condition; increment) { + * statement; + * statement; + * ... + * } + * + * The for statement is equivalent to the following while statement: + * + * initialization; + * while (condition) { + * statement; + * statement; + * ... + * increment; + * } + */ + +/** EXERCISE + * Your job is to write a for statement equivalent to the following + * while statement: + * + * i = 0; // initialization + * while (i < 10) { // condition + * printf("%d\n", i); + * verify_count(&count, i); + * i++; // increment + * } + */ + +// ❌ I AM NOT DONE + +#include +#include +#include + +// DO NOT CHANGE THIS +static int track = 0, count = 0; + +// DO NOT CHANGE THIS +void verify_count (int *count, int i) { + assert(i == track++); + (*count)++; +} + +int main() { + int i = rand() % 100 + 11; // DO NOT CHANGE THIS + + // YOUR CODE HERE + for (initialization; condition; increment) { + // END YOUR CODE + printf("%d\n", i); + verify_count(&count, i); // DO NOT CHANGE THIS + } + + // BONUS: print the value of i here and try to understand why it's 10 + + // DO NOT CHANGE THIS + assert (count == 10); + return 0; +} + diff --git a/exercises/03_for_statement/for_statement_02.c b/exercises/03_for_statement/for_statement_02.c new file mode 100644 index 0000000..f3617e9 --- /dev/null +++ b/exercises/03_for_statement/for_statement_02.c @@ -0,0 +1,46 @@ +/* + * The provided program generates a table of + * temperatures in Fahrenheit and their corresponding + * Celsius equivalents, increasing in steps of 20 degrees from + * 0°F to 300°F. The Celsius temperature is calculated directly + * within the printf statement using a for loop. + * + * Modify the provided temperature conversion + * C program to print the Fahrenheit to Celsius c + * onversion table in reverse order, + * from 0 degrees Fahrenheit up to 300 degrees. +*/ + +// ❌ I AM NOT DONE + +#include +#include + +#define TRACK 320 + +static int track = 0; + +// DO NOT CHANGE THIS +void verify_count (int i) { + assert(i == track); + track += 20; +} + +int main() { + // YOUR CODE HERE + for (int fahr = 300; fahr >= 0; fahr -= 20) { + // END YOUR CODE + printf("%3d %6.1f\n", fahr, (5.0 / 9.0) * (fahr - 32)); + + verify_count(fahr); // DO NOT CHANGE THIS + } + + // BONUS: print the value of track here + // and try to understand why it's 320 + + // DO NOT CHANGE THIS + assert (track == TRACK); + + return 0; +} +