Solved

# Quick help on this program.

Posted on 2003-03-21
Medium Priority
184 Views
A console program.
needs to print out this pattern.

if input is 1,
if input is 2,
*
if input is 3,
*   *                 // if odd number, start with *, then 3 spaces, and then anther *
*
*   *
if input is 4,
*   *
*   *            // start with 2 spaces first, then 3 spaces between *   *
*   *
*   *
if input is 5,
*   *   *
*   *
*   *   *
*   *
*   *   *

so on.  I do not wish you will do this all for me. i am just stuck at this,

i have analyzed this program as following,
for even number row, start with 2 spaces first, and then depending on what the input is, there will be 3 spaces between the *.

this is what i have now.

for (int i =1; i <= row; i++)
{
if ( row%2==1)
{
for (int j=1; j<=(row) ; j++)
{
cout << "1   " ;

}
cout << endl;
}
else
{
cout << "  ";
for (int j=1; j <= row/2 ; j++)
{
cout << "   2";
}
}
}

this is not working now...but i really dont know which way i should start polishing it..

thanks.
0
Question by:takkie
• 4

Author Comment

ID: 8182281
just want to update.

i got it to look better...
now it prints fine for odd number input..
but missing 2 spaces for even number input.

// row is the input from user.

int col=1; int row;
for (int i =row-1; i> 0; i--)
{
if (i%2==1)
{
for (int j=1; j<=col - (col/2) ; j++)
{
cout << "1   " ;
}
cout << endl;
}
else
{
cout << "  ";
for (int j=1; j <= col/2 ; j++)
{
cout << "2   ";
}
cout << endl;
}
}
0

Author Comment

ID: 8182291
just want to update.

i got it to look better...
now it prints fine for odd number input..
but missing 2 spaces for even number input.

// row is the input from user.

int col=1; int row;
for (int i =row-1; i> 0; i--)
{
if (i%2==1)
{
for (int j=1; j<=col - (col/2) ; j++)
{
cout << "1   " ;
}
cout << endl;
}
else
{
cout << "  ";
for (int j=1; j <= col/2 ; j++)
{
cout << "2   ";
}
cout << endl;
}
}
0

Accepted Solution

takkie earned 0 total points
ID: 8182356
just want to update.

i got it to look better...
now it prints fine for odd number input..
but missing 2 spaces for even number input.

// row is the input from user.

int col=1; int row;
for (int i =row-1; i> 0; i--)
{
if (i%2==1)
{
for (int j=1; j<=col - (col/2) ; j++)
{
cout << "1   " ;
}
cout << endl;
}
else
{
cout << "  ";
for (int j=1; j <= col/2 ; j++)
{
cout << "2   ";
}
cout << endl;
}
}
0

LVL 3

Expert Comment

ID: 8183288
int main(int argc, char* argv[])      {
if (argc!=2) return 0;

int i = atoi(argv[1]); // i = input user

int num=i;
int nloop=i/2 ;
for (int f=0;f <= nloop; f++)
{
i=num;
int n=0;
int d = 0;
if (f > 0  || (num%2)==0)
{
if (num > 3)
{
if ( (num%2)!=0)  cout << "  *";
else     cout << "*   ";
if ( (num%2)==0)  cout <<  "*   " << endl;
else     cout << "   *" << endl;
}
else
{
if ( (num%2)==0)  cout <<  "*" << endl;
else     cout << "  *" << endl;
}
}
if ( (i%2)!=0)  {
cout << "*";
n=i - 1;
}
else {
cout << "  *";
n=i - 2;
}
d=(n/2);
for (int y=0;y<d;y++) cout << "   *";
cout << endl;
}

return 0;
}
0

Author Comment

ID: 8183418
i actually got it before your post and mine is alot more shorter than yours. mine is only 15 lines...

but really thank you.
0

LVL 11

Expert Comment

ID: 9543879
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Please leave any comments here within the next seven days. Experts: Silence
means you don't care.

EE Cleanup Volunteer
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
###### Suggested Courses
Course of the Month8 days, 11 hours left to enroll