#include <string>
#include <iostream>
#include <ctime>
#include <iomanip>
using namespace std;
int getInteger(const char* text)
{
bool isError = false;
std::string s;
int number = 0;
do {
isError = false;
std::cout << text << std::endl;
std::cin >> s;
for (size_t i = 0; i < s.length(); i++)
{
if ((int)(s[i]) < 0 || !isdigit(s[i])) //if (!(s[i] >= '0' && s[i] <= '9'))
{
isError = true;
break;
}
}
if (!isError)
number = atoi(s.c_str());
} while (isError || number <= 0);
return number;
}
void task5()
{
cout << "TASK 5" << endl;
const int N = 12;
int arrNum[N];
for (int i = 0; i < N ; i++)
{
cout << "Element " << setw(2) << (i+1) << "= "<< setw(2) << (arrNum[i] = i + 1) << endl;
}
cout << endl;
}
void task40()
{
cout << "TASK 40" << endl << endl;
const int arrSize = 20;
int arr[arrSize];
cout << "Before: ";
for (int i = 0; i <arrSize; i++)
{
cout << setw(3) << (arr[i] = rand() % 10 + 1);
}
arr[rand() % arrSize] = 0;
cout << endl << endl;
cout << " After: ";
for (int i = 0; i < arrSize; i++)
{
cout << setw(3) << arr[i];
if (arr[i] == 0)
{
break;
}
}
cout << endl << endl << endl;
}
void task75()
{
cout << "TASK 75 " << endl;
int size = getInteger("Input size of array");
int *arr = new int[size];
for (int i = 0; i < size - 1; i++)
{
arr[i] = rand() % 98 + 1;
cout << arr[i] ;
if (arr[i] == 77)
{
cout << "(+)";
}
cout << "\t";
}
cout << (arr[(size-1)] = 100) << endl;
cout << endl;
int i = 0;
while (arr[i] != 77 && i != size )
{
i++;
}
i == size ? cout << endl << "Number 77 none" : cout << endl << "Index of number 77 is: " << i;
cout << endl << endl;
}
void task110()
{
cout << "TASK 110" << endl;
int sizeArr = getInteger("Input size of array");
cout << endl;
int* integer = new int[sizeArr];
for (int i = 0; i < sizeArr; i++)
{
integer[i] = rand() % 100;
cout << integer[i] << "\t";
}
cout << endl;
again:
int c = getInteger("Enter place of array");//с - место смены значения массива
if (c > sizeArr) goto again;
cout << endl;
cout << "First element * last element: " << (integer[(c-1)] = integer[0] * integer[(sizeArr-1)]) << endl << endl;
for (int i = 0; i < sizeArr; i++)
{
if (i == ((c-1)))
{
cout << integer[(c - 1)] << "(+)" << "\t";
}
else
{
cout << integer[i] << "\t";
}
}
cout << endl << endl;
}
void task145()
{
cout << "TASK 145" << endl;
const int arrSize = 10;
int numsOfAnimals[arrSize];
string animals[arrSize] = { "macaque", "owl","crane","panda","fish","white-breasted bear","raccoon dog","serau","hares","seagulls" }; //array of animals
int listeners = getInteger("Enter number of listeners");
string* numb = new string[listeners];
//заполняем массив ответами
for ( int i = 0; i < listeners; i++)
{
cout << "Listener " << i+1 << ", answer: " << (numb[i] = animals[(rand() % 10)]) << endl;
}
cout << endl;
for (int n = 0; n < arrSize; n++) numsOfAnimals[n] = 0;//счетчик количества животных (изначально заполняем нулями)
//ищим для каждого слушателя его ответ и записываем в счетчик
for (int i = 0; i < listeners; i++)
{
for (int u = 0; u < arrSize; u++)
{
if (numb[i] == animals[u])
{
numsOfAnimals[u]++;//счетчик количества животных(заполняем)
}
}
}
cout << endl;
int theOftenAnimal = 0;//наиболее частое животное
for (int i = 0; i < arrSize; i++)
{
if (numsOfAnimals[i] > theOftenAnimal)// ищем найболее частых животных
{
theOftenAnimal = numsOfAnimals[i];
}
}
for (int i = 0; i < arrSize; i++)
{
if (numsOfAnimals[i] == theOftenAnimal) cout << "Most meeting animal(s) is: " << animals[i] << " | " << numsOfAnimals[i] <<"| " << double(numsOfAnimals[i])*100/double(listeners)<< "%" << endl;// проверяем и ввыводим найболее частых
}
cout << endl << endl;
}
void task180()
{
cout << "TASK 180 " << endl;
int arrSize = getInteger("Enter size of array");
double* array = new double[arrSize];
cout << endl;
for (int a = 0; a < arrSize; a++)
{
cout << "Number:" << setw(2) << (a + 1) << setw(2) << "= " << setw(2) << (array[a] = 8 - rand() % 15 );
if (array[a] > 0)
{
cout << "(+)";
}
cout << endl;
}
int count = 0;//счетчик положительных чисел
int countMax = 0;// счетчик наибольшего количества последовательно расположенных положительных чисел
for ( int a = 0; a < arrSize; a++)
{
if (array[a] > 0)
{
count++;
}
else
{
if (count > countMax)
{
countMax = count;
}
count = 0;
}
}
cout << "Largest sequence of positive numbers: " << countMax << endl;
cout << endl << endl;
}
int main()
{
srand(time(0));
int c = 0;
while (c != 1)
{
int inputnumber = getInteger("Choose task ( 5, 40, 75, 110, 145, 180): ");
cout << endl;
switch (inputnumber)
{
case 5:
task5();
break;
case 40:
task40();
break;
case 75:
task75();
break;
case 110:
task110();
break;
case 145:
task145();
break;
case 180:
task180();
break;
}
}
}