Add example inputs

This commit is contained in:
Alex Hirsch 2019-01-06 12:11:47 +01:00
parent 0045501c5e
commit bd7fae8198
36 changed files with 624 additions and 0 deletions

View File

@ -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;
}

View File

@ -0,0 +1 @@
15

View File

@ -0,0 +1,2 @@
Please enter a number to search for:
Value was found!

4
examples/empty/empty.mc Normal file
View File

@ -0,0 +1,4 @@
int main()
{
return 0;
}

View File

@ -0,0 +1 @@

View File

72
examples/fem/fem.mc Normal file
View File

@ -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;
}

View File

@ -0,0 +1,5 @@
12
1000
0.000001
0.0001
25

View File

@ -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

View File

@ -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;
}

View File

@ -0,0 +1 @@
2020

View File

@ -0,0 +1,2 @@
Please enter a year:
2020 is a leap year.

View File

@ -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;
}

View File

@ -0,0 +1 @@
32

View File

@ -0,0 +1,2 @@
Please enter a number:
Pi is approximately 3.11

View File

@ -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;
}

View File

@ -0,0 +1 @@
125

View File

@ -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,

View File

@ -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;
}

View File

View File

@ -0,0 +1,50 @@
.
.
.......
.......
. ........ ...
..................... ...
. .......................
..........................
.............................
......... ..............................
............ ..............................
. ..........................................
.............................................................
. ..........................................
............ ..............................
......... ..............................
.............................
..........................
. .......................
..................... ...
. ........ ...
.......
.......
.
.

View File

@ -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;
}

View File

@ -0,0 +1,4 @@
5
2
4
3

View File

@ -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

35
examples/prime/prime.mc Normal file
View File

@ -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;
}

View File

@ -0,0 +1 @@
11

View File

@ -0,0 +1,2 @@
Please enter a number:
prime(11) = 1

View File

@ -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;
}

View File

@ -0,0 +1 @@
17

View File

@ -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

View File

@ -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;
}

View File

@ -0,0 +1 @@
22

View File

@ -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!

42
examples/taylor/taylor.mc Normal file
View File

@ -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;
}

View File

@ -0,0 +1 @@
4

View File

@ -0,0 +1,2 @@
Enter a number to obtain the n Taylor series for pi:
Aproximation of pi: 3.14