Commit 14da3497 authored by Alexander Hirsch's avatar Alexander Hirsch
Browse files

Add student example inputs

parent 480716b4
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();
}
25
\ No newline at end of file
Enter you age:
You Can Drink!
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;
}
\ No newline at end of file
10
30
70
0
10
40
0
0
10
0
0
0
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
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;
}
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
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;
}
Please enter the first number:
Please enter the second number:
euclid(1071, 1029) = 21
/* 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;
}
Enter Dividend:
Enter Divisor:
15 is divisible by 6 with the remainder 3 and result 2
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;
}
Please enter a number:
The factorial of 5 is 120
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;
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment