Skip to content

Commit aab88e3

Browse files
committed
Create main.cpp
1 parent 25c9a0f commit aab88e3

File tree

1 file changed

+182
-0
lines changed

1 file changed

+182
-0
lines changed

cpp_homework_15/main.cpp

Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
//
2+
// main.cpp
3+
// cpp_homework_15
4+
//
5+
// Created by Mark Semenov on 11.07.2022.
6+
//
7+
8+
#include <iostream>
9+
using namespace std;
10+
11+
int random(int MIN, int MAX) {
12+
int k = MAX - MIN + 1;
13+
return MIN + rand() % k;
14+
}
15+
16+
int main(int argc, const char * argv[]) {
17+
//Task 1
18+
const int s = 5;
19+
int n[s] = {};
20+
for (int i = 0; i < s; i++) {
21+
cout << "Enter number #" << i + 1 << ": ";
22+
cin >> n[i];
23+
}
24+
for (int i = s - 1; i >= 0; i--) {
25+
cout << n[i] << "\n";
26+
}
27+
cout << "\n";
28+
29+
//Task 2
30+
const int s = 20;
31+
int n[s] = {};
32+
srand(time(NULL));
33+
rand();
34+
for (int i = 0; i < s; i++) {
35+
n[i] = rand();
36+
if (i % 2 == 0) {
37+
cout << n[i] << "\n";
38+
}
39+
}
40+
cout << "\n";
41+
42+
//Task 3
43+
const int s = 10;
44+
int n[s] = {}, a = 0, sum = 0;
45+
while (true) {
46+
srand(time(NULL));
47+
for (int i = 0; i < s; i++) {
48+
n[i] = random(-10, 10);
49+
if (n[i] > 0) {
50+
a++;
51+
sum += n[i];
52+
}
53+
}
54+
break;
55+
}
56+
cout << "Quantity = " << n << "\nSum = " << sum << "\nAverage = " << (double)sum / a << "\n\n";
57+
58+
//Task 4
59+
const int s = 100;
60+
int symbol[s] = {}, letter = 0, num = 0, symb = 0;
61+
while (true) {
62+
srand(time(0));
63+
for (int i = 0; i < s; i++) {
64+
symbol[i] = random(0, 127);
65+
if (symbol[i] >= 48 && symbol[i] <= 57) {
66+
num++;
67+
cout << (char)symbol[i] << ", ";
68+
}
69+
else if ((symbol[i] >= 65 && symbol[i] <= 90) || (symbol[i] >= 97 && symbol[i] <= 122)) {
70+
letter++;
71+
cout << (char)symbol[i] << ", ";
72+
}
73+
else {
74+
symb++;
75+
cout << (char)symbol[i] << ", ";
76+
}
77+
}
78+
break;
79+
}
80+
cout << "\n";
81+
cout << "Letters are " << letter << "%\nNumbers are " << num << "%\nSymbols are " << symb << "%\n\n";
82+
83+
//Task 5
84+
const int s = 100;
85+
int n[s] = {}, a, c = 0;
86+
cin >> a;
87+
while (true) {
88+
srand(time(NULL));
89+
for (int i = 0; i < s; i++) {
90+
n[i] = rand();
91+
if (n[i] == a) {
92+
c++;
93+
}
94+
}
95+
break;
96+
}
97+
cout << "Number repeats " << c << " times" << "\n\n";
98+
99+
//Task 6
100+
const int s = 20;
101+
int n[s] = {}, count, sum = 0;
102+
while (true) {
103+
srand(time(NULL));
104+
for (int i = 0; i < s; i++) {
105+
n[i] = random(-10, 30);
106+
if (n[i] < 0) {
107+
count = i;
108+
break;
109+
}
110+
}
111+
for (int i = count; i < s; i++) {
112+
n[i] = random(-10, 30);
113+
sum += n[i];
114+
}
115+
break;
116+
}
117+
cout << "Sum after first negative number is equal to " << sum << "\n\n";
118+
119+
//Task 7
120+
const int s = 20;
121+
int n[s] = {}, max = 0, min, max_i, min_i;
122+
while (true) {
123+
srand(time(NULL));
124+
for (int i = 0; i < s; i++) {
125+
n[i] = rand();
126+
cout << n[i] << " ";
127+
if (n[i] > max) {
128+
max = n[i];
129+
max_i = i;
130+
}
131+
}
132+
min = max;
133+
for (int i = 0; i < s; i++) {
134+
if (n[i] < min) {
135+
min = n[i];
136+
min_i = i;
137+
}
138+
}
139+
break;
140+
}
141+
cout << "\n";
142+
cout << max << " #" << max_i + 1 << endl << min << " #" << min_i + 1 << "\n\n";
143+
144+
//Task 8
145+
const int s = 100;
146+
double n[s] = {};
147+
int counter = 0;
148+
while (true) {
149+
srand(time(NULL));
150+
for (int i = 0; i < s; i++) {
151+
n[i] = round(rand() / (double)RAND_MAX * 10000) / 10;
152+
if (n[i] - (int)n[i] == 0) {
153+
counter++;
154+
}
155+
}
156+
break;
157+
}
158+
cout << counter << "% of numbers don`t have a fractional part\n\n";
159+
160+
//Task 9
161+
const int s = 200;
162+
int n[s] = {}, o = 0, t = 0, th = 0;
163+
while (true) {
164+
srand(time(NULL));
165+
for (int i = 0; i < s; i++) {
166+
n[i] = random(0, 200);
167+
if (n[i] / 10 == 0) {
168+
o++;
169+
}
170+
else if (n[i] / 10 > 0 & n[i] / 10 < 10) {
171+
t++;
172+
}
173+
else {
174+
th++;
175+
}
176+
}
177+
break;
178+
}
179+
cout << "Single-digit numbers: " << o / 2.0 << "%" << "\n";
180+
cout << "Double-digit numbers: " << t / 2.0 << "%" << "\n";
181+
cout << "Three-digit numbers: " << th / 2.0 << "%" << "\n";
182+
}

0 commit comments

Comments
 (0)