Welcome! Log In Create A New Profile

Advanced

Assignment 3 question 4

Posted by Bossf 
Announcements Last Post
Announcement SoC Curricula 09/30/2017 01:08PM
Announcement Demarcation or scoping of examinations and assessment 02/13/2017 07:59AM
Announcement School of Computing Short Learning Programmes 11/24/2014 08:37AM
Announcement Unisa contact information 07/28/2011 01:28PM
Assignment 3 question 4
May 26, 2008 11:27AM
Could someone plz help me with this question? The output looks like this can't seem to get the spaces. source file is below the output. thanx

output

What width (greater than 4) frame you would like to draw: 10
What hight (greater than 4) frame you would like to draw: 16
Choose the character you wish to be displayed: #


# # # # # # # # # #
# # # # # # # # # #
# # # #
# # # #
# # # #
# # # #
# # # #
# # # #
# # # #
# # # #
# # # #
# # # #
# # # #
# # # #
# # # # # # # # # #
# # # # # # # # # #


Press any key to continue . . .

source

// Assignment 3 Question 4

#include <iostream>
using namespace std;

// Draw Frame
void drawFrame (int W,int H, char c)
{
cout << endl;
for ( int i = 1; i <= H; i++)
{
for( int j = 1; j <= W; j++)
{
if ((i==1)||(i==2)||(i == (H-1))||(i == H))
cout << c <<" ";
else if ((j==1)||(j==2)||(j == (W-1))||(j == W ))
cout << c <<" ";
else
cout <<" ";
}
cout << endl;
}
}
// Main
int main ( )
{
int width, height;
char c;

cout << "What width (greater than 4) frame you would like to draw: ";
cin >> width;
cout << "What hight (greater than 4) frame you would like to draw: ";
cin >> height;
cout <<"Choose the character you wish to be displayed: ";
cin >> c;
cout << endl;
drawFrame (width, height, c);
cout << endl << endl;
return 0;
}
Re: Assignment 3 question 4
May 26, 2008 11:35AM
the block has a hole in the middle i ment the spacese between the #

thanx
avatar Re: Assignment 3 question 4
May 26, 2008 11:58AM
Hi ... why the spaces after the c ??? Either I'm wrong or this is not required.

  cout << c <<" "; // character space

  cout <<" ";   // or should this be cout <<" " << " "; 2 spaces ????

--------------------------------------------------------------
Don't Assume Everything is a Nail Just Because You Have a Really Big Hammer
Re: Assignment 3 question 4
May 26, 2008 12:06PM
Hi Bossf,

When posting code, you should put it in a

block. That way formatting will be preserved.

Chris beat me to the answer. Either remove extra spaces, or add extra spaces. Personally I would also unify the two IFs by putting an || between the two conditions.

if (a==1) 
{
   z();
}
else if (b==1)
{
   z();
}

Is equivalent to:

if ((a==1)||(b==1))
{
   z();
}

It is also a good habit to always enclose the code after an if with {}, even if it's only one statement. It's too easy to add another statement later, and forget to add the {} too.

In addition, you can simplify (i==1)||(i==2) to (i<=2). You can do the same thing for every wall check.
Re: Assignment 3 question 4
May 26, 2008 06:36PM
"cout << c <<" "; // character space

cout <<" "; // or should this be cout <<" " << " "; 2 spaces ????"

when removing the space or adding anoteher the block is not as posted by the question. i loose the space in the midd of the block.
avatar Re: Assignment 3 question 4
May 26, 2008 06:42PM
OK, I ran your program and this is the output it's actually getting, if you'd have put it between code tags it would have shown.
What width (greater than 4) frame you would like to draw: 10
What hight (greater than 4) frame you would like to draw: 16
Choose the character you wish to be displayed: #


# # # # # # # # # #
# # # # # # # # # #
# #       # #
# #       # #
# #       # #
# #       # #
# #       # #
# #       # #
# #       # #
# #       # #
# #       # #
# #       # #
# #       # #
# #       # #
# # # # # # # # # #
# # # # # # # # # #

The two chaps above have basically nailed it.

It looks like you're putting in extra spaces for asthetics purposes. There's nothing wrong with that but it's not according to the question. Your error is that you're outputting two characters when you're drawing a border but only one character where you're drawing the 'hole'. You must now decide which change to make: adding an extra space to the line that draw the 'hole';

 cout << "  "; //two spaces

# # # # # # # # # #
# # # # # # # # # #
# #             # #
# #             # #
# #             # #
# #             # #
# #             # #
# #             # #
# #             # #
# #             # #
# #             # #
# #             # #
# #             # #
# #             # #
# # # # # # # # # #
# # # # # # # # # #

or taking the extra space out of the border.
cout << c;  // remove the extra space

##########
##########
##      ##
##      ##
##      ##
##      ##
##      ##
##      ##
##      ##
##      ##
##      ##
##      ##
##      ##
##      ##
##########
##########
Re: Assignment 3 question 4
May 27, 2008 03:23AM
now i'm really confused. if you would plz edit the program i posted and re-post.

thanx 4 your time guys smile
avatar Re: Assignment 3 question 4
May 27, 2008 09:54AM

// Assignment 3 Question 4

#include <iostream>
using namespace std;

// Draw Frame
void drawFrame (int W,int H, char c)
{
    cout << endl;
    for ( int i = 1; i <= H; i++)
    {
        for( int j = 1; j <= W; j++)
        {
            if ((i==1)||(i==2)||(i == (H-1))||(i == H)
                (j==1)||(j==2)||(j == (W-1))||(j == W ))
                cout << c;
            else
                cout <<" ";
        }
        cout << endl;
    }
}
// Main
int main ( )
{
    int width, height;
    char c;

    cout << "What width (greater than 4) frame you would like to draw: ";
    cin >> width;
    cout << "What hight (greater than 4) frame you would like to draw: ";
    cin >> height;
    cout <<"Choose the character you wish to be displayed: ";
    cin >> c;
    cout << endl;
    drawFrame (width, height, c);
    cout << endl << endl;
    return 0;
}

--------------------------------------------------------------
Don't Assume Everything is a Nail Just Because You Have a Really Big Hammer
Re: Assignment 3 question 4
May 27, 2008 10:40AM
Actually Chris made a mistake with the if - He forgot to put an || between the two statements.

The if code can also be simplified to:

            if ( (i<=2) || (i>=(H-1)) ||
                 (j<=2) || (j>=(W-1)) )
            {
                cout << c;
            }
            else
            {
                cout <<" ";
            }
avatar Re: Assignment 3 question 4
May 27, 2008 12:08PM
thanks ... didn' see that one

--------------------------------------------------------------
Don't Assume Everything is a Nail Just Because You Have a Really Big Hammer
Re: Assignment 3 question 4
May 28, 2008 02:09AM
Thanx a lot guys. i saw what the problem was i made the code more complicated than i was suppose to.

i'll be sure to ask again i've i have any questions.thumbs up
Sorry, only registered users may post in this forum.

Click here to login