HW 1

HW 1.

Task 1. Printing a rectangle

/*
  Author: Alexey Nikolaev
  Description: HW 1. Task 1. Printing a rectangle
 */

#include <iostream>
using namespace std;

int main() {

    int w;
    cout << "Please, enter the width = ";
    cin >> w;

    int h;
    cout << "Please, enter the height = ";
    cin >> h;

    if (w > 0 && h > 0) {
        for (int j = 0; j < h; ++j) {
            for (int i = 0; i < w; ++i) {
                cout << '.';
            }
            cout << endl;
        }
    }

    return 0;
}

Task 2. Printing a grid

/*
  Author: Alexey Nikolaev
  Description: HW 1. Task 2. Printing a grid
 */

#include <iostream>
using namespace std;

int main() {

    int w;
    cout << "Please, enter the width = ";
    cin >> w;

    int h;
    cout << "Please, enter the height = ";
    cin >> h;

    if (w > 0 && h > 0) {
        for (int j = 0; j < h; ++j) {
            for (int i = 0; i < w; ++i) {

                if (i % 5 == 0 || j % 5 == 0)
                    cout << '#';
                else
                    cout << '.';
            }
            cout << endl;
        }
    }

    return 0;
}

Task 3. Graph paper

/*
  Author: Alexey Nikolaev
  Description: HW 1. Task 2. Printing a grid
 */

#include <iostream>
using namespace std;

int main() {

    int w;
    cout << "Please, enter the width = ";
    cin >> w;

    int h;
    cout << "Please, enter the height = ";
    cin >> h;

    if (w > 0 && h > 0) {
        for (int j = 0; j < h; ++j) {
            for (int i = 0; i < w; ++i) {

                if (i % 5 == 0 || j % 5 == 0)
                    cout << '#';
                else
                    cout << '.';
            }
            cout << endl;
        }
    }

    return 0;
}

Task 4. Diamond

I have 3 possible solutions that use slightly different approaches

Solution 1

/*
  Author: Alexey Nikolaev
  Description: HW 1. Task 4. Diamond

  The idea:

  Let N = 5. The center of the diamond is marked by X:

    *
   ***
  **X**
   ***
    *

  We draw a star '*' at (x,y) when |x - x_center| + |y - y_center| <= n/2

  By the way, this definition of distance is called L1 metric,
    or Taxicab metric:

    https://en.wikipedia.org/wiki/Taxicab_geometry

    (in particular, look at the section "Circles in Taxicab geometry")

 */

#include <iostream>
#include <cstdlib>

using namespace std;

int main() {

    int n = 0;
    do {
        cout << "Please, enter a positive odd number: ";
        cin >> n;
    } while (n % 2 != 1 || n <= 0);

    // the coordinates of the center of the diamond
    int ic = n / 2;
    int jc = n / 2;

    for (int j = 0; j < n; ++j) {
        for (int i = 0; i < n; ++i) {

            if ( abs(i-ic) + abs(j-jc) <= n/2 )
                cout << '*';
            else
                cout << ' ';

        }
        cout << endl;
    }

    return 0;
}

Solution 2

/*
  Author: Alexey Nikolaev
  Description: HW 1. Task 4. Diamond

  An alternative solution:
  The idea is to count how many stars and whitespaces must be printed
  on each row:

    Conisder an example, N = 5:

         |01234     How many stars is in the row?
     ----|------
   j = 0 |  *         1
       1 | ***        3
       2 |*****       5
       3 | ***        3
       4 |  *         1

    We can express the number of stars as a function of j:
    For the row j, the number of stars = N - 2|j - N/2|

    Therefore, for each row, we have to print:
      First, |j - N/2| whitespaces,
      Then, N - 2|j - N/2| stars.

 */

#include <iostream>
#include <cstdlib>

using namespace std;

int main() {

    int n = 0;
    do {
        cout << "Please, enter a positive odd number: ";
        cin >> n;
    } while (n % 2 != 1 || n <= 0);

    for (int j = 0; j < n; ++j) {

        // let x = |j - N/2|
        int x = abs(j - n/2);

        // first, we draw x whitespaces
        for (int i = 0; i < x; ++i)
            cout << ' ';

        // then draw N - 2x stars
        for (int i = 0; i < n - 2*x; ++i)
            cout << '*';

        cout << endl;
    }

    return 0;
}

Solution 3

/*
  Author: Alexey Nikolaev
  Description: HW 1. Task 4. Diamond

  Another alternative solution:

        How many stars is in the row?

     *         1
    ***        3
   *****       5
    ***        3
     *         1

  Observe that the number of stars:
     is inceremented by 2 in the top half of the diamond, and
     is deceremented by 2 at the bottom half of the diamond

 */

#include <iostream>

using namespace std;

int main() {

    int n = 0;
    do {
        cout << "Please, enter a positive odd number: ";
        cin >> n;
    } while (n % 2 != 1 || n <= 0);

    // the number of stars
    // we start with just 1 star at the top row
    int stars = 1;

    for (int j = 0; j < n; ++j) {

        // how many whitespaces to draw?
        int whitespaces = (n - stars) / 2;

        for (int i = 0; i < whitespaces; ++i)
            cout << ' ';

        for (int i = 0; i < stars; ++i)
            cout << '*';

        cout << endl;

        // increment the number of stars +2 if we are above the middle.
        // or decrement it -2, if we are below the middle
        if (j < n/2)
            stars += 2;
        else
            stars -= 2;

    }

    return 0;
}