COM 1100 Fall 2000 -- Prof. Futrelle -- Lab 6 Directions

Using iteration and conditionals for pattern generation. String analysis.

Lab date: Wednesday November 8th. FLASH!! THIS LAB NOW DUE TUESDAY NOON, NOVEMBER 14th
These directions were posted Wednesday, November 8th.

SPECIAL NOTICE -- THIS CAN BE DONE AS A TEAM PROJECT WITH UP TO THREE PEOPLE, NO MORE. To do this, you must devote a great deal of attention to an extensive write-up that carefully explains what each member of the team did. This is not to be written in generalities, but must refer to specific strategies, code, debugging, testing, writing up, etc., that the various team members did. If there was serious sharing on some specific items, give an estimate of the percentage that each team member contributed to it. There may be more documentation of this type than actual code. That's as it should be. If there are two or three people on the team, I expect approximately two or three times as much work, total, e.g, 6 or 8 distinct patterns possibly including some complex ones.

Goal of the Lab #6:

Part A: To develop programs that use iteration (for and while) and conditionals (if) to generate patterns on the screen -- arrangements of characters such as the following:


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

Part B: To further use iterations and conditionals as well as simple string operations, to discover patterns in text such as determining that the following string has exactly 3 upper-case characters in it: "Gotta Tell You", or that the following string cannot be a proper Social Security Number "393938484A".

Generating Patterns on the screen:

The simplest way to describe what you'll be doing is to give a few code snippets that will produce some simple patterns.

This snippet:
int tot = 20;
for(int i = 0; i < tot; i++)
  {
  cout << '*';
  }

produces this:
********************

This snippet:
int tot = 20;
int gap = 5;
for(int i = 0; i < tot; i++)
  {
   if(i%gap == 0)
      cout << '*';
   else cout << ' ';
  }

produces this:
*    *    *    *

If the last snippet of code is placed inside another loop it can produce a pattern such as this:

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

Finally, we can arrange for the position of a character to depend on where in the outer loop it is (which line), so that this snippet of code:
  int width = 10;
  int height = 10;
  for(int h = 0; h < height; h++)
    {
    for(int i = 0; i < width; i++)
      {
        if(i == h)
          cout << '*';
        else cout << ' ';
      }
    cout << endl;
    }

will produce this pattern:

*
 *
  *
   *
    *
     *
      *
       *
        *
         *

Note carefully that the first or "outer" for loop has a body that consists of the inner for loop, one for each line, followed by an endl for each line.

The directions for Part A of the lab assignment, pattern generation:

The patterns referred to in the directions are:

Square:

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

Tic-Tac-Toe:
 
    *     *  
    *     *  
    *     *      
***************
    *     *  
    *     *  
    *     *  
***************
    *     *  
    *     *  
    *     *  

N:

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

Extra credit: Try writing code that will produce a sine wave or a pattern such as this:

          *         
         * *             *         
        *   *           * *           *
       *     *         *   *         * *        *
      *********       *******       *****      *** 
          |              |            |         |
   -------------------------------------------------

The directions for Part B of the lab assignment, string analysis:

Here are some samples of code that do simple string analysis:

This snippet of code tells the user whether or not it has found an upper case letter in the string entered.

// COM 1100, Fall 2000, Prof. Futrelle
// Pattern generation and analysis
// l6.cpp, 11/06/2000

#include <iostream>
#include <string>

using namespace std;

void main()
{
  string input;
  cout << "Please enter a string (with no blanks -- underscores OK): ";
  cin >> input;
  int len = input.length();

  bool flag = false;
  char c;

  for(int i=0; i <len; i++)
    {
      c = input.at(i);
      if((c >= 'A') && (c <= 'Z'))
        flag = true;
    }


  if(flag) cout << "\n\nFound an upper-case character in: ";
  else cout << "\n\nNo upper-case characters found in : ";

  cout << input << endl;
}