Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reset plan #183

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions master_exercises_read_only/01_welcome/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Welcome to clings!

clings is an interactive course to help you learn C entirely through the terminal and your text editor.

Each exercise will due to 1 or both of the following reasons:
1. A compilation error
2. A runtime error

For each exercise, your mission is simple: fix the errors you encounter.

Once all errors have been fixed, the program will automatically move you to the next exercise.

Good luck and have fun!
44 changes: 44 additions & 0 deletions master_exercises_read_only/01_welcome/hello_world.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Welcome to clings!
// This first exercise is just to show you how things work.
//
// There are two ways an exercise could be failing:
// 1. Compilation Error
// 2. Runtime Error
//
// Your goal is to complete an exercise and get it to both compile and also to run with no errors
// while following the directions of the exercise.
//
// Once there are no errors and you're ready to move on, delete the "I AM NOT DONE" line.


// ❌ I AM NOT DONE


#include <stdio.h>

int main()
{
char msg[] = "\033[1;32m"
" _ _ \n"
" | (_) \n"
" ___| |_ _ __ __ _ ___ \n"
" / __| | | '_ \\ / _` / __|\n"
"| (__| | | | | | (_| \\__ \\\n"
" \\___|_|_|_| |_|\\__, |___/\n"
" __/ | \n"
" |___/ \n"
"\033[0m";

printf("%s\n", msg);

// Missing something here? What does the compiler say?
printf("Welcome to clings! \n")

// This is a segmentation fault
// It's added to demonstrate a runtime failure
// For now, just delete these lines to see a successful exercise completion
int *ptr = NULL;
*ptr = 42;

return 0;
}
1 change: 1 addition & 0 deletions master_exercises_read_only/02_variables/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
For more information please refer to Chapter 1.2 of "The C Programming Language".
15 changes: 15 additions & 0 deletions master_exercises_read_only/02_variables/variables_01.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
* Variables in C must be declared before they are used. Typically you will see them declared at the top of a function.
*
* Take a look at the program below and figure out what primitive datatype each variable should be and assign them accordingly.
*/

// ❌ I AM NOT DONE

int main() {
x = 10;
y = 1.0;
z = 'A';

return 0;
}
27 changes: 27 additions & 0 deletions master_exercises_read_only/02_variables/variables_02.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Arithmetic in C is pretty straightforward, though there are some gotchas.
*
* If you use integers for division for example, you may lose precision in the output.
*
* The program below is using integers and is thus failing the assertion for a certain level of precision.
*
* Make changes to the program so that precision is not lost.
*/

// ❌ I AM NOT DONE


#include <assert.h>
#include <math.h>

#define EPSILON 0.000001

int main() {
int x = 100.0;
int y = 9.0;
int z = x / y;

// DO NOT CHANGE THIS
assert(fabs(z - 11.111111) < EPSILON);
return 0;
}
24 changes: 24 additions & 0 deletions master_exercises_read_only/02_variables/variables_03.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* So what is a practical application of all of this?
*
* How about a program to convert from Fahrenheit to Celcius.
*
* The program below is not accurate enough, how can we fix that?
*/

// ❌ I AM NOT DONE


#include <assert.h>
#include <math.h>

#define EPSILON 0.000001

int main() {
int fahrenheit = 100;
int celcius = (5 / 9) * (fahrenheit - 32);

// DO NOT CHANGE THIS
assert(fabs(celcius - 37.777779) < EPSILON);
return 0;
}
1 change: 1 addition & 0 deletions master_exercises_read_only/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 master_exercises_read_only/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 master_exercises_read_only/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;
}

2 changes: 2 additions & 0 deletions master_exercises_read_only/04_symbolic_constants/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
For more information please refer to Chapter 1.3 of "The C Programming Language".

Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* In programming, hardcoding numbers directly into your code
* (often referred to as "magic numbers") can make the code less
* understandable and harder to maintain.
*
* Using symbolic constants can help alleviate these issues by
* providing meaningful names for these numbers.
*
* Below is a simple C program that prints a Fahrenheit-to-Celsius
* conversion table. The program currently uses magic numbers.
* Your task is to refactor this program to use symbolic constants.
*
* Example macro definition:
* #define PI 3.14159
*
* Example usage:
* double circumference = 2 * PI * radius;
*/

// ❌ I AM NOT DONE

#include <stdio.h>

// [TODO] DEFINE CONSTANTS LOWER, UPPER, AND STEP WITH APPROPRIATE VALUES.
#define LOWER // lower limit of the table
#define UPPER // upper limit of the table
#define STEP // step size between consecutive temperatures
// END OF THE CONSTANTS

int main() {
int fahr;

// [TODO] REPLACE THE MAGIC NUMBERS WITH THE SYMBOLIC CONSTANTS.
for (fahr = 0; fahr <= 300; fahr += 20) {
// END OF THE REPLACEMENT
printf("%3d %6.1f\n", fahr, (5.0/9.0)*(fahr-32));
}

return 0;
}

1 change: 1 addition & 0 deletions master_exercises_read_only/15_variable_names/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
For more information please refer to Chapter 2.1 of "The C Programming Language".
21 changes: 21 additions & 0 deletions master_exercises_read_only/15_variable_names/variable_names_01.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Variable naming seems pretty simple however there are some gotchas.
*
* The primary gotchas are: "reserved words" and "illegal characters".
*
* Take a look at the below exercise and identify the variables that are
* causing the compilation error.
*/

// ❌ I AM NOT DONE

int main() {
// [TODO] Identify the variable(s) that is causing the compilation error.
char foo1 = 'a';
float float = 1.0;
int bar = 1;
char 1baz = 'a';

return 0;
}

3 changes: 3 additions & 0 deletions master_exercises_read_only/16_data_types/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Data types

For more information please refer to Chapter 2.2 of "The C Programming Language".
48 changes: 48 additions & 0 deletions master_exercises_read_only/16_data_types/data_type_01.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* In this exercise, you will practice understanding data types and sizes in C.
* The basic data types in C include `char`, `int`, `float`, and `double`.
* You can also apply qualifiers like `short`, `long`, `signed`, and `unsigned` to these types,
* which affect their size and behavior. Understanding how these types and qualifiers
* work at a binary level is crucial for robust C programming.
*
* The provided file will not compile or run correctly until
* the user fixes the mistakes.
* (You can only delete/edit lines with // <-- Fix this line)
*/

// ❌ I AM NOT DONE

#include <assert.h>
#include <stdio.h>
#include <limits.h>
#include <stdbool.h>

// Note: Carefully observe the output to understand the underlying behavior caused by type limits and conversions.
// |char| <= |short| <= |int| <= |long| <= |long long|

int main() {
unsigned int negativeAsUnsigned = -5; // <-- Fix this kind of misuse

long long veryLargeNumber = 9223372036854775807;
printf("Maximum long long value: %l\n", veryLargeNumber); // <-- Fix this line

printf("Range of unsigned long long: 0 to %llu\n", ); // <-- Fix this line (use symbolic constant limits.h)
printf("Range of signed long long: %lld to %lld\n", ); // <-- Fix this line (use symbolic constant)
printf("Range of unsigned char: %d to %d\n", ); // <-- Fix this line (Write a number)
printf("Range of signed char: %d to %d\n", ); // <-- Fix this line (write a number)

double floatingPointNumber = 0.999999;
int integerRepresentation = floatingPointButNotReally; // <-- Fix this line (use floatingPointNumber)
printf("Double to int casting result: %d\n", integerRepresentation);

bool understand = false;
unsigned int plus_one = 1;
int minus_one = -1;

if(plus_one > minus_one) { // <-- Fix this line
understand = true;
}
assert(understand);

return 0;
}
3 changes: 3 additions & 0 deletions master_exercises_read_only/17_constants/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Constants

For more information please refer to Chapter 2.1 of "The C Programming Language".
Loading