[Solved] I need some assistance with creating a certain shape / pattern [closed]


You should split your code into functions. Then your main function should look like this

int main() {
    const auto rows = []() {std::size_t r; std::cin >> r; return r;}();
    printHeader(rows);
    printDashes(rows);
    printMid(rows);
    printDashes(rows);
    printFooter(rows);
    return 0;
}

The first pyramid can be printed with

void printStarsLine(const std::size_t row, const std::size_t rows) {
    const std::size_t stars = 1 + 2 * row;
    const std::size_t spaces = 2 * rows - row;

    std::cout << std::string(spaces, ' ')
              << std::string(stars, '*')
              << std::string(spaces, ' ')
              << '\n';
}

void printHeader(const std::size_t rows) {
    for (std::size_t i {0}; i < rows; ++i) {
        printStarsLine(i, rows);
    }
}

The bottom pyramid is the top pyramid reverted

void printFooter(const std::size_t rows) {
    for (std::size_t i {rows}; i > 0; --i) {
        printStarsLine(i - 1, rows);
        std::cout << '\n';
    }
}

The dashes are simple dashes

void printDashes(const std::size_t rows) {
    const std::size_t dashes = 2 * rows + 1;

    std::cout << std::string(rows, ' ')
              << std::string(dashes, '-')
              << std::string(rows, ' ')
              << '\n';
}

and the middle part can be printed with

void printMidLine(const std::size_t row, const std::size_t rows) {
    const std::size_t spaces = rows - 1 - row;

    std::cout << std::string(spaces, ' ')
              << std::string(row + 1, '*')
              << '|'
              << std::string(2 * rows - 1, '*')
              << '|'
              << std::string(row + 1, '*')
              << std::string(spaces, ' ')
              << '\n';
}

void printMid(const std::size_t rows) {
    for (std::size_t i {0}; i < rows; ++i) {
        printMidLine(i, rows);
    }
    for (std::size_t i {1}; i < rows; ++i) {
        printMidLine(rows - i - 1, rows);
    }
}

The whole code is

#include <cstddef>
#include <iostream>
#include <string>

void printStarsLine(const std::size_t row, const std::size_t rows) {
    const std::size_t stars = 1 + 2 * row;
    const std::size_t spaces = 2 * rows - row;

    std::cout << std::string(spaces, ' ')
              << std::string(stars, '*')
              << std::string(spaces, ' ')
              << '\n';
}

void printHeader(const std::size_t rows) {
    for (std::size_t i {0}; i < rows; ++i) {
        printStarsLine(i, rows);
    }
}

void printFooter(const std::size_t rows) {
    for (std::size_t i {rows}; i > 0; --i) {
        printStarsLine(i - 1, rows);
    }
}

void printDashes(const std::size_t rows) {
    const std::size_t dashes = 2 * rows + 1;

    std::cout << std::string(rows, ' ')
              << std::string(dashes, '-')
              << std::string(rows, ' ')
              << '\n';
}

void printMidLine(const std::size_t row, const std::size_t rows) {
    const std::size_t spaces = rows - 1 - row;

    std::cout << std::string(spaces, ' ')
              << std::string(row + 1, '*')
              << '|'
              << std::string(2 * rows - 1, '*')
              << '|'
              << std::string(row + 1, '*')
              << std::string(spaces, ' ')
              << '\n';
}

void printMid(const std::size_t rows) {
    for (std::size_t i {0}; i < rows; ++i) {
        printMidLine(i, rows);
    }
    for (std::size_t i {1}; i < rows; ++i) {
        printMidLine(rows - i - 1, rows);
    }
}

int main() {
    const auto rows = []() {std::size_t r; std::cin >> r; return r;}();
    printHeader(rows);
    printDashes(rows);
    printMid(rows);
    printDashes(rows);
    printFooter(rows);
    return 0;
}

The line

printMid(rows);

can be replaced with

for (int i {0}; i < rows; ++i) {
    const int spaces = rows - 1 - i;
    for (int j {0}; j < spaces; ++j) {
        std::cout << ' ';
    }
    for (int j {0}; j < i + 1; ++j) {
        std::cout << '*';
    }
    std::cout << '|';
    for (int j {0}; j < 2 * rows - 1; ++j) {
        std::cout << '*';
    }
    std::cout << '|';
    for (int j {0}; j < i + 1; ++j) {
        std::cout << '*';
    }
    for (int j {0}; j < spaces; ++j) {
        std::cout << ' ';
    }
    std::cout << '\n';
}
for (int i {1}; i < rows; ++i) {
    const int spaces = i;
    for (int j {0}; j < spaces; ++j) {
        std::cout << ' ';
    }
    for (int j {0}; j < rows - i; ++j) {
        std::cout << '*';
    }
    std::cout << '|';
    for (int j {0}; j < 2 * rows - 1; ++j) {
        std::cout << '*';
    }
    std::cout << '|';
    for (int j {0}; j < rows - i; ++j) {
        std::cout << '*';
    }
    for (int j {0}; j < spaces; ++j) {
        std::cout << ' ';
    }
    std::cout << '\n';
}

13

solved I need some assistance with creating a certain shape / pattern [closed]