Thread: how can i do this without using loops?

  1. #1
    Registered User
    Join Date
    Aug 2003
    Posts
    71

    how can i do this without using loops?

    Hi,

    I am supposed to print 4 lines of each like this

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

    i have started like this
    Code:
    int main(int argc, char* argv[])
    {
    
       cout << "* * * * * * * *\n";
       cout << " * * * * * * * *";
    but what do i do now? i haven't covered loops in my book yet.

  2. #2
    Rabite SirCrono6's Avatar
    Join Date
    Nov 2003
    Location
    California, US
    Posts
    269

    Question Uh...

    You mean just make 4 lines of * * * * * * or whatever? Just use cout if you want:

    Code:
    #include <iostream>
    using std::cin;
    using std::cout;
    using std::endl;
    int main(int argc,char *argv[])
    {
      cout<<"**********"<<endl;
      cout<<"**********"<<endl;
      cout<<"**********"<<endl;
      cout<<"**********"<<endl;
      cin.get();
      return 0;
    }
    That will print four lines of asterisks and not close. But you can do it with a loop.
    From C to shining C++!

    Great graphics, sounds, algorithms, AI, pathfinding, visual effects, cutscenes, etc., etc. do NOT make a good game.
    - Bubba

    IDE and Compiler - Code::Blocks with MinGW
    Operating System - Windows XP Professional x64 Edition

  3. #3
    Registered User
    Join Date
    Jun 2003
    Posts
    361
    If I understand right, you're trying to display 4 lines of stars?

    If so, the easy way around would be close to what you have, but instead:
    Code:
    cout << "* * * * * * * *\n";
    cout << "* * * * * * * *\n";
    cout << "* * * * * * * *\n";
    cout << "* * * * * * * *\n"; //You probably want this last '\n' character incase you want to display more things afterwards.
    Using loops though, you could use something like:
    Code:
    int x;
    for (x=0; x<4; x++)
    {
    cout << "* * * * * * * *\n";
    }
    What this does, is:
    1) a variable "x" is set to 0
    2) The "For Loop" (that's what it's called) will continue to go as long as x is less than 4
    3) "x++" is short for "x = x + 1", so, x will go up by one each time
    So, everything within the {} brackets goes, then x goes up by one, then the For Loop checks to see if it should stop yet, if not, it does the code in the {} brackets again, and so on, until x gets bigger than 4

    Another loop method could be:
    Code:
    int x=0;
    while(x < 4)
    {
    cout << "* * * * * * * *\n";
    x++;
    }
    This will give you the same thing. x starts at 0. Then it displays your first line of stars. Then it increments x by 1. Then it checks to see if x is less than 4. If it is, it repeats the code in the {} brackets. If not, it will leave the loop.

    I hope this helps out some.

    [edit]
    I'm not up to speed in my COUT stuff, so SirCrono's example will probably be better to follow.
    [/edit]
    Last edited by Epo; 12-20-2003 at 09:31 PM.

  4. #4
    Registered User
    Join Date
    Aug 2003
    Posts
    71
    Sorry if i was unclear, well here goes -

    the instructions - display the checkerboard pattern with 8 output statements. so that is like this

    Code:
      cout << "* * * * * * * *\n";
      cout << " * * * * * * * *\n";
      cout << "* * * * * * * *\n";
      cout << " * * * * * * * *\n";
      cout << "* * * * * * * *\n";
      cout << " * * * * * * * *\n";
      cout << "* * * * * * * *\n";
      cout << " * * * * * * * *\n";
    but then i have to display the same pattern using as few output statements as possible.

    so i'm wondering how i could do that, without using a loop remember.

    thanks
    Last edited by Guti14; 12-20-2003 at 09:58 PM.

  5. #5
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    One statement.
    Code:
    cout << "* * * * * * * *\n"
         << " * * * * * * * *\n"
         << "* * * * * * * *\n"
         << " * * * * * * * *\n"
         << "* * * * * * * *\n"
         << " * * * * * * * *\n"
         << "* * * * * * * *\n"
         << " * * * * * * * *\n";
    gg

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Code:
      char *rows[] = {
        "* * * * * * * *\n",
        " * * * * * * * *\n"
      };
      cout << rows[0] << rows[1] << rows[0] << rows[1] << rows[0] << rows[1] << rows[0] << rows[1];

    Disguised loop
    Code:
    void rowpair ( const char *rows[], int n ) {
        if ( n != 0 ) {
            cout << rows[0] << rows[1];
            rowpair( rows, n-1 );
        }
    }
    
    int main ( ) {
      char *rows[] = {
        "* * * * * * * *\n",
        " * * * * * * * *\n"
      };
      rowpair( rows, 4 );
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Loops Trouble
    By rlframpton in forum C Programming
    Replies: 2
    Last Post: 04-17-2009, 01:08 AM
  2. Too many loops D:
    By F5 Tornado in forum C++ Programming
    Replies: 6
    Last Post: 12-03-2007, 01:18 AM
  3. recoursion or loops?
    By Mecnels in forum C++ Programming
    Replies: 2
    Last Post: 01-14-2002, 12:09 PM
  4. help with arrays and loops
    By jdiazj1 in forum C Programming
    Replies: 4
    Last Post: 11-24-2001, 04:28 PM
  5. for loops - newbie q's
    By Narciss in forum C Programming
    Replies: 8
    Last Post: 09-26-2001, 02:44 AM