Coffee Space


Listen:

Micro-Code

Preview Image

Preview Image

Background

These programs have been designed to be written on the back of a business card, with their output displayed on the front.

The following is the limitations on the design:

There are the following assumptions that are reasonable to make:

Pattern Test

Output

Pattern Test Image

Pattern Test Image

Numbers

  • With comments: 254 characters (not including newlines)
  • Without comments: 201 characters (not including newlines)

Code

#include<stdio.h>/*gcc a.c; ./a.out; eog b.ppm*/
main(){int w=256,h=256,k;FILE*f=fopen("b.ppm","w");fprintf(f,"P6\n%d %d\n255\n"
,w,h);for(;--h>=0;){for(k=w;k>=0;--k){char c[3]={h%256,(k*h)%256,k%256};fwrite(
c,1,3,f);}}fclose(f);}/*coffeespace.org.uk*/

Use

To compile:

gcc a.c

To run:

./a.out

To view:

eog b.ppm

Comments

This program scales well by changing what w and h equal, which are width and height respectively. Numbers that reflect 2^X display nicer than others.

Mandelbrot

Output

Mandelbrot Image

Mandelbrot Image

Numbers

  • Without comments: 291 characters (not including newlines)

Code

#include<stdio.h>
main(){int k=256,w,h=k,x,a=k/2;double b=a/2,i,j,y,z,t;FILE*f=fopen("b.ppm","w")
;fprintf(f,"P6\n%d %d\n255\n",k,k);for(;h--;)for(w=k;w--;){i=j=0;y=(w-a)/b;z=(h
-a)/b;x=k;for(;i*i+j*j<4&&x--;){t=i*i-j*j+y;j=2*i*j+z;i=t;}char c[3]={x<<4,x<<2
,x};fwrite(c,1,3,f);}fclose(f);}

Use

To compile:

gcc a.c

To run:

./a.out

To view:

eog b.ppm

Comments

The image produced is really good looking and well detailed with k = 256^X where X is greater than 1. It has been tested with k = 2^14 = 16385, on a 3rd generation i5 processor, taking about 3 or 4 hours. This program certainly comes under the category of "embarrassingly parallel" and could benefit from multi-core processing, as little RAM and disk speed is used and pixels can be calculated independently.

Difference

Output

FILE_1 (t1.txt)

Hello World!
1234567890
Difference
0123456789
hello

FILE_2 (t2.txt)

Hello World!
1234567890
difference
9876543210
hello
extra

Output

[0003]
t1.txt
Difference

t2.txt
difference

[0004]
t1.txt
0123456789

t2.txt
9876543210

[0006]
t2.txt
extra

Numbers

  • Without comments: 383 characters (not including newlines)

Code

#include<stdio.h>
main(int c,char**v){int n=512,i=1,j,d=i,e=i;char a[n];char b[n];FILE*y=fopen(v[
1],"r");FILE*z=fopen(v[2],"r");for(;d&&e;++i){d=fgets(a,n,y);e=fgets(b,n,z);for
(j=-1;++j<n&&a[j]==b[j];)if(a[j]==0)j=n;if(j<n){char c[7]={'[',(i/1000)+48,(i/
100%10)+48,(i/10%10)+48,(i%10)+48,']',0};puts(c);if(d){puts(v[1]);puts(a);}if(e
){puts(v[2]);puts(b);}}}fclose(y);fclose(z);}

Use

To compile:

gcc a.c

To run:

./a.out FILE_1 FILE_2

To view, read the terminal output.

Comments

This program is limited to a difference check of 9999 lines, after which the line numbering goes out of the window but it continues to check. This program simply checks the lines, one after another and checks whether they are the same or not. It's particularly useful if you need to check for a difference in a log for example, or a revision to a simple text file somebody has made.