Add student example inputs
This commit is contained in:
61
examples/intToBin/intToBin.mc
Normal file
61
examples/intToBin/intToBin.mc
Normal 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;
|
||||
}
|
3
examples/intToBin/intToBin.stdin.txt
Normal file
3
examples/intToBin/intToBin.stdin.txt
Normal file
@@ -0,0 +1,3 @@
|
||||
42
|
||||
255
|
||||
150595
|
4
examples/intToBin/intToBin.stdout.txt
Normal file
4
examples/intToBin/intToBin.stdout.txt
Normal 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
|
Reference in New Issue
Block a user