?
Solved

Quick help on this program.

Posted on 2003-03-21
7
Medium Priority
?
183 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
7 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

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.

Question has a verified solution.

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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses

765 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