?
Solved

Quick help on this program.

Posted on 2003-03-21
7
Medium Priority
?
184 Views
Last Modified: 2010-04-01
A console program.
needs to print out this pattern.

if input is 1,
*                     // start with 1 *
if input is 2,
*        
  *                   // if even number, start with 2 spaces first.
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..

any leads?

thanks.
0
Comment
Question by:takkie
  • 4
6 Comments
 

Author Comment

by:takkie
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

by:takkie
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

by:
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 3

Expert Comment

by:jcgd
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

by:takkie
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

by:bcladd
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:

Answered by original poster:  PAQ/Refund

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

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

-bcl (bcladd)
EE Cleanup Volunteer
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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…

621 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question