1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
|
#include <iostream>
#include <string>
int main()
{
int rows(0);
/* Input */
std::cout << "Enter number of rows(odd between 1 and 79) : ";
std::cin >> rows;
while (rows%2 == 0 || rows < 1 || rows > 79) {
std::cin.clear(); // If somebody enters not a number
std::cin.ignore(100, '\n'); // We will skip it and clear errors
std::cout << "You have entred a wrong value\n" <<
"Enter number of rows(odd between 1 and 79) : ";
std::cin >> rows;
}
/* Top part */
for(int i = 0; i <= rows/2; ++i)
std::cout << std::string(rows/2 - i, ' ') <<
std::string( i * 2 + 1, '*') << '\n';
/* Bottom part */
for(int i = rows/2 - 1; i >= 0; --i)
std::cout << std::string(rows/2 - i, ' ') <<
std::string( i * 2 + 1, '*') << '\n';
/* Wait for enter */
std::cin.ignore(100, '\n');
std::cin.get();
}
|
#include <iostream>
#include <string>
#include <cmath>
int main()
{
int rows(0);
/* Input */
std::cout << "Enter number of rows(odd between 1 and 79) : ";
std::cin >> rows;
while (rows%2 == 0 || rows < 1 || rows > 79) {
std::cin.clear(); // If somebody enters not a number
std::cin.ignore(100, '\n'); // We will skip it and clear errors
std::cout << "You have entred a wrong value\n" <<
"Enter number of rows(odd between 1 and 79) : ";
std::cin >> rows;
}
const int half = rows / 2;
for(int i = 0; i < rows; ++i) {
const int spaces = std::abs(half - i);
const int stars = std::abs(spaces - half) * 2 + 1;
std::cout << std::string(spaces, ' ') <<
std::string( stars, '*') << '\n';
}
/* Wait for enter */
std::cin.ignore(100, '\n');
std::cin.get();
} |