This commit is contained in:
2025-07-27 19:34:54 +02:00
commit ea5a29c918
133 changed files with 2724 additions and 0 deletions

2
2023/Makefile Normal file
View File

@@ -0,0 +1,2 @@
yep:
clang arrays_of_structs.c -o arrays_of_structs && ./arrays_of_structs

BIN
2023/a.out Executable file

Binary file not shown.

View File

@@ -0,0 +1 @@
This repo is to document my progress working through the book "Algorithmic Thinking".

Binary file not shown.

View File

@@ -0,0 +1,30 @@
#include <stdio.h>
#define MAX_LINES 100
int main()
{
int i, j, num_lines, num_new_people;
int lines[MAX_LINES];
scanf("%d%d", &num_lines, &num_new_people);
for (i = 0; i < num_lines; i++)
{
scanf("%d", &lines[i]);
}
int smallest;
int smallest_index = -1;
for (i = 0; i < num_new_people; i++)
{
smallest = 101;
for (j = 0; j < num_lines; j++)
{
if (lines[j] < smallest) {
smallest = lines[j];
smallest_index = j;
}
}
printf("%d\n", smallest);
lines[smallest_index] += 1;
}
return 0;
}

View File

@@ -0,0 +1,6 @@
build:
clang snowflakes.c -o snowflakes
run:
./snowflakes < snowflakes.txt
go:
clang snowflakes.c -o snowflakes && ./snowflakes < snowflakes.txt

View File

@@ -0,0 +1,11 @@
- demonstrate segfault when you iterate beyond the end of an array
- make sure `num_snowflakes` is on the stack, and that it goes out of scope after you return from get_num_snowflakes_from_stdin
- print the memory address, for example
- is snowflakes really empty after declaration?
- why do we have to create `snowflake` using malloc? its size is predictable at compile time, so why can't we use the stack?
# Later
- how does `bool` work in C? The definitions are simple enough but what about the type?
- does clang put "0"s in a declared array?
- make the Snowflakes linked list, but putting the latest Entry at the end instead of the beginning
- make the Snowflakes linked list, but putting the latest Entry in the middle (?)

View File

@@ -0,0 +1,33 @@
#include <stdio.h>
typedef struct Node
{
int value;
struct Node * next;
} Node;
int main()
{
Node nodes[5];
for (int i = 0; i < 5; i++)
{
nodes[i].value = i + 42;
if (i > 0)
{
nodes[i - 1].next = &nodes[i];
}
}
for (int i = 0; i < 5; i++)
{
printf("node at %d has value %d\n", i, nodes[i].value);
if (i > 0)
{
printf("node at %d has value %d\n", i - 1, nodes[i - 1].value);
printf(
"that node's 'next' is %p and, as expected, its 'next' has value %d\n",
nodes[i - 1].next, nodes[i - 1].next->value
);
}
}
return 0;
}

View File

@@ -0,0 +1,28 @@
#include <stdlib.h>
#include <stdio.h>
typedef struct Node
{
int value;
struct Node * next;
} Node;
int main(void)
{
Node nodes[5];
for (int i = 0; i < 5; i++)
{
Node curr_node = { i };
nodes[i] = curr_node;
printf("Node in ARR current after insertion: %d\n", nodes[i].value);
printf("Node _address_ in ARR current after insertion: %p\n", &nodes[i]);
}
for (int i = 0; i < 5; i++)
{
printf("Node in ARR: %d\n", nodes[i].value);
}
return 0;
}

View File

@@ -0,0 +1,29 @@
#include <stdlib.h>
#include <stdio.h>
typedef struct Node
{
int value;
struct Node * next;
} Node;
int main(void)
{
Node *nodes[5];
for (int i = 0; i < 5; i++)
{
Node * curr_node = malloc(sizeof(Node));
curr_node->value = i;
nodes[i] = curr_node;
printf("Node in ARR current after insertion: %d\n", nodes[i]->value);
printf("Node _address_ in ARR current after insertion: %p\n", &nodes[i]);
}
for (int i = 0; i < 5; i++)
{
printf("Node in ARR: %d\n", nodes[i]->value);
}
return 0;
}

View File

@@ -0,0 +1,31 @@
#include <stdio.h>
#include <stdlib.h>
#define ARRAY_LEN 1047505
typedef struct Node
{
int value;
struct Node * next;
} Node;
Node * make_node(int value)
{
Node * node = malloc(sizeof(Node));
node->value = value;
return node;
}
int main()
{
Node * nodes[ARRAY_LEN];
for (int i = 0; i < ARRAY_LEN; i++)
{
nodes[i] = make_node(i);
}
for (int i = 0; i < ARRAY_LEN; i++)
{
printf("Node in ARR: %d\n", nodes[i]->value);
}
return 0;
}

Binary file not shown.

View File

@@ -0,0 +1,147 @@
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#define STRING_THERE_ARE_DUPLICATE_SNOWFLAKES "Twin snowflakes found.\n"
#define STRING_SNOWFLAKES_ARE_UNIQUE "No two snowflakes are alike.\n"
#define LEN_SNOWFLAKE 6
#define LARGEST_POSSIBLE_SUM 6000
int get_num_snowflakes_from_stdin(void)
{
int num_snowflakes; // this is allocated on the stack
scanf("%d", &num_snowflakes);
return num_snowflakes;
}
bool they_are_equal_right(int sf1[LEN_SNOWFLAKE], int sf2[LEN_SNOWFLAKE], int sf2_offset)
{
int sf1_tip, sf2_tip, sf2_index;
for (int i = 0; i < LEN_SNOWFLAKE; i++)
{
sf1_tip = sf1[i];
sf2_index = (i + sf2_offset) % LEN_SNOWFLAKE;
sf2_tip = sf2[sf2_index];
if (sf1_tip != sf2_tip)
return false;
}
return true;
}
bool they_are_equal_left(int sf1[LEN_SNOWFLAKE], int sf2[LEN_SNOWFLAKE], int sf2_offset)
{
int sf1_tip, sf2_tip, sf2_index;
for (int i = 0; i < LEN_SNOWFLAKE; i++)
{
sf1_tip = sf1[i];
sf2_index = sf2_offset - i;
if (sf2_index < 0)
sf2_index = sf2_index + LEN_SNOWFLAKE;
sf2_tip = sf2[sf2_index];
if (sf1_tip != sf2_tip)
return false;
}
return true;
}
bool they_are_equal(int sf1[LEN_SNOWFLAKE], int sf2[LEN_SNOWFLAKE])
{
for (int o = 0; o < LEN_SNOWFLAKE; o++)
{
if (they_are_equal_right(sf1, sf2, o))
return true;
if (they_are_equal_left(sf1, sf2, o))
return true;
}
return false;
}
int get_hash(int snowflake[LEN_SNOWFLAKE])
{
int sum = 0;
for (int i = 0; i < LEN_SNOWFLAKE; i++)
{
sum += snowflake[i];
}
return sum;
}
typedef struct Entry {
int snowflake[LEN_SNOWFLAKE];
struct Entry * next;
} Entry;
void populate_snowflakes_from_stdin(Entry * snowflakes[LARGEST_POSSIBLE_SUM], int indices_to_check[LARGEST_POSSIBLE_SUM], int num_snowflakes)
{
int hash;
Entry * snowflake;
int indices_index = 0;
for (int _ = 0; _ < num_snowflakes; _++)
{
snowflake = malloc(sizeof(Entry));
for (int j = 0; j < LEN_SNOWFLAKE; j++)
{
scanf("%d", &snowflake->snowflake[j]);
}
hash = get_hash(snowflake->snowflake);
if (snowflakes[hash] != 0)
{
indices_to_check[indices_index] = hash;
indices_index++;
}
// snowflakes[hash] is 0 at the terminus
snowflake->next = snowflakes[hash];
snowflakes[hash] = snowflake;
}
}
int main(void)
{
int num_snowflakes = get_num_snowflakes_from_stdin();
Entry * snowflakes[LARGEST_POSSIBLE_SUM]; // 48MB of memory, 6 million slots
int indices_to_check[LARGEST_POSSIBLE_SUM];
populate_snowflakes_from_stdin(snowflakes, indices_to_check, num_snowflakes);
int prev_idx;
int idx_to_check;
for (int i = 0; i < LARGEST_POSSIBLE_SUM; i++)
{
idx_to_check = indices_to_check[i];
if (idx_to_check == 0)
break;
if (idx_to_check == prev_idx)
continue;
printf("at index %d of indices_to_check => %d\n", i, indices_to_check[i]);
Entry * sf = snowflakes[idx_to_check];
Entry * next;
while (sf->next)
{
next = sf->next;
if (they_are_equal(sf->snowflake, next->snowflake))
{
printf(STRING_THERE_ARE_DUPLICATE_SNOWFLAKES);
return 0;
}
}
prev_idx = idx_to_check;
}
// for (int i = 0; i < num_snowflakes; i++)
// for (int j = i + 1; j < num_snowflakes; j++)
// if (they_are_equal(snowflakes[i], snowflakes[j]))
// {
// printf(STRING_THERE_ARE_DUPLICATE_SNOWFLAKES);
// return 0;
// }
// printf(STRING_SNOWFLAKES_ARE_UNIQUE);
return 0;
}

View File

@@ -0,0 +1,25 @@
Two snowflakes are identical if they are the same,
if we can make them the same by moving rightward
through one of the snowflakes (moving clockwise),
or if we can make them the same by moving leftward
through one of the snowflakes (moving counterclockwise).
# Input
The first line of input is an integer n,
the number of snowflakes that well be processing.
The value n will be between 1 and 100,000.
Each of the following n lines represents one snowflake:
each line has six integers, where each integer is
at least 0 and at most 10,000,000.
# Output
Our output will be a single line of text:
If there are no identical snowflakes, output exactly
"No two snowflakes are alike."
If there are at least two identical snowflakes, output exactly
"Twin snowflakes found."
The time limit for solving the test cases is one second.

View File

@@ -0,0 +1,8 @@
4
6 5 4 3 2 1
4 6 5 1 2 3
1 2 10 2 1 5
15 12 12 12 12 12
4 5 6 1 2 3
1 1 1 1 1 1

BIN
2023/arrays_of_structs Executable file

Binary file not shown.

16
2023/arrays_of_structs.c Normal file
View File

@@ -0,0 +1,16 @@
#include <stdio.h>
#include <stdlib.h>
typedef struct MyStruct {
char name[10];
} MyStruct;
int main()
{
MyStruct * structs_p = malloc(5 * sizeof(MyStruct));
MyStruct &structs_p[0] = { "Sylvia" };
printf("structs[4] = '%s'\n", structs[4].name);
printf("pointer for structs[4] = '%p'\n", &structs[4].name);
return 0;
}

8
2023/assert.c Normal file
View File

@@ -0,0 +1,8 @@
#include <stdio.h>
int main()
{
int age = 43;
assert(age == 44);
return 0;
}

Binary file not shown.

View File

@@ -0,0 +1,10 @@
#include <stdio.h>
#define MESSAGE_WITH_A_LENGTH_GREATER_THAN_31_HOPEFULLY "Hi Mom!\n"
int main(void)
{
int a_number_whose_var_name_has_quite_a_long_length_wouldnt_you_say = 42;
printf(MESSAGE_WITH_A_LENGTH_GREATER_THAN_31_HOPEFULLY);
printf("%d\n", a_number_whose_var_name_has_quite_a_long_length_wouldnt_you_say);
return 0;
}

Binary file not shown.

View File

@@ -0,0 +1,17 @@
#include <stdio.h>
#include <limits.h>
#include <float.h>
int main(void) {
printf("variables of type char store values from %d to %d\n", CHAR_MIN, CHAR_MAX);
printf("variables of type unsigned char store values from 0 to %d\n", UCHAR_MAX);
printf("variables of type short store values from %d to %d\n", SHRT_MIN, SHRT_MAX);
printf("variables of type unsigned short store values from 0 to %d\n", USHRT_MAX);
printf("variables of type int store values from %d to %d\n", INT_MIN, INT_MAX);
printf("variables of type unsigned int store values from 0 to %d\n", UINT_MAX);
printf("variables of type long store values from %ld to %ld\n", LONG_MIN, LONG_MAX);
printf("variables of type unsigned long store values from 0 to %lu\n", ULONG_MAX);
printf("variables of type long long store values from %lld to %lld\n", LLONG_MIN, LLONG_MAX);
printf("variables of type unsigned long long store values from 0 to %llu\n", ULLONG_MAX);
printf("\nthe size of the smallest positive non-zero value of type float is %.3e\n", FLT_MIN);
}

View File

@@ -0,0 +1,46 @@
#include <stdio.h>
#include <sys/_types/_size_t.h>
/* Write a program that will generate a multiplication table of a size entered by the user.
A table of size 4, for instance, would have four rows and four columns.
The rows and columns would be labeled from 1 to 4.
Each cell in the table will contain the product of the corresponding row and column numbers,
so the value in the position corresponding to the third row and the fourth column would contain 12.*/
void print_column_labels(int size_of_table) {
printf(" ");
for (int i=0; i<size_of_table; i++) {
printf("%d ", i + 1);
}
printf("\n");
}
int main(void)
{
unsigned int size_of_table;
printf("How large of a multiplication table would you like?\n");
scanf("%d", &size_of_table);
print_column_labels(size_of_table);
unsigned int prev_prod = 0;
unsigned int prod = 0;
for (unsigned int i=0; i<size_of_table; i++) {
for (unsigned int j=0; j<size_of_table; j++) {
if (j == 0) {
printf("%d", i + 1);
}
prod = (i + 1) * (j + 1);
if (prev_prod > 99) {
printf("%d");
} else if (prev_prod > 9) {
printf(" %d", prod);
} else {
printf(" %d", prod);
}
prev_prod = prod;
}
printf("\n");
prev_prod = 0;
}
return 0;
}

View File

@@ -0,0 +1,20 @@
/*
Write a program that will output the printable characters for character code values from 0 to 127.
Output each character code along with its symbol with two characters to a line.
Make sure the columns are aligned.
(Hint: You can use the isgraph() function thats declared in ctype.h to determine when a character is printable.)”
*/
#include <ctype.h>
#include <stdio.h>
int main(void) {
for (int i=0; i<128; i++) {
if (isgraph(i)) {
printf("%d: %c\n", i, i);
} else {
printf("%d: %c\n", i, toascii(i));
}
}
return 0;
}

BIN
2023/get_index_of_letter Executable file

Binary file not shown.

View File

@@ -0,0 +1,9 @@
#include <stdio.h>
int main()
{
char a = 'a';
int char_code = a - 'a';
printf("the letter '%c' has char code %d\n", a, char_code);
return 1;
}

View File

@@ -0,0 +1,10 @@
#include <stdio.h>
#include <stdint.h>
int main()
{
uint64_t zero = 0;
uint64_t minus_one = zero - 1;
printf("%llu", minus_one);
return 0;
}

BIN
2023/get_num_digits_of_integer Executable file

Binary file not shown.

View File

@@ -0,0 +1,18 @@
#include <stdio.h>
#include <math.h>
int get_num_digits(int num)
{
int the_log_10 = log10(num);
printf("\nlog10 of %d is %d\n", num, the_log_10);
int num_digits = floor(the_log_10 + 1);
return num_digits;
}
int main()
{
int the_num = 42000;
int num_digits = get_num_digits(the_num);
printf("%d has %d digits\n\n", the_num, num_digits);
return 0;
}

BIN
2023/leetcode/maximum_subarray_53 Executable file

Binary file not shown.

View File

@@ -0,0 +1,21 @@
#include <stdio.h>
int find_largest_subarray_sum(int * nums, int size)
{
// find the subarray with the largest sum and return that sum
int largest_sum = 10 << 4;
printf("sizeof(int) = %lu\n", sizeof(int));
printf("largest_sum is starting out as %d\n", largest_sum);
}
int main()
{
int result;
int input1_size = 9;
int input1[9] = {-2, 1, -3, 4, -1, 2, 1, -5, 4};
result = find_largest_subarray_sum(input1, input1_size);
printf("result for input1 = %d\n", result);
printf("was hoping for 6 (the sum of {4, -1, 2, 1})\n");
}

405
2023/muratori/add/.gitignore vendored Normal file
View File

@@ -0,0 +1,405 @@
# globs
Makefile.in
*.userprefs
*.usertasks
config.make
config.status
aclocal.m4
install-sh
autom4te.cache/
*.tar.gz
tarballs/
test-results/
# Mac bundle stuff
*.dmg
*.app
# content below from: https://github.com/github/gitignore/blob/main/Global/macOS.gitignore
# General
.DS_Store
.AppleDouble
.LSOverride
# Icon must end with two \r
Icon
# Thumbnails
._*
# Files that might appear in the root of a volume
.DocumentRevisions-V100
.fseventsd
.Spotlight-V100
.TemporaryItems
.Trashes
.VolumeIcon.icns
.com.apple.timemachine.donotpresent
# Directories potentially created on remote AFP share
.AppleDB
.AppleDesktop
Network Trash Folder
Temporary Items
.apdisk
# content below from: https://github.com/github/gitignore/blob/main/Global/Windows.gitignore
# Windows thumbnail cache files
Thumbs.db
ehthumbs.db
ehthumbs_vista.db
# Dump file
*.stackdump
# Folder config file
[Dd]esktop.ini
# Recycle Bin used on file shares
$RECYCLE.BIN/
# Windows Installer files
*.cab
*.msi
*.msix
*.msm
*.msp
# Windows shortcuts
*.lnk
# content below from: https://github.com/github/gitignore/blob/master/VisualStudio.gitignore
## Ignore Visual Studio temporary files, build results, and
## files generated by popular Visual Studio add-ons.
##
## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore
# User-specific files
*.suo
*.user
*.userosscache
*.sln.docstates
# User-specific files (MonoDevelop/Xamarin Studio)
*.userprefs
# Build results
[Dd]ebug/
[Dd]ebugPublic/
[Rr]elease/
[Rr]eleases/
x64/
x86/
bld/
[Bb]in/
[Oo]bj/
[Ll]og/
# Visual Studio 2015/2017 cache/options directory
.vs/
# Uncomment if you have tasks that create the project's static files in wwwroot
#wwwroot/
# Visual Studio 2017 auto generated files
Generated\ Files/
# MSTest test Results
[Tt]est[Rr]esult*/
[Bb]uild[Ll]og.*
# NUNIT
*.VisualState.xml
TestResult.xml
# Build Results of an ATL Project
[Dd]ebugPS/
[Rr]eleasePS/
dlldata.c
# Benchmark Results
BenchmarkDotNet.Artifacts/
# .NET Core
project.lock.json
project.fragment.lock.json
artifacts/
# StyleCop
StyleCopReport.xml
# Files built by Visual Studio
*_i.c
*_p.c
*_h.h
*.ilk
*.meta
*.obj
*.iobj
*.pch
*.pdb
*.ipdb
*.pgc
*.pgd
*.rsp
*.sbr
*.tlb
*.tli
*.tlh
*.tmp
*.tmp_proj
*_wpftmp.csproj
*.log
*.vspscc
*.vssscc
.builds
*.pidb
*.svclog
*.scc
# Chutzpah Test files
_Chutzpah*
# Visual C++ cache files
ipch/
*.aps
*.ncb
*.opendb
*.opensdf
*.sdf
*.cachefile
*.VC.db
*.VC.VC.opendb
# Visual Studio profiler
*.psess
*.vsp
*.vspx
*.sap
# Visual Studio Trace Files
*.e2e
# TFS 2012 Local Workspace
$tf/
# Guidance Automation Toolkit
*.gpState
# ReSharper is a .NET coding add-in
_ReSharper*/
*.[Rr]e[Ss]harper
*.DotSettings.user
# JustCode is a .NET coding add-in
.JustCode
# TeamCity is a build add-in
_TeamCity*
# DotCover is a Code Coverage Tool
*.dotCover
# AxoCover is a Code Coverage Tool
.axoCover/*
!.axoCover/settings.json
# Visual Studio code coverage results
*.coverage
*.coveragexml
# NCrunch
_NCrunch_*
.*crunch*.local.xml
nCrunchTemp_*
# MightyMoose
*.mm.*
AutoTest.Net/
# Web workbench (sass)
.sass-cache/
# Installshield output folder
[Ee]xpress/
# DocProject is a documentation generator add-in
DocProject/buildhelp/
DocProject/Help/*.HxT
DocProject/Help/*.HxC
DocProject/Help/*.hhc
DocProject/Help/*.hhk
DocProject/Help/*.hhp
DocProject/Help/Html2
DocProject/Help/html
# Click-Once directory
publish/
# Publish Web Output
*.[Pp]ublish.xml
*.azurePubxml
# Note: Comment the next line if you want to checkin your web deploy settings,
# but database connection strings (with potential passwords) will be unencrypted
*.pubxml
*.publishproj
# Microsoft Azure Web App publish settings. Comment the next line if you want to
# checkin your Azure Web App publish settings, but sensitive information contained
# in these scripts will be unencrypted
PublishScripts/
# NuGet Packages
*.nupkg
# The packages folder can be ignored because of Package Restore
**/[Pp]ackages/*
# except build/, which is used as an MSBuild target.
!**/[Pp]ackages/build/
# Uncomment if necessary however generally it will be regenerated when needed
#!**/[Pp]ackages/repositories.config
# NuGet v3's project.json files produces more ignorable files
*.nuget.props
*.nuget.targets
# Microsoft Azure Build Output
csx/
*.build.csdef
# Microsoft Azure Emulator
ecf/
rcf/
# Windows Store app package directories and files
AppPackages/
BundleArtifacts/
Package.StoreAssociation.xml
_pkginfo.txt
*.appx
# Visual Studio cache files
# files ending in .cache can be ignored
*.[Cc]ache
# but keep track of directories ending in .cache
!*.[Cc]ache/
# Others
ClientBin/
~$*
*~
*.dbmdl
*.dbproj.schemaview
*.jfm
*.pfx
*.publishsettings
orleans.codegen.cs
# Including strong name files can present a security risk
# (https://github.com/github/gitignore/pull/2483#issue-259490424)
#*.snk
# Since there are multiple workflows, uncomment next line to ignore bower_components
# (https://github.com/github/gitignore/pull/1529#issuecomment-104372622)
#bower_components/
# RIA/Silverlight projects
Generated_Code/
# Backup & report files from converting an old project file
# to a newer Visual Studio version. Backup files are not needed,
# because we have git ;-)
_UpgradeReport_Files/
Backup*/
UpgradeLog*.XML
UpgradeLog*.htm
ServiceFabricBackup/
*.rptproj.bak
# SQL Server files
*.mdf
*.ldf
*.ndf
# Business Intelligence projects
*.rdl.data
*.bim.layout
*.bim_*.settings
*.rptproj.rsuser
# Microsoft Fakes
FakesAssemblies/
# GhostDoc plugin setting file
*.GhostDoc.xml
# Node.js Tools for Visual Studio
.ntvs_analysis.dat
node_modules/
# Visual Studio 6 build log
*.plg
# Visual Studio 6 workspace options file
*.opt
# Visual Studio 6 auto-generated workspace file (contains which files were open etc.)
*.vbw
# Visual Studio LightSwitch build output
**/*.HTMLClient/GeneratedArtifacts
**/*.DesktopClient/GeneratedArtifacts
**/*.DesktopClient/ModelManifest.xml
**/*.Server/GeneratedArtifacts
**/*.Server/ModelManifest.xml
_Pvt_Extensions
# Paket dependency manager
.paket/paket.exe
paket-files/
# FAKE - F# Make
.fake/
# JetBrains Rider
.idea/
*.sln.iml
# CodeRush personal settings
.cr/personal
# Python Tools for Visual Studio (PTVS)
__pycache__/
*.pyc
# Cake - Uncomment if you are using it
# tools/**
# !tools/packages.config
# Tabs Studio
*.tss
# Telerik's JustMock configuration file
*.jmconfig
# BizTalk build output
*.btp.cs
*.btm.cs
*.odx.cs
*.xsd.cs
# OpenCover UI analysis results
OpenCover/
# Azure Stream Analytics local run output
ASALocalRun/
# MSBuild Binary and Structured Log
*.binlog
# NVidia Nsight GPU debugger configuration file
*.nvuser
# MFractors (Xamarin productivity tool) working folder
.mfractor/
# Local History for Visual Studio
.localhistory/

BIN
2023/muratori/add/a.out Executable file

Binary file not shown.

10
2023/muratori/add/add.cpp Normal file
View File

@@ -0,0 +1,10 @@
int __declspec(noinline) add(int A, int B)
{
return A + B;
}
#pragma optimize("", off)
int main(int ArgCount, char **Args)
{
return add(1234, 5678);
}

View File

@@ -0,0 +1,44 @@
.section __TEXT,__text,regular,pure_instructions
.build_version macos, 14, 0 sdk_version 14, 0
.intel_syntax noprefix
.globl __Z3addii ## -- Begin function _Z3addii
.p2align 4, 0x90
__Z3addii: ## @_Z3addii
.cfi_startproc
## %bb.0:
push rbp
.cfi_def_cfa_offset 16
.cfi_offset rbp, -16
mov rbp, rsp
.cfi_def_cfa_register rbp
mov dword ptr [rbp - 4], edi
mov dword ptr [rbp - 8], esi
mov eax, dword ptr [rbp - 4]
add eax, dword ptr [rbp - 8]
pop rbp
ret
.cfi_endproc
## -- End function
.globl _main ## -- Begin function main
.p2align 4, 0x90
_main: ## @main
.cfi_startproc
## %bb.0:
push rbp
.cfi_def_cfa_offset 16
.cfi_offset rbp, -16
mov rbp, rsp
.cfi_def_cfa_register rbp
sub rsp, 16
mov dword ptr [rbp - 4], 0
mov dword ptr [rbp - 8], edi
mov qword ptr [rbp - 16], rsi
mov edi, 1234
mov esi, 5678
call __Z3addii
add rsp, 16
pop rbp
ret
.cfi_endproc
## -- End function
.subsections_via_symbols

BIN
2023/pointers Executable file

Binary file not shown.

23
2023/pointers.c Normal file
View File

@@ -0,0 +1,23 @@
#include <stdio.h>
#include <string.h>
int main()
{
int x;
int *px;
x = 42;
px = &x;
long px_copy;
px_copy = px;
printf("x = %d\n", x);
printf("px = %p\n", px);
printf("px_copy = %ld\n", px_copy);
printf("*px = %d\n", *px);
printf("incremented");
x++;
printf("x = %d\n", x);
printf("px = %p\n", px);
printf("*px = %d\n", *px);
return 0;
}

BIN
2023/shifting Executable file

Binary file not shown.

11
2023/shifting.c Normal file
View File

@@ -0,0 +1,11 @@
#include <stdint.h>
#include <stdio.h>
int main()
{
uint64_t one = 1;
uint64_t sixteen_times = one << 16; // this is hopefully 65_536 or so
uint64_t thirty_two_times = one << 32; // this is hopefully 65_536 or so
printf("\n%llu", sixteen_times);
printf("\n%llu\n", thirty_two_times);
}

BIN
2023/tiny_c_projects/greetings Executable file

Binary file not shown.

View File

@@ -0,0 +1,30 @@
#include <stdio.h>
#include <time.h>
int main(int num_args, char *args[])
{
time_t now;
int hour;
struct tm * clock = localtime(&now);
time(&now);
hour = clock->tm_hour;
printf("Good ");
if (hour < 12)
{
printf("morning");
} else if (hour < 17)
{
printf("afternoon");
} else {
printf("evening");
}
if (num_args < 2)
{
puts(", dude.\n");
} else {
printf(", %s\n", args[1]);
}
return 0;
}

BIN
2023/tiny_c_projects/pithy Executable file

Binary file not shown.

View File

@@ -0,0 +1,41 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define BSIZE 512
int main()
{
const char filename[] = "pithy.txt";
FILE *fp;
char buffer[BSIZE];
char *line_of_text, *entry;
int items;
fp = fopen(filename, "r");
if (fp == NULL)
{
fprintf(stderr, "Unable to open file %s\n", filename);
exit(1);
}
while (!feof(fp))
{
entry = (char *)malloc(sizeof(char) * strlen(buffer) + 1);
if (entry == NULL)
{
fprintf(stderr, "Unable to allocate memory\n");
exit(1);
}
line_of_text = fgets(buffer, BSIZE, fp);
if (line_of_text == NULL)
break;
strlcpy(entry, buffer, BSIZE);
printf("%d: %s", items, entry);
items++;
}
fclose(fp);
return 0;
}

View File

@@ -0,0 +1,5 @@
Once upon a time, there was a text file.
It was not a long text file, and it contained very little of any value.
One day there was a very long line and it exceeded 256 characters. That is a super long line! Oh well, maybe there will be something of value in this text file after all: A segfault-causing line of text which is greater than 256 chars long. Amazing! Oh well, I guess this wasn't quite long enough, so here's a longer line. Are we at 256 chars? My vim is configured to be so minimal that I can't even find this info out at a glance. 😝

Submodule 2023/tsoding/music_visualizer/music_visualizer added at 80c8257e7c

BIN
2025/a.out Executable file

Binary file not shown.

Binary file not shown.

View File

@@ -0,0 +1,14 @@
#include <stdio.h>
int main(void) {
printf("8 / 5 -> %d\n", 8 / 5);
printf("-8 / 5 -> %d\n", -8 / 5);
printf("8 / -5 -> %d\n", 8 / -5);
printf("-8 / -5 -> %d\n", -8 / -5);
printf("8 %% 5 -> %d\n", 8 % 5);
printf("-8 %% 5 -> %d\n", -8 % 5);
printf("8 %% -5 -> %d\n", 8 % -5);
printf("-8 %% -5 -> %d\n", -8 % -5);
return 0;
}

Binary file not shown.

View File

@@ -0,0 +1,8 @@
#include <stdio.h>
int main(void) {
float a = 1.235230f;
a++;
printf("a is %f\n", a);
return 0;
}

Binary file not shown.

View File

@@ -0,0 +1,31 @@
/*
write a program which asks the user to enter a two-digit number,
then prints the number with its digits reversed.
*/
#include <stdio.h>
int using_math(void) {
printf("enter a three digit number: ");
int entry;
scanf("%d", &entry);
int first_digit = entry / 100;
int last_digits = entry % 100;
int second_digit = last_digits / 10;
int third_digit = last_digits % 10;
printf("reversed: %d%d%d\n", third_digit, second_digit, first_digit);
return 0;
}
int using_scanf(void) {
printf("enter a nine digit number: ");
int first, second, third;
scanf("%1d%1d%1d", &first, &second, &third);
printf("reversed: %d%d%d\n", third, second, first);
return 0;
}
int main(void) {
using_scanf();
return 0;
}

Binary file not shown.

View File

@@ -0,0 +1,17 @@
#include <stdio.h>
/*
Write a program which calculates how many digits a number contains.
*/
int main(void) {
int num;
int num_digits = 1;
printf("enter a number: ");
scanf("%d", &num);
while (num > 9) {
num /= 10;
num_digits++;
}
printf("number of digits: %d\n", num_digits);
return 0;
}

View File

@@ -0,0 +1,101 @@
#include <stdio.h>
/*
find the closest flight
Departure Time Arrival Time
8:00am 10:16am
9:43am 11:52am
11:19am 1:31pm
12:47pm 3:00pm
2:00pm 4:08pm
3:45pm 5:55pm
7:00pm 9:20pm
9:45pm 11:58pm
*/
int min(int num1, int num2) {
return num1 < num2 ? num1 : num2;
}
int get_diff(int flight_time, int current_time) {
int diff = flight_time - current_time;
if (diff < 0) {
diff = -diff;
}
int flight_time_to_midnight = (24 * 60) - flight_time;
int mid_diff = flight_time_to_midnight + current_time;
return min(mid_diff, diff);
}
int main(void) {
printf("Enter a 24-hour time: ");
int hours, minutes;
scanf("%2d:%2d", &hours, &minutes);
int minutes_since_midnight = minutes + (hours * 60);
int smallest_diff = 60 * 24;
int diff;
char *departure_time;
char *arrival_time;
diff = get_diff(8 * 60, minutes_since_midnight);
if (diff < smallest_diff) {
smallest_diff = diff;
departure_time = "8:00am";
arrival_time = "10:16am";
}
diff = get_diff((9 * 60) + 43, minutes_since_midnight);
if (diff < smallest_diff) {
smallest_diff = diff;
departure_time = "9:43am";
arrival_time = "11:52am";
}
diff = get_diff((11 * 60) + 19, minutes_since_midnight);
if (diff < smallest_diff) {
smallest_diff = diff;
departure_time = "11:19am";
arrival_time = "1:31pm";
}
diff = get_diff((12 * 60) + 47, minutes_since_midnight);
if (diff < smallest_diff) {
smallest_diff = diff;
departure_time = "12:47pm";
arrival_time = "3:00pm";
}
diff = get_diff((14 * 60), minutes_since_midnight);
if (diff < smallest_diff) {
smallest_diff = diff;
departure_time = "2:00pm";
arrival_time = "4:08pm";
}
diff = get_diff((15 * 60) + 45, minutes_since_midnight);
if (diff < smallest_diff) {
smallest_diff = diff;
departure_time = "3:45pm";
arrival_time = "5:55pm";
}
diff = get_diff((19 * 60), minutes_since_midnight);
if (diff < smallest_diff) {
smallest_diff = diff;
departure_time = "7:00pm";
arrival_time = "9:20pm";
}
diff = get_diff((21 * 60) + 45, minutes_since_midnight);
if (diff < smallest_diff) {
smallest_diff = diff;
departure_time = "9:45pm";
arrival_time = "11:58pm";
}
printf("Closest departure time is %s, arriving at %s\n", departure_time, arrival_time);
return 0;
}

View File

@@ -0,0 +1,15 @@
If it's 2am then the closest flight is 9:45pm, not 8:00am because
it's 4.25 hours between 9:45pm and 2am, versus six hours for the latter.
With the following implementation,
```c
int get_diff(int flight_time, int current_time) {
int diff = flight_time - current_time;
return diff < 0 ? -diff : diff;
}
```
When `current_time` is 120 and `flight_time` is 8:00am -> 480, the difference is 360.
When `current_time` is 120 and `flight_time` is 9:45pm -> 1_305, the difference is 1_185, but what we'd like it to show is 255.

View File

@@ -0,0 +1,33 @@
#include <stdio.h>
int main(void) {
// find the largest and smallest numbers; use no more than four "if"s
int one, two, three, four;
printf("please enter four integers: ");
scanf("%d %d %d %d", &one, &two, &three, &four);
int smallest = one;
int largest = two;
if (one > two) {
smallest = two;
largest = one;
}
int smallest2 = three;
int largest2 = four;
if (three > four) {
smallest2 = four;
largest2 = three;
}
if (smallest2 < smallest) {
smallest = smallest2;
}
if (largest2 > largest) {
largest = largest2;
}
printf("the smallest is %d and the largest is %d\n", smallest, largest);
}

Binary file not shown.

View File

@@ -0,0 +1,12 @@
#include <stdio.h>
int main(void) {
int n;
printf("enter an integer and I'll print all the even squares between zero and it: ");
scanf("%d", &n);
int i, product;
for (i = 1, product = i * i; i <= n / 10, product % 2 == 0; i++, product = i * i) {
printf("%d\n", i * i);
}
return 0;
}

Binary file not shown.

View File

@@ -0,0 +1,17 @@
#include <stdio.h>
int main(void) {
printf("to what precision shall we calculate 'e'? " );
int n;
scanf("%d", &n);
float e = 1;
for (int i = 1; i < n; i++) {
float prod = 1;
for (int j = i; j > 1; j--) {
prod *= j;
}
e += 1 / prod;
}
printf("e is approximately %.22f\n", e);
return 0;
}

Binary file not shown.

View File

@@ -0,0 +1,10 @@
#include <stdio.h>
int main(void) {
int i = 9384;
do {
printf("%d ", i);
i /= 10;
} while (i > 0);
return 0;
}

Binary file not shown.

View File

@@ -0,0 +1,16 @@
#include <stdio.h>
#define PROMPT "Enter a number: "
int main(void) {
float num = 0;
float largest_num = num;
do {
printf(PROMPT);
scanf("%f", &num);
if (num > largest_num) {
largest_num = num;
}
} while (num > 0);
printf("The largest number entered was %f\n", largest_num);
}

Binary file not shown.

View File

@@ -0,0 +1,17 @@
// we did this in an earlier chapter but now want to do it for arbitrary numbers of digits
#include <stdio.h>
int main(void) {
int num;
printf("enter an integer of any length and I'll reverse the digits: ");
scanf("%d", &num);
int next_digit;
while (num > 0) {
next_digit = num - (num / 10 * 10);
printf("%d", next_digit);
num /= 10;
}
printf("\n");
return 0;
}

Binary file not shown.

View File

@@ -0,0 +1,14 @@
#include <stdio.h>
int main(void) {
int sum = 0;
for (int i = 0; i < 10; i++) {
if (i % 2) {
continue;
}
sum += i;
printf("%d\n", sum);
}
printf("%d\n", sum);
return 0;
}

Binary file not shown.

Binary file not shown.

View File

@@ -0,0 +1,19 @@
/*
Write a program which takes a first and last name entered by the user and displays the last name, first initial like "Smith, J."
*/
#include <stdio.h>
int main(void) {
char first_char, last_entered_char;
// skip leading spaces
while ((first_char = getchar()) == ' ');
// skip first word after the first letter
while ((last_entered_char = getchar()) != ' ');
while ((last_entered_char = getchar()) != '\n') {
printf("%c", last_entered_char);
}
printf(", %c.\n", first_char);
return 0;
}

Binary file not shown.

View File

@@ -0,0 +1,12 @@
#include <stdio.h>
#include <stdbool.h>
#define ENTER_KEY_NUM 10;
int main(void) {
while (true) {
char c = getchar();
printf("you typed '%d'\n", c);
}
return 0;
}

Binary file not shown.

View File

@@ -0,0 +1,19 @@
#include <stdio.h>
#include <stdbool.h>
#define ENTER_KEY_NUM 10;
int main(void) {
int i = 1;
while (true) {
if (i % 1000000 == 0) {
printf("press ENTER to continue...");
while (getchar() != 10) {
continue;
}
}
printf("%d\n", i);
i++;
}
return 0;
}

View File

@@ -0,0 +1,8 @@
i = 1
while True:
if i % 1_000_000 == 0:
print("press ENTER to continue...")
while input() != "":
continue
print(i)
i += 1

Binary file not shown.

View File

@@ -0,0 +1,36 @@
#include <stdio.h>
#include <stdbool.h>
/*
Write a program that evaluates an expression e.g.
Enter an expression: 1+2.5*3
Value of expression: 10.5
*/
int main(void) {
float total, rhs;
char operator;
printf("Enter an expression: ");
scanf("%f", &total);
while (true) {
scanf("%c", &operator);
if (operator == '\n') {
break;
}
scanf("%f", &rhs);
if (operator == '+') {
total += rhs;
} else if (operator == '-') {
total -= rhs;
} else if (operator == '*') {
total *= rhs;
} else {
total /= rhs;
}
}
printf("Value of expression: %.1f\n", total);
return 0;
}

Binary file not shown.

View File

@@ -0,0 +1,10 @@
#include <stdio.h>
int main(void) {
float f, frac_part;
printf("enter a fraction: ");
scanf("%f", &f);
frac_part = f - (int) f;
printf("frac_part: %f\n", frac_part);
return 0;
}

View File

@@ -0,0 +1,35 @@
#include <stdio.h>
int main(void) {
char c = '\1';
short s = 2;
int i = -3;
long m = 5;
float f = 6.5f;
double d = 7.5;
printf("c is %d\n", c);
printf("i is %d\n", i);
printf("c * i = %d\n", c * i);
printf("s is %d\n", c);
printf("m is %d\n", i);
printf("s + m = %d\n", c + i);
printf("f is %f\n", f);
printf("c is %d\n", c);
printf("f / c = %f\n", f / c);
printf("d is %f\n", d);
printf("s is %d\n", s);
printf("d / s = %lf\n", d / s);
printf("f is %f\n", f);
printf("d is %lf\n", d);
printf("f - d = %lf\n", f - d);
printf("f is %f\n", f);
printf("(int) f = %d\n", (int) f);
return 0;
}

Binary file not shown.

View File

@@ -0,0 +1,11 @@
#include <stdio.h>
int main(void) {
printf("sizeof(short): %zu\n", sizeof(short));
printf("sizeof(int): %zu\n", sizeof(int));
printf("sizeof(long): %zu\n", sizeof(long));
printf("sizeof(float): %zu\n", sizeof(float));
printf("sizeof(double): %zu\n", sizeof(double));
printf("sizeof(long double): %zu\n", sizeof(long double));
return 0;
}

Binary file not shown.

View File

@@ -0,0 +1,36 @@
#include <stdio.h>
/*
This is interesting. The compiler and linter give me warnings about implicit conversion to `long` when I assign a value to `n` above ~2_147_400_000. Meanwhile, if I stay at or below that value the results are as expected. However, somewhere above 2146923072 the sign flips:
i (46340) * i (46340) = 2147395600
breaking becauses we're about to overflow
This happens because, indeed, `i * i` _is_ less than `n`, but only because there's been an overflow.
The exact highest number for `short` is 2_147_483_647
What's also interesting is that I get a compilation warning if I assign a value one larger than the largest int, even if I use the type `long` or `long long` for `n`.
I conclude that the number of bits my machine uses to store an `int` is 32 bits: one bit for the sign, and 31 bits for the number:
2^31 = 2_147_483_648
*/
#define LARGEST_INT 2147483647
#define LARGEST_SHORT 32767
int main(void) {
long long n = LARGEST_INT + 1;
for (int i = 1; (i * i) < n; i++) {
if (i * i < 0) {
printf("breaking becauses we're about to overflow\n");
printf("the next product is %d x %d = %d\n", i, i, i * i);
break;
}
if (i * i <= n) {
printf("i (%d) * i (%d) = %d\n", i, i, i * i);
}
}
return 0;
}

Binary file not shown.

View File

@@ -0,0 +1,12 @@
#include <stdio.h>
typedef short Int8;
typedef int Int16;
typedef long Int32;
int main(void) {
Int32 a = 32000000000;
Int16 b = 5;
printf("a - b -> %ld\n", a - b);
return 0;
}

Binary file not shown.

View File

@@ -0,0 +1,17 @@
#include <stdio.h>
char to_upper(char a_char) {
if (a_char >= 97 && a_char <= 97 + 26) {
return a_char - 32;
}
return a_char;
}
int main(void) {
printf("give me a letter and I'll uppercase it: ");
char letter;
scanf("%c", &letter);
letter = to_upper(letter);
printf("here it is! %c\n", letter);
return 0;
}

Binary file not shown.

View File

@@ -0,0 +1,25 @@
#include <stdio.h>
#include <stdbool.h>
/*
Write a program that calculates the average word length for a sentence:
Enter a sentence: It was deja vu all over again.
Average word length: 3.4
*/
int main(void) {
printf("Enter a sentence: ");
float num_words = 0;
float num_chars = 0;
char next_char;
while ((next_char = getchar()) != '\n') {
if (next_char == ' ') {
num_words++;
} else {
num_chars++;
}
}
num_words++; // when the next char is '\n' we've finished one more word
printf("Average word length: %.1f\n", num_chars / num_words);
return 0;
}

Binary file not shown.

View File

@@ -0,0 +1,17 @@
#include <stdio.h>
#include <stdbool.h>
int main(void) {
bool bools[100] = {false};
bool multi_dimensional_bools[3][5] = {false};
for (size_t i = 0; i < 100; i++) {
printf("bools[%zu] => %b\n", i, bools[i]);
}
for (size_t i = 0; i < 3; i++) {
for (size_t j = 0; j < 5; j++) {
printf("multi_dimensional_bools[%zu][%zu] => %b\n", i, j, multi_dimensional_bools[i][j]);
}
}
return 0;
}

View File

@@ -0,0 +1,9 @@
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
int main(void) {
rand()
return 0;
}

View File

@@ -0,0 +1,9 @@
#include <stdio.h>
int main(void) {
int a[] = {[100] = 42};
printf("a[0] = %d\n", a[0]);
printf("a[99] = %d\n", a[99]);
printf("a[100] = %d\n", a[100]);
return 0;
}

Some files were not shown because too many files have changed in this diff Show More