forked from danwritecode/clings
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add Exercise 3: Reverse Temperature Converter for Chapter 1.3 (danwri…
- Loading branch information
Showing
3 changed files
with
113 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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". |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|