Add student example inputs

This commit is contained in:
Alex Hirsch 2019-03-18 13:50:36 +01:00
parent 480716b4a9
commit 14da3497ed
45 changed files with 1245 additions and 0 deletions

View File

@ -0,0 +1,28 @@
bool ageforalco(int n)
{
if (n < 21) {
return false;
}
return true;
}
int main()
{
print("Enter you age: ");
int age;
age = read_int();
print_nl();
bool result;
result = ageforalco(age);
if (!result) {
print("You Can't drink ):");
}
else {
print("You Can Drink!");
}
print_nl();
}

View File

@ -0,0 +1 @@
25

View File

@ -0,0 +1,2 @@
Enter you age:
You Can Drink!

View File

@ -0,0 +1,179 @@
void dijkstra(int[15] path_cost)
{
int[15] calc_cost;
int i;
int j;
i = 0;
while (i < 4)
{
j = 0;
while (j < 4)
{
if (path_cost[i * 4 + j] == 0)
calc_cost[i * 4 + j] = 9999;
else
calc_cost[i * 4 + j] = path_cost[i * 4 + j];
j = j + 1;
}
i = i + 1;
}
int[4] distance;
bool[4] visited;
int node_count;
int next_node;
node_count = 0;
while (node_count < 4)
{
distance[node_count] = calc_cost[node_count];
if (node_count == 0)
visited[node_count] = true;
else
visited[node_count] = false;
node_count = node_count + 1;
}
node_count = 1;
while (node_count < 4)
{
int min_distance;
int traversel_count;
min_distance = 9999;
traversel_count = 0;
while (traversel_count < 4)
{
if (!visited[traversel_count] &&
distance[traversel_count] < min_distance)
{
min_distance = distance[traversel_count];
next_node = traversel_count;
}
traversel_count = traversel_count + 1;
}
visited[next_node] = true;
traversel_count = 0;
while (traversel_count < 4)
{
if (!visited[traversel_count])
{
if (distance[traversel_count] >
calc_cost[next_node * 4 + traversel_count] + min_distance)
{
distance[traversel_count] =
calc_cost[next_node * 4 + traversel_count] + min_distance;
}
}
traversel_count = traversel_count + 1;
}
node_count = node_count + 1;
}
int print_counter;
print_counter = 1;
while (print_counter < 4)
{
print("Distance to node ");
print_int(print_counter);
print(" = ");
print_int(distance[print_counter]);
print_nl();
print_counter = print_counter + 1;
}
}
int main()
{
int[15] path_cost;
int input_counter;
int node_counter;
input_counter = 1;
node_counter = 1;
path_cost[0] = 0;
print("Enter the path costs ('0' = no path)");
print_nl();
while (input_counter < 15)
{
if (input_counter < 4)
{
print("Enter path cost from 0 -> ");
print_int(node_counter);
print(":");
print_nl();
path_cost[input_counter] = read_int();
}
if (input_counter >= 4 && input_counter < 8)
{
if (input_counter != 5)
{
print("Enter path cost from 1 -> ");
print_int(node_counter);
print(":");
print_nl();
path_cost[input_counter] = read_int();
}
else
{
path_cost[input_counter] = 0;
}
}
if (input_counter >= 8 && input_counter < 12)
{
if (input_counter != 10)
{
print("Enter path cost from 2 -> ");
print_int(node_counter);
print(":");
print_nl();
path_cost[input_counter] = read_int();
}
else
{
path_cost[input_counter] = 0;
}
}
if (input_counter >= 12)
{
if (input_counter != 15)
{
print("Enter path cost from 3 -> ");
print_int(node_counter);
print(":");
print_nl();
path_cost[input_counter] = read_int();
}
else
{
path_cost[input_counter] = 0;
}
}
if (node_counter >= 3)
node_counter = 0;
else
node_counter = node_counter + 1;
input_counter = input_counter + 1;
}
dijkstra(path_cost);
return 0;
}

View File

@ -0,0 +1,12 @@
10
30
70
0
10
40
0
0
10
0
0
0

View File

@ -0,0 +1,16 @@
Enter the path costs ('0' = no path)
Enter path cost from 0 -> 1:
Enter path cost from 0 -> 2:
Enter path cost from 0 -> 3:
Enter path cost from 1 -> 0:
Enter path cost from 1 -> 2:
Enter path cost from 1 -> 3:
Enter path cost from 2 -> 0:
Enter path cost from 2 -> 1:
Enter path cost from 2 -> 3:
Enter path cost from 3 -> 0:
Enter path cost from 3 -> 1:
Enter path cost from 3 -> 2:
Distance to node 1 = 10
Distance to node 2 = 20
Distance to node 3 = 30

45
examples/draw/draw.mc Normal file
View File

@ -0,0 +1,45 @@
void drawrect(int w, int h) {
int i;
int j;
i = 0;
while(i < w) {
print("x");
i = i + 1;
}
print_nl();
i = 2;
while(i < h) {
print("x");
j = 2;
while(j < w) {
print(" ");
j = j + 1;
}
print("x");
print_nl();
i = i + 1;
}
i = 0;
while(i < w) {
print("x");
i = i + 1;
}
print_nl();
}
int main() {
drawrect(10, 15);
drawrect(3, 5);
drawrect(50, 20);
drawrect(10, 10);
drawrect(2, 2);
return 0;
}

View File

View File

@ -0,0 +1,52 @@
xxxxxxxxxx
x x
x x
x x
x x
x x
x x
x x
x x
x x
x x
x x
x x
x x
xxxxxxxxxx
xxx
x x
x x
x x
xxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
x x
x x
x x
x x
x x
x x
x x
x x
x x
x x
x x
x x
x x
x x
x x
x x
x x
x x
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxx
x x
x x
x x
x x
x x
x x
x x
x x
xxxxxxxxxx
xx
xx

45
examples/euclid/euclid.mc Normal file
View File

@ -0,0 +1,45 @@
int euclid(int n, int k)
{
if (k == 0) {
return n;
}
if (n == 0) {
return k;
}
if (n > k) {
return euclid(n - k, k);
} else {
return euclid(n, k - n);
}
}
int main()
{
print("Please enter the first number: ");
print_nl();
int n;
n = read_int();
print("Please enter the second number: ");
print_nl();
int k;
k = read_int();
print_nl();
int result;
result = euclid(n, k);
print("euclid(");
print_int(n);
print(", ");
print_int(k);
print(") = ");
print_int(result);
print_nl();
return 0;
}

View File

@ -0,0 +1,2 @@
1071
1029

View File

@ -0,0 +1,4 @@
Please enter the first number:
Please enter the second number:
euclid(1071, 1029) = 21

View File

@ -0,0 +1,44 @@
/* Divide integer "divisor" by integer "dividend" and output the remainder */
int calculate_remainder(int divisor, int dividend) {
while (dividend > divisor) {
dividend = dividend - divisor;
}
return dividend;
}
/* The divisor must not be smaller than or equal to 0 */
int main() {
print("Enter Dividend:");
print_nl();
int dividend;
dividend = read_int();
print("Enter Divisor:");
print_nl();
int divisor;
divisor = read_int();
if (dividend < divisor) {
print("Dividend has to be bigger than divisor.");
print_nl();
} else {
int remainder;
remainder = calculate_remainder(divisor, dividend);
int result;
result = (dividend - remainder) / divisor;
print_int(dividend);
print(" is divisible by ");
print_int(divisor);
print(" with the remainder ");
print_int(remainder);
print(" and result ");
print_int(result);
print_nl();
}
return 0;
}

View File

@ -0,0 +1,2 @@
15
6

View File

@ -0,0 +1,3 @@
Enter Dividend:
Enter Divisor:
15 is divisible by 6 with the remainder 3 and result 2

View File

@ -0,0 +1,29 @@
int calculateFactorial(int number){
int factorial = 1;
int i = 1;
while (i <= number){
factorial *= i;
i = i + 1;
}
return factorial;
}
int main() {
print("Please enter a number: ");
int number;
number = read_int();
print_nl();
int result;
result = calculateFactorial(number);
print("The factorial of ");
print_int(number);
print(" is ");
print_int(result);
print_nl();
return 0;
}

View File

@ -0,0 +1 @@
5

View File

@ -0,0 +1,2 @@
Please enter a number:
The factorial of 5 is 120

98
examples/flw/flw.mc Normal file
View File

@ -0,0 +1,98 @@
void print_2_signals(float[64] signal1, float[64] signal2)
{
int i;
float j;
j = 1;
float dec;
dec = 1.0 / 16.0;
while (j > -0.001)
{
i = 0;
while (i < 64)
{
if (signal2[i] < j + 0.5 * dec && signal2[i] > j - 0.5 * dec)
{
print("o");
}
else if (signal1[i] < j + 0.5 * dec && signal1[i] > j - 0.5 * dec)
{
print("#");
}
else if (j < 0.5 * dec && j > -0.5 * dec)
{
print("-");
}
else
{
print(" ");
}
i = i + 1;
}
print_nl();
j = j - dec;
}
}
void make_gate(float[64] signal)
{
int i;
i = 0;
while (i < 64)
{
if (i < 32)
{
signal[i] = 1.0;
}
else
{
signal[i] = 0.0;
}
i = i + 1;
}
}
void follower(float[64] in, float[64] out, float a, float r)
{
float p;
p = 0.0;
int i;
i = 0;
while (i < 64)
{
if (in[i] > p)
{
p = a * p + (1.0 - a) * in[i];
}
else
{
p = r * p + (1.0 - r) * in[i];
}
out[i] = p;
i = i + 1;
}
}
int main()
{
print("Enter Envelope Attack value between 0 and 1 (infinity): ");
float a;
a = read_float();
print_nl();
print("Enter Envelope Release value between 0 and 1 (infinity): ");
float r;
r = read_float();
print_nl();
print("Resulting Envelope Follower:");
print_nl();
print_nl();
float[64] gate;
make_gate(gate);
float[64] envelope;
follower(gate, envelope, a, r);
print_2_signals(gate, envelope);
return 0;
}

View File

@ -0,0 +1,2 @@
0.9
0.9

View File

@ -0,0 +1,21 @@
Enter Envelope Attack value between 0 and 1 (infinity):
Enter Envelope Release value between 0 and 1 (infinity):
Resulting Envelope Follower:
################################
oooooooooo
ooooo o
ooo o
oo
oo o
oo o
o o
o o
oo oo
o o
oo
o ooo
o ooo
o ooooo
oooooooooo
--------------------------------################################

43
examples/gcd/gcd.mc Normal file
View File

@ -0,0 +1,43 @@
/* calulcate modulo */
int mod(int a, int b){
return a-(a/b*b);
}
/* calulate greatest common divisor */
int gcd(int x, int y){
int c;
if (x < 0){
x = -x;
}
if (y < 0){
y = -y;
}
while (y != 0) {
c = mod(x, y);
x = y;
y = c;
}
return x;
}
int main() {
int a;
int b;
print("Please enter the first number: ");
print_nl();
a = read_int();
print("Please enter the second number: ");
print_nl();
b = read_int();
int res = gcd(a,b);
print("The greatest common divisor is: ");
print_int(res);
print_nl();
return 0;
}

View File

@ -0,0 +1,2 @@
10
15

View File

@ -0,0 +1,3 @@
Please enter the first number:
Please enter the second number:
The greatest common divisor is: 5

View File

@ -0,0 +1,61 @@
/*
* Example input, written in mC
*
* This program coverts three non-negative integers (entered by the user) into their binary representation.
* The result is printed to the console.
*/
void convert2bin(int number){
/*Recursive conversion of an integer into binary*/
if (number!=0){
int quot;
quot = number/2;
if(quot>0){
convert2bin(quot);
}
int remainder;
remainder=number-2*quot;
print_int(remainder);
}
else{
print_int(0);
}
}
int main()
{
int[3] array;
int counter;
counter=1;
/* Get user input*/
while(counter<=3){
print("Enter a non-negative integer (");
print_int(counter);
print(" of 3): ");
array[counter-1]=read_int();
counter=counter+1;
}
print_nl();
/* Compute binary numbers and print them*/
counter=0;
while(counter<3){
print("Binary representation of ");
print_int(array[counter]);
print(": ");
convert2bin(array[counter]);
print_nl();
counter=counter+1;
}
return 0;
}

View File

@ -0,0 +1,3 @@
42
255
150595

View File

@ -0,0 +1,4 @@
Enter a non-negative integer (1 of 3): Enter a non-negative integer (2 of 3): Enter a non-negative integer (3 of 3):
Binary representation of 42: 101010
Binary representation of 255: 11111111
Binary representation of 150595: 100100110001000011

View File

@ -0,0 +1,29 @@
/* is_squared returns true if a number is the square of any integer */
bool is_square(int n)
{
int i = 0;
while(i*i < n){
if(i*i == n){
return true;
}
i = i + 1;
}
return false;
}
int main()
{
print("Please enter a number: ");
print_nl();
int n;
n = read_int();
if(is_square(n)){
print("Yes");
}
else
{
print("No");
}
print_nl();
}

View File

@ -0,0 +1 @@
16

View File

@ -0,0 +1,2 @@
Please enter a number:
No

144
examples/nim/nim.mc Normal file
View File

@ -0,0 +1,144 @@
/*
The computers realy stupid strategy
It is only able to win if the human lets the computer make the first move
*/
int computer_strategy(int pile, int human_last_choice)
{
int choice;
if(human_last_choice == 0)
{
{
{ return 2; }
}
}
choice = 2 * 2 - human_last_choice;
if( (pile - choice ) < 0 )
{
return 1;
}
return choice;
}
void print_pile_size(int pile)
{
print("Size of the match stick pile: ");
print_int(pile);
print_nl();
}
void print_removal(string player, int choice)
{
print(player);
print(" removed: ");
print_int(choice);
print_nl();
}
void game(int pile, int start)
{
int human_choice;
int computer_choice;
int next;
string opponent;
string computer_chose;
computer_chose = "Computer removed: ";
string human_chose;
human_chose = "You decided to remove: ";
if( start == 1 )
{
opponent = "Human";
computer_choice = computer_strategy(pile, 0);
pile = pile - computer_choice;
print_removal("Computer", computer_choice);
}
while( pile > 0 )
{
opponent = "Computer";
print_pile_size(pile);
print("How many match sticks do you want to take away? Between 1 and 3.");
print_nl();
human_choice = read_int();
next = pile - human_choice;
if( (human_choice <= 3) && ( next >=0 ) )
{
pile = next;
print_removal("You", human_choice);
print_pile_size(pile);
}
else
{
print("Invalid move. You must take between 1 and 3 match sticks. But you tried to take ");
print_int(human_choice);
print(" match sticks");
print_nl();
}
if( human_choice <= 3 && pile>0)
{
opponent = "Human";
computer_choice = computer_strategy(pile, human_choice);
pile = pile - computer_choice;
print(computer_chose);
print_int(computer_choice);
print_nl();
}
}
print(opponent);
print(" wins.");
print_nl();
}
int main()
{
int pile;
int start;
pile = 15;
print("NIM: The match stick game");
print_nl();
print(".........................");
print_nl();
print("Size of the match stick pile: ");
print_int(pile);
print_nl();
print("Who should start the game? 0 for You 1 for Computer");
print_nl();
start = read_int();
if( start < 0 || start > 1 )
{
print("FAILURE: Start was ");
print_int(start);
print("but should have been between 0 and 1");
print_nl();
return 1;
}
game(pile, start);
return 0;
}

View File

@ -0,0 +1,6 @@
0
14
2
2
2
2

View File

@ -0,0 +1,28 @@
NIM: The match stick game
.........................
Size of the match stick pile: 15
Who should start the game? 0 for You 1 for Computer
Size of the match stick pile: 15
How many match sticks do you want to take away? Between 1 and 3.
Invalid move. You must take between 1 and 3 match sticks. But you tried to take 14 match sticks
Size of the match stick pile: 15
How many match sticks do you want to take away? Between 1 and 3.
You removed: 2
Size of the match stick pile: 13
Computer removed: 2
Size of the match stick pile: 11
How many match sticks do you want to take away? Between 1 and 3.
You removed: 2
Size of the match stick pile: 9
Computer removed: 2
Size of the match stick pile: 7
How many match sticks do you want to take away? Between 1 and 3.
You removed: 2
Size of the match stick pile: 5
Computer removed: 2
Size of the match stick pile: 3
How many match sticks do you want to take away? Between 1 and 3.
You removed: 2
Size of the match stick pile: 1
Computer removed: 1
Human wins.

View File

@ -0,0 +1,61 @@
int returnTwo() {
int value = 2;
return value;
}
bool isOne(int in) {
if (in == 1)
return true;
else
return false;
}
int main() {
int[42] array;
int[21] arrayTwo;
int i = 0;
while (i < 21) {
array[i] = i*i;
i = i + 1;
}
i = 0;
while (i < 21) {
arrayTwo[i] = array[i];
i = i + 1;
}
print_int(array[20]);
print_nl();
if (array[0] == 0) {
print_int(array[0]);
print_nl();
}
if (array[1] == 2) {
print_int(0);
print_nl();
} else {
print_int(array[1]);
print_nl();
}
if (!isOne(2)) {
print_int(returnTwo());
print_nl();
}
int b;
int a;
b = 2 + 3;
b = -b;
print_int(b);
print_nl();
a = b;
b = b * b;
if (a == (b/a)) {
print_int(b);
print_nl();
}
return 0;
}

View File

View File

@ -0,0 +1,6 @@
400
0
1
2
-5
25

View File

@ -0,0 +1,45 @@
int modulo(int n, int k) {
return (n - k * (n / k));
}
bool is_palindrome(int original) {
int reversed;
int remainder;
int to_check;
reversed = 0;
to_check = original;
while (to_check != 0) {
remainder = modulo(to_check, 10);
reversed = reversed * 10 + remainder;
to_check = to_check / 10;
}
if (original == reversed) {
return true;
} else {
return false;
}
}
int main() {
int input;
print("Please enter a number:");
input = read_int();
print_nl();
bool result = is_palindrome(input);
print_int(input);
if (result) {
print(" is a palindrome.");
} else {
print(" is not a palindrome.");
}
print_nl();
return 0;
}

View File

@ -0,0 +1 @@
4321234

View File

@ -0,0 +1,2 @@
Please enter a number:
4321234 is a palindrome.

View File

@ -0,0 +1,151 @@
/* implementation of slowsort on integers, based on https://en.wikipedia.org/wiki/Slowsort */
void i_slowsort(int[10] arr, int i, int j){
if (i >= j){
return;
}
int m;
m = (i+j) / 2;
i_slowsort(arr, i, m);
i_slowsort(arr, m+1, j);
if (arr[j] < arr[m]){ /* swap elements on index j and m */
int tmp;
tmp = arr[j];
arr[j] = arr[m];
arr[m] = tmp;
}
j = j - 1;
i_slowsort(arr, i, j);
return;
}
/* implementation of slowsort on floats, based on https://en.wikipedia.org/wiki/Slowsort */
void f_slowsort(float[10] arr, int i, int j){
if (i >= j){
return;
}
int m;
m = (i+j) / 2;
f_slowsort(arr, i, m);
f_slowsort(arr, m+1, j);
if (arr[j] < arr[m]){ /* swap elements on index j and m */
float tmp;
tmp = arr[j];
arr[j] = arr[m];
arr[m] = tmp;
}
j = j - 1;
f_slowsort(arr, i, j);
return;
}
/* prints int array to stdout */
void print_i_array(int[10] arr, int arr_size){
int i;
i = 0;
while(i < arr_size){
print_int(arr[i]);
print(" ");
i = i+1;
}
print_nl();
}
/* prints float array to stdout */
void print_f_array(float[10] arr, int arr_size){
int i;
i = 0;
while(i < arr_size){
print_float(arr[i]);
print(" ");
i = i+1;
}
print_nl();
}
int get_choice(){
print("Do you want to sort: ");
print_nl();
print("[1] integers, or");
print_nl();
print("[2] floats?");
print_nl();
print("choice: ");
print_nl();
int choice;
choice = read_int();
return choice;
}
int main(){
print("*** Implementation of Slowsort ***");
print_nl();
/* get user choice */
int choice;
choice = get_choice();
bool is_int;
if (choice == 1){
is_int = true;
}else if (choice == 2){
is_int = false;
}else{
print("Invalid choice! Choice have to be either 1, or 2.");
print_nl();
return 0;
}
print("give a sequence of 10 numbers to be sorted: ");
print_nl();
int arr_size = 10;
int i;
i=0;
if (is_int){
int i_arr[arr_size];
int i_number;
while(i < arr_size){
print_int(i);
print(". number = ");
print_nl();
i_number = read_int();
i_arr[i] = i_number;
i = i+1;
}
/* print input array to stdout */
print("input array: ");
print_i_array(i_arr, arr_size);
/* call sorting function */
i_slowsort(i_arr, 0, arr_size-1);
/* print result array to stdout */
print("output array: ");
print_i_array(i_arr, arr_size);
}else{
float f_arr[arr_size];
float f_number;
while(i < arr_size){
print_int(i);
print(". number = ");
f_number = read_float();
f_arr[i] = f_number;
i = i+1;
}
/* print input array to stdout */
print("input array: ");
print_f_array(f_arr, arr_size);
/* call sorting function */
f_slowsort(f_arr, 0, arr_size-1);
/* print result array to stdout */
print("output array: ");
print_f_array(f_arr, arr_size);
}
return 0;
}

View File

@ -0,0 +1,11 @@
1
25
35435
232
12435
25
678
96854
1
456
77777

View File

@ -0,0 +1,18 @@
*** Implementation of Slowsort ***
Do you want to sort:
[1] integers, or
[2] floats?
choice:
give a sequence of 10 numbers to be sorted:
0. number =
1. number =
2. number =
3. number =
4. number =
5. number =
6. number =
7. number =
8. number =
9. number =
input array: 25 35435 232 12435 25 678 96854 1 456 77777
output array: 1 25 25 232 456 678 12435 35435 77777 96854

View File

@ -0,0 +1,34 @@
/* This is a comment */
int main(){
print("Please enter a number: ");
print_nl();
int n;
n = read_int();
bool a = 1 <= n;
bool b = 2 == n;
b = 3 >= n;
n = n * 2;
n = n / 2;
n = n + 2;
n = n - 2;
a && b;
a || b;
a != !b;
int[3] test;
string hi = "Hello";
float f = -1.0;
bool c = true;
if (c)
{
while(c){
c = false;
}
}
if (c){
f = f * 2;
}
else{
f = f * 3;
}
return 0;
}

View File

@ -0,0 +1 @@
20

View File

@ -0,0 +1 @@
Please enter a number: