Printing out a diagram

How would i go about in writing a program that prints the following diagram just as it appears. It's suppose to be a rectangle but it doesn't come out like one when i hit preview.

*********
* *
* *
* *
* *
* *
* *
* *
*********

I guess I'd look at this as two rows of the rectangle (first and last) have one pattern, and the other rows have a different pattern. Nested for loops could be used to control rows and columns and determine what to print out.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
using namespace std;

int main()
{
    int length;
    int width;

    cout<<"Enter length: ";
    cin>>length;

    cout<<"Enter width: ";
    cin>>width;

    for(int i = 0; i < width; i++)
    {
        for(int j = 0; j < length; j++)
        {
            if(j%length == 0)
                cout<<endl;
            if((j == 0 || j ==length - 1) || (i == 0 || i == width - 1))
                cout<<'*';
            else
                cout<<' ';

        }
    }
    cin.ignore();
    return 0;
}
I remember this being a homework assignment, that's why I just gave a hint to get started, not an entire program :)
ok thanks ill try it out and see if it works
Topic archived. No new replies allowed.