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 C++ (The Book) Short Programs


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)
    • Basic cin operations
    • Short Programs
      • Create a triangle type pattern
        • Enhancement # 1 - Trap non numeric input
        • Enhancement # 2 - Valid Input Range
        • Enhancement # 3 - Trap number followed by non numeric
      • Array Solution - Half and half, 10 per line
      • Calculate Pace (Running Program)
      • Calculate Pace (Running Program) - # 2
      • Remove Vowels
      • Remove Vowels - # 2 - String version
    • Compiler/Linker Error Messages
  • Changing Hosts - a Dummies Guide
  • Wordpress to Drupal

Recent comments

  • Thanks..
    37 weeks 1 day ago
  • Hmmm,Interesting one,thx for
    38 weeks 2 days ago
  • Buyer beware
    39 weeks 1 day ago
  • REPLY:Actual Processes Involved
    39 weeks 2 days ago
  • Back to Ruby
    48 weeks 4 days ago
  • Links provided
    1 year 11 weeks ago
  • Module for wordpress to Drupal 6.x
    1 year 11 weeks ago
  • The wordpress plugin looks
    1 year 13 weeks ago
  • Good point..... You're
    1 year 13 weeks ago
  • Many thanks. If I'm going to
    1 year 13 weeks ago

New forum topics

  • Imported posts only visible to user1
  • 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
more

Sponsored links

Steve's Stuff
All about my running and from time to time other stuff

Improve Memory
All about memory techniques, excellent and relevant articles.

Create a triangle type pattern

Submitted by Steve on Thu, 24 Apr, 2008 - 19: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.

The guts of the solution is that an outer loop is first created, that relating to the rows. Within this loop the columns are accounted for and for this particular problem the columns loop is split into two, one dealing with the periods and the other dealing with the asterisks. Now trying to explain the sequence of the variables concerned will take forever. I reckon the code alone should clearly explain what is happening.

I mentioned earlier that this code doesn't account for incorrect data input, try it, enter a non numeric for the number of rows. The program doesn't work.

The following pages discuss enhancements to the program.

  • Enhancement # 1 - Trap non numeric input
  • Enhancement # 2 - Valid Input Range
  • Enhancement # 3 - Trap number followed by non numeric
‹ Short Programs up Enhancement # 1 - Trap non numeric input ›
  • Printer-friendly version
  • Add new comment
  • 1377 reads

 Subscribe in a reader

free hit counter


RoopleTheme