Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C++ and Visual C++ > C++ Programming
|
C++ Programming General discussions for the C++ language. For questions specific to Microsoft's Visual C++ variant, see the Visual C++ forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C++ Programming section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old February 13th, 2006, 11:37 PM
Registered User
 
Join Date: Feb 2006
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default for loop trouble (beginner)

I am trying to output the following pattern:

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

Here is what i have been able to come with:

#include <iostream>
using namespace std;

int main()
{
    int column=16;
    int row=8;

    for (int counter = 0; counter < row; counter++){
        for (int count = 0; count < column; count++){
            if ( count % 2 == 0)
            cout << "*";
            else
                cout << " ";
        }
    cout << endl;
    }
    return 0;
}

I dont know how to get every other row shifted 1 space. The exercise i am doing says to only use 3 output statements

cout <<'*';
cout <<" ";
cout << endl;

I would like to add, this isnt homework. I am trying to learn C++ on my own (and with your help). I have about 8 books on the subject (one is wrox- beginning C++). This particular exercise is out of an old Deitel & Deitel book, C++ How To Progream (3rd Edition).

Please dont just give a solution, try to walk me thru the solution. I am sure its something math related, i just cant figure it out and instead of giving up completly, decided to ask for help.

Thanks
Chris

Reply With Quote
  #2 (permalink)  
Old March 5th, 2006, 04:00 PM
Authorized User
 
Join Date: Oct 2004
Posts: 39
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to C@uark
Default

Try this,

int main()
{
    int column=16;
    int row=8;

    for (int counter = 0; counter < row; counter++){
        if ( counter % 2 == 0 ) // if even row shift one space
            cout << " ";
        for (int count = 0; count < column; count++){
            if ( count % 2 == 0)
            cout << "*";
            else
                cout << " ";
        }
    cout << endl;
    }
    return 0;
}
Reply With Quote





Similar Threads
Thread Thread Starter Forum Replies Last Post
A beginner who needs need help Jamesbizprocom Beginning PHP 1 June 28th, 2007 11:02 PM
Beginner Tilak_1 C# 2 January 21st, 2005 11:19 PM
Need help - C++ beginner Satheesh C++ Programming 13 August 7th, 2004 09:18 PM
nested while loop doesn't loop hosefo81 PHP Databases 5 November 12th, 2003 08:46 AM
Beginner NEED HELP!! nvillare VB.NET 4 September 18th, 2003 02:44 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.