2) ( 60p) Write a C program that reads a number n from the user and allows to print a house with a roof using ‘*’ characters. The house should be a n x n square and the roof two diagonal lines of 45 degrees forming a roof (if n is odd the roof closes in the top perfectly).

Respuesta :

Answer:

Explanation:

#include <iostream>

using namespace std;

int printroofln(int, int);

int printbodyln(int, int);

int main()

{

   int hgt=0,odd=0,rhgt=0;

   cout<<"Enter the height:";

   cin>>hgt;

   if ((hgt%2)==1) rhgt=(hgt/2)+1;

   else rhgt=hgt/2;

       

   for (int i=0;i<rhgt;i++)

   {

       printroofln(i,hgt);

   }

   for (int j=0;j<hgt;j++)

   {

       printbodyln(j,hgt);

   }

   return 0;

}

int printroofln(int rfln, int hgt)

{

   int id1,id2,hlf=0;

   hlf=hgt/2;

   if ((hgt%2)==1)

   {

       id1=hlf-rfln;

       id2=hlf+rfln;

   }

   else

   {

       id1=hlf-rfln-1;

       id2=hlf+rfln;

   }

   

   for (int i=0;i<=hgt;i++)

   {

       if ((id1==i)||(id2==i))

           cout<<"*";

       else

           cout<<" ";

   }

   cout<<endl;

   return 0;

}

int printbodyln(int bln, int hgt)

{

   for (int i=0;i<hgt;i++)

   {

       if ((bln==0)||(bln==(hgt-1))||(i==0)||(i==(hgt-1)))

       {

           cout<<"*";

       }

       else

       {

           cout<<" ";

       }

   }

   cout<<endl;

   return 0;

}