diff --git a/examples/binary_search/binary_search.mc b/examples/binary_search/binary_search.mc new file mode 100644 index 0000000..dc6483a --- /dev/null +++ b/examples/binary_search/binary_search.mc @@ -0,0 +1,47 @@ +bool binary_search(int[20] arr, int val) +{ + int i; + i = 0; + + while(i < 20) + { + + if(arr[i] == val) + { + return true; + } + i = i + 1; + } + + return false; +} + +int main() +{ + int[20] arr; + + int i; + i = 0; + + while(i < 20) + { + arr[i] = i; + i = i+1; + } + + print("Please enter a number to search for: "); + + int n; + n = read_int(); + print_nl(); + + if (binary_search(arr,n)) { + print("Value was found!"); + } + else { + print("Value was not found!"); + } + print_nl(); + + return 0; +} diff --git a/examples/binary_search/binary_search.stdin.txt b/examples/binary_search/binary_search.stdin.txt new file mode 100644 index 0000000..60d3b2f --- /dev/null +++ b/examples/binary_search/binary_search.stdin.txt @@ -0,0 +1 @@ +15 diff --git a/examples/binary_search/binary_search.stdout.txt b/examples/binary_search/binary_search.stdout.txt new file mode 100644 index 0000000..15269e1 --- /dev/null +++ b/examples/binary_search/binary_search.stdout.txt @@ -0,0 +1,2 @@ +Please enter a number to search for: +Value was found! diff --git a/examples/empty/empty.mc b/examples/empty/empty.mc new file mode 100644 index 0000000..905869d --- /dev/null +++ b/examples/empty/empty.mc @@ -0,0 +1,4 @@ +int main() +{ + return 0; +} diff --git a/examples/empty/empty.stdin.txt b/examples/empty/empty.stdin.txt new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/examples/empty/empty.stdin.txt @@ -0,0 +1 @@ + diff --git a/examples/empty/empty.stdout.txt b/examples/empty/empty.stdout.txt new file mode 100644 index 0000000..e69de29 diff --git a/examples/fem/fem.mc b/examples/fem/fem.mc new file mode 100644 index 0000000..5ece9fd --- /dev/null +++ b/examples/fem/fem.mc @@ -0,0 +1,72 @@ +int main() +{ + float Vbat; + float Vc; + float dt; + float R; + float C; + int iter; + float t; + float Ic; + float dV; + + print("Battery voltage: "); + Vbat = read_float(); + + if (Vbat < 0.0) { + print("Please plug the battery in the right way"); + print_nl(); + return; + } + + print("Resistor value: "); + R = read_float(); + + print("Capictor value: "); + C = read_float(); + + if (R < 0.0 || C < 0.0) { + print("Part values must be positive"); + print_nl(); + return; + } + + print("Time step: "); + dt = read_float(); + + print("Number of iterations: "); + iter = read_int(); + + if (dt < 0.0 && iter < 0) { + dt = -dt; + iter = -iter; + print("IC what you did there"); + print_nl(); + } + + if (dt < 0.0 || iter < 0) { + print("Cowardly refusing to go backwards in time"); + print_nl(); + return; + } + + Vc = 0.0; + t = 0.0; + + while (iter > 0) { + print("Capacitor voltage after "); + print_float(t); + print("s: "); + print_float(Vc); + print_nl(); + + Ic = (Vbat - Vc) / R; + dV = (Ic * dt) / C; + + Vc = Vc + dV; + t = t + dt; + iter = iter - 1; + } + + return 0; +} diff --git a/examples/fem/fem.stdin.txt b/examples/fem/fem.stdin.txt new file mode 100644 index 0000000..24dca57 --- /dev/null +++ b/examples/fem/fem.stdin.txt @@ -0,0 +1,5 @@ +12 +1000 +0.000001 +0.0001 +25 diff --git a/examples/fem/fem.stdout.txt b/examples/fem/fem.stdout.txt new file mode 100644 index 0000000..c4732fa --- /dev/null +++ b/examples/fem/fem.stdout.txt @@ -0,0 +1,25 @@ +Battery voltage: Resistor value: Capictor value: Time step: Number of iterations: Capacitor voltage after 0.00s: 0.00 +Capacitor voltage after 0.00s: 1.20 +Capacitor voltage after 0.00s: 2.28 +Capacitor voltage after 0.00s: 3.25 +Capacitor voltage after 0.00s: 4.13 +Capacitor voltage after 0.00s: 4.91 +Capacitor voltage after 0.00s: 5.62 +Capacitor voltage after 0.00s: 6.26 +Capacitor voltage after 0.00s: 6.83 +Capacitor voltage after 0.00s: 7.35 +Capacitor voltage after 0.00s: 7.82 +Capacitor voltage after 0.00s: 8.23 +Capacitor voltage after 0.00s: 8.61 +Capacitor voltage after 0.00s: 8.95 +Capacitor voltage after 0.00s: 9.25 +Capacitor voltage after 0.00s: 9.53 +Capacitor voltage after 0.00s: 9.78 +Capacitor voltage after 0.00s: 10.00 +Capacitor voltage after 0.00s: 10.20 +Capacitor voltage after 0.00s: 10.38 +Capacitor voltage after 0.00s: 10.54 +Capacitor voltage after 0.00s: 10.69 +Capacitor voltage after 0.00s: 10.82 +Capacitor voltage after 0.00s: 10.94 +Capacitor voltage after 0.00s: 11.04 diff --git a/examples/leap_year/leap_year.mc b/examples/leap_year/leap_year.mc new file mode 100644 index 0000000..43e220b --- /dev/null +++ b/examples/leap_year/leap_year.mc @@ -0,0 +1,36 @@ +bool isLeapYear(int n) +{ + if ((modulo(n,4) == 0 && modulo(n,100) != 0) || (modulo(n,400) == 0)){ + return true; + } + return false; +} + +int modulo(int k, int i) +{ + while (k > 0){ + k = k - i; + } + return k; +} + +int main() +{ + print("Please enter a year: "); + print_nl(); + + int n; + n = read_int(); + + print_int(n); + if (isLeapYear(n)){ + print(" is a leap year."); + } + else + { + print(" is not a leap year."); + } + print_nl(); + + return 0; +} diff --git a/examples/leap_year/leap_year.stdin.txt b/examples/leap_year/leap_year.stdin.txt new file mode 100644 index 0000000..e0fbadd --- /dev/null +++ b/examples/leap_year/leap_year.stdin.txt @@ -0,0 +1 @@ +2020 diff --git a/examples/leap_year/leap_year.stdout.txt b/examples/leap_year/leap_year.stdout.txt new file mode 100644 index 0000000..4b1aa92 --- /dev/null +++ b/examples/leap_year/leap_year.stdout.txt @@ -0,0 +1,2 @@ +Please enter a year: +2020 is a leap year. diff --git a/examples/leibniz_pi/leibniz_pi.mc b/examples/leibniz_pi/leibniz_pi.mc new file mode 100644 index 0000000..d641765 --- /dev/null +++ b/examples/leibniz_pi/leibniz_pi.mc @@ -0,0 +1,37 @@ +float kth(float k) +{ + + return 1.0 / (2.0 * k + 1.0); +} + +int main() +{ + + print("Please enter a number: "); + + float sign; + float n; + float i; + float pi; + + i = 1.0; + n = read_float(); + print_nl(); + sign = 1.0; + pi = 1.0; + + while (i < n) + { + sign = sign * (-1.0); + pi = pi + sign * kth(i); + i = i + 1.0; + } + + pi = pi*4.0; + + print("Pi is approximately "); + print_float(pi); + print_nl(); + + return 0; +} diff --git a/examples/leibniz_pi/leibniz_pi.stdin.txt b/examples/leibniz_pi/leibniz_pi.stdin.txt new file mode 100644 index 0000000..f5c8955 --- /dev/null +++ b/examples/leibniz_pi/leibniz_pi.stdin.txt @@ -0,0 +1 @@ +32 diff --git a/examples/leibniz_pi/leibniz_pi.stdout.txt b/examples/leibniz_pi/leibniz_pi.stdout.txt new file mode 100644 index 0000000..4b64a05 --- /dev/null +++ b/examples/leibniz_pi/leibniz_pi.stdout.txt @@ -0,0 +1,2 @@ +Please enter a number: +Pi is approximately 3.11 diff --git a/examples/lucas_number/lucas_number.mc b/examples/lucas_number/lucas_number.mc new file mode 100644 index 0000000..d0858f2 --- /dev/null +++ b/examples/lucas_number/lucas_number.mc @@ -0,0 +1,30 @@ +void lucas_number (int x, int y, int num){ + int z; + z=0; + while(num >= x) + { + print_int(x); + print(", "); + z=x+y; + x=y; + y=z; + } + +} + +int main() +{ + int x; + int y; + int num; + print("Enter the limit of the Lucas number:"); + print_nl(); + x=2; + y=1; + num= read_int(); + print("The Lucas numbers are: "); + lucas_number(x, y, num); + print_nl(); + + return 0; +} diff --git a/examples/lucas_number/lucas_number.stdin.txt b/examples/lucas_number/lucas_number.stdin.txt new file mode 100644 index 0000000..d136d6a --- /dev/null +++ b/examples/lucas_number/lucas_number.stdin.txt @@ -0,0 +1 @@ +125 diff --git a/examples/lucas_number/lucas_number.stdout.txt b/examples/lucas_number/lucas_number.stdout.txt new file mode 100644 index 0000000..2e26068 --- /dev/null +++ b/examples/lucas_number/lucas_number.stdout.txt @@ -0,0 +1,2 @@ +Enter the limit of the Lucas number: +The Lucas numbers are: 2, 1, 3, 4, 7, 11, 18, 29, 47, 76, 123, diff --git a/examples/mandelbrot/mandelbrot.mc b/examples/mandelbrot/mandelbrot.mc new file mode 100644 index 0000000..fde05b6 --- /dev/null +++ b/examples/mandelbrot/mandelbrot.mc @@ -0,0 +1,72 @@ +float transform_x(float x) +{ + float fx; + + fx = x; + + return (-2.0) + ((1.0) - (-2.0)) * (fx / 80.0); +} + +float transform_y(float y) +{ + float fy; + + fy = y; + + return ((1.0) - ((1.0) - (-1.0)) * (fy / 50.0)) * 2.0; +} + +bool is_in_set(float pX, float pY) +{ + float temp; + float x; + float y; + int i; + + x = 0.0; + y = 0.0; + i = 0; + + while (((x * x + y * y) <= 4.0) && i < 30) { + temp = x * x - y * y + pX; + y = 2.0 * x * y + pY; + x = temp; + i = i + 1; + } + + if (i == 30) + return true; + + return false; +} + +int main() +{ + float fx; + float fy; + float x; + float y; + + y = 0.0; + + while (y < 50.0) { + x = 0.0; + + while (x < 80.0) { + fx = transform_x(x); + fy = transform_y(y); + x = x + 1.0; + + if (is_in_set(fx, fy)) { + print("."); + } else { + print(" "); + } + } + + print_nl(); + y = y + 1.0; + } + + return 0; +} diff --git a/examples/mandelbrot/mandelbrot.stdin.txt b/examples/mandelbrot/mandelbrot.stdin.txt new file mode 100644 index 0000000..e69de29 diff --git a/examples/mandelbrot/mandelbrot.stdout.txt b/examples/mandelbrot/mandelbrot.stdout.txt new file mode 100644 index 0000000..65fde27 --- /dev/null +++ b/examples/mandelbrot/mandelbrot.stdout.txt @@ -0,0 +1,50 @@ + + + + + + + + + + + + + . + + . + ....... + ....... + . ........ ... + ..................... ... + . ....................... + .......................... + ............................. + ......... .............................. + ............ .............................. + . .......................................... +............................................................. + . .......................................... + ............ .............................. + ......... .............................. + ............................. + .......................... + . ....................... + ..................... ... + . ........ ... + ....... + ....... + . + + . + + + + + + + + + + + diff --git a/examples/number_guessing/number_guessing.mc b/examples/number_guessing/number_guessing.mc new file mode 100644 index 0000000..ef13446 --- /dev/null +++ b/examples/number_guessing/number_guessing.mc @@ -0,0 +1,30 @@ +/* Test booleans and greater/smaller comparisons */ +void guessNumber(int number, int max) { + bool success; + success = false; + while(!success) { + print("Enter a number between 0 and "); + print_int(max); + print("\n"); + int guess; + guess = read_int(); + success = guess == number; + if(success) + print("you guessed correct"); + else { + if(guess < number) + print("the number is bigger\n"); + else + print("the number is smaller\n"); + } + } + print_nl(); +} + +int main() { + int test; + test = 3; + guessNumber(test, 10); + return 0; +} + diff --git a/examples/number_guessing/number_guessing.stdin.txt b/examples/number_guessing/number_guessing.stdin.txt new file mode 100644 index 0000000..cd92e18 --- /dev/null +++ b/examples/number_guessing/number_guessing.stdin.txt @@ -0,0 +1,4 @@ +5 +2 +4 +3 diff --git a/examples/number_guessing/number_guessing.stdout.txt b/examples/number_guessing/number_guessing.stdout.txt new file mode 100644 index 0000000..4336e0f --- /dev/null +++ b/examples/number_guessing/number_guessing.stdout.txt @@ -0,0 +1,8 @@ +Enter a number between 0 and 10 +the number is smaller +Enter a number between 0 and 10 +the number is bigger +Enter a number between 0 and 10 +the number is smaller +Enter a number between 0 and 10 +you guessed correct diff --git a/examples/prime/prime.mc b/examples/prime/prime.mc new file mode 100644 index 0000000..18bbf43 --- /dev/null +++ b/examples/prime/prime.mc @@ -0,0 +1,35 @@ +int is_prime(int n){ + + int i; + i = 2; + int mod; + while (i < n/2) + { + mod = n - (n/i)*i; + if(mod == 0){ + return 0; + } + i = i +1; + } + return 1; +} + +int main() +{ + print("Please enter a number: "); + + int n; + n = read_int(); + print_nl(); + + int result; + result = is_prime(n); + + print("prime("); + print_int(n); + print(") = "); + print_int(result); + print_nl(); + + return 0; +} diff --git a/examples/prime/prime.stdin.txt b/examples/prime/prime.stdin.txt new file mode 100644 index 0000000..b4de394 --- /dev/null +++ b/examples/prime/prime.stdin.txt @@ -0,0 +1 @@ +11 diff --git a/examples/prime/prime.stdout.txt b/examples/prime/prime.stdout.txt new file mode 100644 index 0000000..769ae17 --- /dev/null +++ b/examples/prime/prime.stdout.txt @@ -0,0 +1,2 @@ +Please enter a number: +prime(11) = 1 diff --git a/examples/sorting_network/sorting_network.mc b/examples/sorting_network/sorting_network.mc new file mode 100644 index 0000000..1e49aaf --- /dev/null +++ b/examples/sorting_network/sorting_network.mc @@ -0,0 +1,43 @@ +/* this example sorts 4 numbers like in an sorting network */ +void sort(int a, int b, int c, int d) { + int tmp; + if(a > c) { tmp = c; c = a; a = tmp; } + if(b > d) { tmp = d; d = b; b = tmp; } else {} + if(a > b) {tmp = b; b = a; a = tmp; } + if(c > d) { tmp = d; d = c; c = tmp; } + if(b > c) { tmp = c; c = b; b = tmp; } + print_int(a); print_int(b); print_int(c); print_int(d); print_nl(); +} + +int main() { + string info; info = "Enter a number which should be sorted within 1, 26 and 77:"; + print(info); print_nl(); + int a; a = read_int(); + int b; b = 26; + int c; c = 77; + int d; d = 1; + + { + { + sort(a, b, c, d); + sort(a, b, d, c); + sort(a, c, b, d); + sort(a, c, d, b); + sort(c, b, a, d); + sort(c, b, d, a); + sort(b, c, d, a); + sort(b, c, a, d); + sort(b, a, c, d); + sort(b, a, d, c); + sort(b, d, a, c); + sort(b, d, c, a); + sort(d, a, b, c); + sort(d, a, c, b); + sort(d, c, a, b); + sort(d, b, a, c); + /* .*./. */ + } + } + + return 0; +} diff --git a/examples/sorting_network/sorting_network.stdin.txt b/examples/sorting_network/sorting_network.stdin.txt new file mode 100644 index 0000000..98d9bcb --- /dev/null +++ b/examples/sorting_network/sorting_network.stdin.txt @@ -0,0 +1 @@ +17 diff --git a/examples/sorting_network/sorting_network.stdout.txt b/examples/sorting_network/sorting_network.stdout.txt new file mode 100644 index 0000000..27c60e7 --- /dev/null +++ b/examples/sorting_network/sorting_network.stdout.txt @@ -0,0 +1,17 @@ +Enter a number which should be sorted within 1, 26 and 77: +1172677 +1172677 +1172677 +1172677 +1172677 +1172677 +1172677 +1172677 +1172677 +1172677 +1172677 +1172677 +1172677 +1172677 +1172677 +1172677 diff --git a/examples/subset_sum/subset_sum.mc b/examples/subset_sum/subset_sum.mc new file mode 100644 index 0000000..54711af --- /dev/null +++ b/examples/subset_sum/subset_sum.mc @@ -0,0 +1,42 @@ +bool is_subset_sum(int[20] arr, int sum, int arr_size) +{ + if (sum == 0) { + return true; + } + if (arr_size == 0) { + return false; + } + if (arr[arr_size-1] > sum) { + return is_subset_sum(arr, sum, arr_size-1); + } + return is_subset_sum(arr, sum, arr_size-1) || + is_subset_sum(arr, sum-arr[arr_size-1], arr_size-1); +} + +int main() +{ + int n; + int[20] arr; + int sum; + n = 0; + while (n < 20) { + arr[n] = n; + n = n + 1; + } + print("Problem: Given the array [1, 2, ..., 20] and a natural number n"); + print_nl(); + print("is there a subset of the array which sum is equals to n?"); + print_nl(); + print("Input: n = "); + sum = read_int(); + print_nl(); + if (is_subset_sum(arr, sum, n+1)) { + print("Subset found with given sum!"); + } + else { + print("No subset found with given sum!"); + } + print_nl(); + + return 0; +} diff --git a/examples/subset_sum/subset_sum.stdin.txt b/examples/subset_sum/subset_sum.stdin.txt new file mode 100644 index 0000000..2bd5a0a --- /dev/null +++ b/examples/subset_sum/subset_sum.stdin.txt @@ -0,0 +1 @@ +22 diff --git a/examples/subset_sum/subset_sum.stdout.txt b/examples/subset_sum/subset_sum.stdout.txt new file mode 100644 index 0000000..58d7a1d --- /dev/null +++ b/examples/subset_sum/subset_sum.stdout.txt @@ -0,0 +1,4 @@ +Problem: Given the array [1, 2, ..., 20] and a natural number n +is there a subset of the array which sum is equals to n? +Input: n = +Subset found with given sum! diff --git a/examples/taylor/taylor.mc b/examples/taylor/taylor.mc new file mode 100644 index 0000000..52c4b08 --- /dev/null +++ b/examples/taylor/taylor.mc @@ -0,0 +1,42 @@ +float taylor_approx(int n) { + float signed_one; + signed_one = 1.0; + float pi; + pi = 0.0; + int i; + i = 1; + float j; + j = 1.0; + + /* summation loop i goes from 1 to n */ + while(i <= n) { + /* add next term to sum */ + pi = pi + (signed_one/((2.0*j) * (2.0*j+1.0) *(2.0*j+2.0))); + + /* reverse sign for next term */ + signed_one = -signed_one; + i = i + 1; + j = j + 1.0; + } + + /* finishing touches */ + pi = 4.0 * pi + 3.0; + + return pi; +} + +int main() { + print("Enter a number to obtain the n Taylor series for pi: "); + int n; + n = read_int(); + print_nl(); + float pi; + + pi = taylor_approx(n); + + print("Aproximation of pi: "); + print_float(pi); + print_nl(); + + return 0; +} diff --git a/examples/taylor/taylor.stdin.txt b/examples/taylor/taylor.stdin.txt new file mode 100644 index 0000000..b8626c4 --- /dev/null +++ b/examples/taylor/taylor.stdin.txt @@ -0,0 +1 @@ +4 diff --git a/examples/taylor/taylor.stdout.txt b/examples/taylor/taylor.stdout.txt new file mode 100644 index 0000000..9a2e6fc --- /dev/null +++ b/examples/taylor/taylor.stdout.txt @@ -0,0 +1,2 @@ +Enter a number to obtain the n Taylor series for pi: +Aproximation of pi: 3.14