Click here to Skip to main content
15,889,863 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Implement a function that draws a rectangle shape based on the given dimension. This function 
will take two parameters:
• L: Length of the rectangle
• W: Width of the rectangle
Conditions:
• Output shape should be printed using the alphabets, starting with the alphabet ‘a’ on the 
first line, then ‘b’ on the second, ‘c’ on 3rd and so on.
• The casing of the alphabet should change in each column (only columns). The first 
column will have lower case, the 2nd upper case, 3rd lower case again and so on.
• The inside of the rectangle should be empty (fill it with spaces).
The function that draws the rectangle will call 2 functions to help it perform its task.
The first function will be called to handle drawing the top or bottom line. The second function 
will be called to draw middle lines.
(Hint: the lower and upper case ascii of an alphabet character are 32 characters apart)


What I have tried:

#include <iostream>

using namespace std;
void uperline(int d , int l)
{
for (int i=1; i<=d; i++)
    {
        if (i%2==0)
            cout<<"A";

        else
            cout<<"a";}

}
void middlelines (int d , int l)
{
    for (int i=1 ;i <=l-1;i++)
    {  char alpha ='b';
    for(int y=0 ; y==i; y++)
        alpha++;
    cout<<alpha<<endl;

    }
}
int main()
{

    int width ,length;
    cout <<"please enter the length and the width of the rctangle"<<endl;
    cin >> width>> length;
uperline(width , length);
middlelines (width , length);

return 0;


"
I couldn't complete the rest of the columns
"
Posted
Updated 15-Dec-21 2:00am
Comments
Richard MacCutchan 15-Dec-21 7:15am    
What does that mean? What results do you see when you run the code, and why is it wrong?

The question asks you to only use two functions, one of which draws both the upper and lower lines. The character to use therefore needs to be an argument to that function. The character to use could also be an argument to the function that draws the middle lines if you change main to invoke it within a for loop. Also note the hint that if ch is an upper case character, then ch+32 is its lower case equivalent.
 
Share this answer
 
You may write a function for edge (i.e. top and bottom) lines, like the following:
C++
void edge_line(int row, int length)
{
  for (int col=0; col < length; ++col)
  {
    char c = 'a'+ row - 32 * (col & 1);
    cout << c;
  }
  cout << '\n';
}
Similarly, you may write the code for the middle line function.

Eventually you have to call them:
C++
edge_line(0, length);
for (row = 1; row < (width-1)); ++row)
  middle_line(row, length);
edge_line((width-1), length);

That's all.
 
Share this answer
 
Comments
Greg Utas 15-Dec-21 8:15am    
5, but you're spoiling him. :)
CPallini 15-Dec-21 9:02am    
:-D
Thanks, have my 5 as well.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900