SOURCE FILE: varStars.cpp



/*

With this program, the user can make a field of stars 
like those in the USA flag, but of any desired dimensions.

*/

#include <iostream>
using namespace std ;

void GetDimensions(int & width_param, int & rows_param) ;
    /* From the user, gets the desired width and 
       number of rows of the star field. */

void MakeField (int rows_Arg, int width_Arg) ;
     /* make a field of stars 
        with the specified number of rows and specified width */

int main()
{
  int width, number_of_rows ;

  GetDimensions(width, number_of_rows) ;
  MakeField (number_of_rows, width) ;

  return 0;
}

void GetDimensions(int & width_param, int & rows_param)
{
  cout << "How wide do you want your field of stars? " ;
  cin  >> width_param ;
  cout << "How many rows do you want in your field of stars? " ;
  cin  >> rows_param ;
}

void MakeLong(int width_par) ;
      /* Make a row of asterisks like this:
         *   *   * 
         width_par is the number of asterisks. 
         The first asterisk is printed in the 
         FIRST column of the display. */

void MakeShort(int width_par) ; 
      /* Make a row of asterisks like this:
           *   *   * 
         width_par is the number of asterisks. 
         The first asterisk is printed in the 
         THIRD column of the display. */

void MakeField (int rows_Param, int width_Param)
{
   int rowNum ;

   cout << endl ; /* format -- make a blank line first */

   for (rowNum=1; rowNum <= rows_Param; rowNum++)
   {
          /* Here we carefully plan the logic.
             We want alternating rows: long, short, long, ...
             So .. if rowNum is odd then make a long row,
             else make a short row. */

     if    (rowNum % 2 == 1) 
           MakeLong(width_Param); 
     else  MakeShort(width_Param-1) ; 
   }

   cout << endl ; /* format -- make a blank line last */
}

void MakeCommonPartOfRow (int width_Arg) ;
     /* A function to make the part of a star-row that has the same stucture
        in both the 'Long' and the 'Short' rows.  It makes a line on 
        standard input with this string: "   *" 
        repeated width_Arg times. */

void MakeLong(int width_par) 
{
          /* make the special start-pattern for a long row */
   cout << "*" ; 

         /* Now call a helper-function to make the (not special) rest of
            the row. */
   MakeCommonPartOfRow (width_par - 1) ;
}

void MakeShort(int width_par) 
{
          /* make the special start-pattern for a short row */
   cout << "  *" ; 

         /* Now call a helper-function to make the (not special) rest of
            the row. */
   MakeCommonPartOfRow (width_par - 1) ;
}

void MakeCommonPartOfRow (int width_par) 
{
   int numDone ;
          /* Execute a loop to repeat '3 spaces + star' enough times */
   for (numDone=1; numDone <= width_par; numDone++)
   {
      cout << "   *" ;
   }
          /* finally go to a new line after the row is complete */

   cout << endl ; 
}