Skip to content

Commit

Permalink
add Exercise 3: Reverse Temperature Converter for Chapter 1.3 (danwri…
Browse files Browse the repository at this point in the history
  • Loading branch information
7etsuo committed Jun 8, 2024
1 parent ac6dfff commit 3b4b151
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 0 deletions.
1 change: 1 addition & 0 deletions exercises/03_for_statement/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
For more information please refer to Chapter 1.3 of "The C Programming Language".
66 changes: 66 additions & 0 deletions exercises/03_for_statement/for_statement_01.c
Original file line number Diff line number Diff line change
@@ -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 <stdio.h>
#include <stdlib.h>
#include <assert.h>

// 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;
}

46 changes: 46 additions & 0 deletions exercises/03_for_statement/for_statement_02.c
Original file line number Diff line number Diff line change
@@ -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 <stdio.h>
#include <assert.h>

#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;
}

0 comments on commit 3b4b151

Please sign in to comment.