Add student example inputs
This commit is contained in:
151
examples/slowsort/slowsort.mc
Normal file
151
examples/slowsort/slowsort.mc
Normal 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;
|
||||
}
|
11
examples/slowsort/slowsort.stdin.txt
Normal file
11
examples/slowsort/slowsort.stdin.txt
Normal file
@@ -0,0 +1,11 @@
|
||||
1
|
||||
25
|
||||
35435
|
||||
232
|
||||
12435
|
||||
25
|
||||
678
|
||||
96854
|
||||
1
|
||||
456
|
||||
77777
|
18
examples/slowsort/slowsort.stdout.txt
Normal file
18
examples/slowsort/slowsort.stdout.txt
Normal 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
|
Reference in New Issue
Block a user