//---------------------------------------------------------------------------------------------------------------------
// homework_1
//
// From the defined INPUT with a range from 2 to 127, this program builds a ASCII house.
// After the house is output in the terminal the INPUT is converted to Octal, Hexadecimal,
// Binary and Binary Coded Decimal
//
// tested in Wokwii, because I currently have no raspberry pi. I had to do a workaround in VS Code
// to test there: removed pico/stdlib.h and stdio_init_all(), and a define for int8_t: "define int8_t int"
//
//
// Author: Tobias Färber
// Mat. Nr.: 12321474
//---------------------------------------------------------------------------------------------------------------------
//
#include <stdio.h>
#include "pico/stdlib.h"
// INPUT defines your input number and can be altered
#define INPUT (5)
//---------------------------------------------------------------------------------------------------------------------
///
/// handles whole program
///
///
///
//
void main()
{
int8_t house_height = 0;
int8_t base_length = 0;
int8_t roof_left = 0;
int8_t roof_right = 0;
int8_t roof_middle = -1;
int8_t octal_quotient = 0;
int8_t octal_digit = 0;
int8_t octal_leading_0_cond = 0;
int8_t hex_quotient = 0;
int8_t hex_digit = 0;
int8_t hex_leading_0_cond = 0;
int8_t binary_quotient = 0;
int8_t binary_digit = 0;
int8_t binary_leading_0_cond = 0;
int8_t bcd_quotient = 0;
int8_t bcd_digit = 0;
int8_t bcd_digit_power = 0;
stdio_init_all(); // init text output
//check if INPUT has correct range
if (!(INPUT >= 2 && INPUT <= 127))
{
return;
}
//Genereate house
// needs to be set to -1 or programm may give false outputs
roof_middle = -1;
//House height and middle part
base_length = INPUT;
if(base_length%2 == 0){
house_height = base_length;
roof_left = (base_length/2)-1;
roof_right = (roof_left+1);
}
else{
house_height = (base_length-1);
roof_left = (base_length/2)-1;
roof_middle = (roof_left+1);
roof_right = (roof_left+2);
}
//Build house, generate roof
for(size_t i = 0; i < (house_height/2); i++){
if(base_length%2 == 0){
for(size_t j = 0; j < base_length; j++){
if(j == roof_left){
printf("/");
}
else if(j == roof_right){
printf("\\");
}
else{
printf(" ");
}
}
roof_left--;
roof_right++;
}
else{
for(size_t k = 0; k < base_length; k++){
if(k == roof_left){
printf("/");
}
else if(k == roof_middle && i == 0){
printf("-");
}
else if(k == roof_right){
printf("\\");
}
else{
printf(" ");
}
}
roof_left--;
roof_right++;
}
printf("\n");
}
roof_left++;
roof_right--;
//Generate walls
for(size_t i = (house_height/2); i < house_height;i++){
for(size_t j = 0; j < base_length;j++){
if(j == roof_left){
printf("|");
}
else if(j == roof_right){
printf("|");
}
else{
printf(" ");
}
}
printf("\n");
}
printf("\n");
//Conversions
//Decimal
printf("Decimal: %d\n",INPUT);
//Octal conversion
octal_quotient = INPUT;
printf("Octal : 0");
for(size_t i = 64; i>=1; i/=8)
{
octal_digit = octal_quotient/i;
octal_quotient = octal_quotient%i;
if (octal_digit != 0 || octal_leading_0_cond)
{
printf("%d",octal_digit);
octal_leading_0_cond++;
}
}
printf("\n");
//Hex conversion
hex_quotient = INPUT;
printf("Hex : 0x");
for(size_t i = 16; i>=1; i/=16)
{
hex_digit = hex_quotient/i;
hex_quotient = hex_quotient%i;
if (hex_digit != 0 || hex_leading_0_cond)
{
switch(hex_digit){
case 10:
printf("A");
break;
case 11:
printf("B");
break;
case 12:
printf("C");
break;
case 13:
printf("D");
break;
case 14:
printf("E");
break;
case 15:
printf("F");
break;
default:
printf("%d",hex_digit);
break;
}
hex_leading_0_cond++;
}
}
printf("\n");
//Binary conversion
binary_quotient = INPUT;
printf("Binary : ");
for(size_t i = 64; i>=1; i/=2)
{
binary_digit = binary_quotient/i;
binary_quotient = binary_quotient%i;
if (binary_digit == 1||binary_leading_0_cond)
{
printf("%d",binary_digit);
binary_leading_0_cond++;
}
}
printf("\n");
//BCD conversion
bcd_quotient = INPUT;
printf("BCD : ");
for(size_t i = 100; i>=1; i/=10)
{
bcd_digit_power = bcd_quotient/i;
if (bcd_digit_power != 0)
{
for(size_t j = 8; j>=1; j/=2)
{
bcd_digit = bcd_digit_power/j;
bcd_digit_power = bcd_digit_power%j;
printf("%d",bcd_digit);
}
printf(" ");
}
bcd_quotient = bcd_quotient%i;
}
}