Prime 357

We'll learn something

Site Menu

  • Home
  • Recent Posts
  • Forum
    • Programming Languages
      • C++
    • Website Design & Content Management
      • Wordpress >> Drupal
  • Blogs
  • Topics
    • C++
    • Changing hosts - Dummies Guide
    • Wordpress >> Drupal
  • Download Centre
  • Contact us
Home


Image - OpenID

User login

What is OpenID?
  • Log in using OpenID
  • Cancel OpenID login
  • Create new account
  • Request new password

Navigation

  • Recent posts

Topics

  • C++ (The Book)
  • Changing Hosts - a Dummies Guide
  • Wordpress to Drupal

Recent comments

  • Got it solved This page here
    3 hours 46 sec ago
  • Links working
    1 day 2 hours ago
  • Thanks... I may be able to
    1 day 8 hours ago
  • 3306 by default
    1 day 18 hours ago
  • Is this the right place to
    1 day 19 hours ago
  • Figured
    3 weeks 6 days ago
  • I'm guessing at this stage
    3 weeks 6 days ago
  • WordPress MU?
    4 weeks 3 hours ago
  • Thanks
    10 weeks 1 day ago
  • I'm running the conversion
    11 weeks 5 days ago

New forum topics

  • What should the port number be
  • WordPress MU?
  • funny little bug in mac version
  • Error: Unable to Insert into Node_revisions table when converting from wordpress 2.6.0 to drupal 6.4
  • index.php?
more

Who's online

There are currently 0 users and 0 guests online.

Who's new

  • oODeathStormOo
  • leruffiant
  • Emtee
  • mnogodet
  • ZioMimmo

loop

Create a triangle type pattern

Submitted by Steve on Thu, 24 Apr, 2008 - 20:49
  • C++
  • for
  • loop

Write a C++ program that asks the user to enter a number of rows to be printed. It should then display for the first row one asterisk preceded by periods. The second row should display two asterisks preceded by periods and so on until all the rows have been printed as entered by the user.

A sample run would be like so:

Enter number of rows: 5
....*
...**
..***
.****
*****

I've seen many varied questions surrounding this particular problem.

Here's a solution, firstly, just the logic of the code for clarity, not concerning ourselves with error trapping incorrect keyboard input. The object is to understand the reasoning of the logic of addressing the problem at hand.

// nestedloop.cpp
// 29 Nov, 2007.

#include <iostream>
using namespace std;

int main()
{
    int rows, c;
    cout << "Enter the number of rows: ";
    cin >> rows;
    cin.get();
    for (int r = 1; r <= rows; r++)
    {
        for (c = 1; c <= rows - r; c++)
            cout << ".";

        for (; c <= rows; c++)
            cout << "*";
        cout << endl;
    }
    // exit routine
    cout << "\n\n...Press 'ENTER' key to EXIT...\n";
    cin.get();
    return 0;
}

Copy and paste the above into your compiler of choice, compile and run it. As you can see, it works.

  • Login or register to post comments
  • Read more
  • 524 reads

 Subscribe in a reader

free hit counter


RoopleTheme