C/C++

Задачи на тему циклов C++

Написать код можно на любом ЯП.
Ex. 5
Write a program which generates pattern of O. Number of lines take from user.
e.g.
input: 4
output:
OOOO
OOO
OO
O

Ex. 6
Write a program which generates pattern of O. Size of pattern take from user.
e.g.
input: 5
output:
VVVVV
VVVV
VVV
VV
V
VV
VVV
VVVV
VVVVV

Ex. 7
Write a program which generate table:
1 2 3 4
2 3 4 5
3 4 5 6
4 5 6 7

Ex. 8
Write a program which generate upper triangular matrix:
2 2 1
0 3 2
0 0 5

Ex. 9
Write a program which generate lower triangular matrix:
2 0 0
3 2 0
1 2 0

Ex. 10
Write a program which generate diagonal matrix:
1 0 0
0 2 0
0 0 3

Ex. 11
Write a program which generate table:
1 2 3 4
2 1 2 3
3 2 1 2
4 3 2 1

Ex. 12
Write a program which generates pattern of O. Size of pattern take from user.
e.g.
input: 5
output:
*
**
***
****
*****
****
***
**
*
#include <iostream>
#include <string>
#include <cmath>
#include <conio.h>
using namespace std;
unsigned integer(const char* msg = "input: ") {
cout << msg;
unsigned value;
cin >> value;
return value;
}
void ex5(char x = 'O') {
auto n = integer();
puts("output:");
for (auto i = n; i != 0U; --i) cout << string(i, x) << '\n';
puts("");
}
void ex6(char x = 'V') {
auto n = integer();
puts("output:");
for (auto i = n; i != 0U; --i) cout << string(i, x) << '\n';
for (auto i = 2U; i <= n; ++i) cout << string(i, x) << '\n';
puts("");
}
void ex7(int n = 4) {
puts("output:");
for (auto i = 1; i <= n; ++i) {
for (auto j = i; j < n + i; ++j) cout << j << ' ';
puts("");
}
puts("");
}
void ex8(int x = 5, int n = 3) {
puts("output:");
for (auto i = n, k = 0; i != 0; --i, ++k) {
auto m = static_cast<int>(ceil(double(x) / i));
auto s = x;
auto j = 0;
while (j < k) {
cout << "0 ";
++j;
}
cout << m << ' ';
while (++j < n) {
s -= m;
if (m <= s) cout << m << ' ';
else cout << s;
}
puts("");
}
puts("");
}
void ex9(int x = 2, int n = 3) {
puts("output:");
int e = x, m = x;
for (auto i = 0; i < n; ++i) {
if (i & 1) {
e = m = x;
e += i;
m -= i;
}
if (e > x) {
for (auto i = 0; i < n; ++i) {
if (e >= x) cout << e << ' ';
else cout << "0 ";
--e;
}
} else {
for (auto i = 0; i < n; ++i) {
if (m <= x) cout << m << ' ';
else cout << "0 ";
++m;
}
}
puts("");
}
puts("");
}
void ex10(int n = 3) {
puts("output:");
for (auto i = 0; i < n; ++i) {
for (auto j = 0; j < n; ++j) {
if (i == j) cout << i + 1 << ' ';
else cout << "0 ";
}
puts("");
}
puts("");
}
void ex11(int n = 4) {
puts("output:");
for (auto i = 0; i < n; ++i) {
for (auto j = i; j != 0; --j) cout << j + 1 << ' ';
for (auto j = 1; j <= n - i; ++j) cout << j << ' ';
puts("");
}
puts("");
}
void ex12(char x = '*') {
auto n = integer();
puts("output:");
unsigned i;
for (i = 1U; i <= n; ++i) cout << string(i, x) << '\n';
for (i -= 2U; i != 0U; --i) cout << string(i, x) << '\n';
puts("");
}
int main() {
while (true) {
cout << "Exercise: ";
int ex;
cin >> ex;
switch (ex) {
case 5: ex5(); break;
case 6: ex6(); break;
case 7: ex7(); break;
case 8: ex8(); break;
case 9: ex9(); break;
case 10: ex10(); break;
case 11: ex11(); break;
case 12: ex12(); break;
default: puts("This is a shame!");
}
cout << "Press Esc to exit\nPress any key to continue... ";
if (_getch() == 0x1B) break;
system("cls");
}
system("pause > nul");
}
ПГ
Петр Грибок
99 543
Лучший ответ