Показаны сообщения с ярлыком do while. Показать все сообщения
Показаны сообщения с ярлыком do while. Показать все сообщения

24 апреля 2016 г.

Реализовать программу, которая позволит пользователю самому переставлять элементы массива. Изначально массив заполняется случайными значениями(диапазон этих значений задает пользователь). Затем пользователь указывает индекс того элемента, который он хочет переместить, на экран выводиться массив без этого элемента. Затем пользователь указывает индекс, того места куда необходимо вставить элемент, который достали. Если пользователь хочет завершить сортировку, достаточно ввести индекс несуществующего элемента.

setlocale(LC_ALL,"rus");

const int size = 10;

int A[size]{};

for (int i = 0; i < size; i++)

A[i] = rand() % 51;

int digit;

do

{

cout << "Massive A:" << endl;
for (int i = 0; i < size; i++) {
A[i] = rand() % 51;
cout.width(3);
cout << A[i];
}
cout << endl;
cout << "Выберите элемент, который хотите переставить:";
cin >> digit;
int tmp_value = A[digit - 1];
int tmp_index = digit - 1;
for (int i = 0; i < size; i++) {
if (i == digit-1)
continue;
cout.width(3);
cout << A[i];
}
cout << endl;
cout << "Выберите место, куда вы хотите его вставить:";
cin >> digit;



A[tmp_index] = A[digit - 1];
A[digit - 1] = tmp_value;
for (int i = 0; i < size; i++) {
cout.width(3);
cout << A[i];
}
cout << endl;
cout << "Если хотите продолжить, нажмите 1:";
cin >> digit;

} while (digit == 1);




system("pause");

Дан двумерный массив размерностью N x M, заполненный случайными числами из диапазона от 0 до 100. Выполнить циклический сдвиг массива на заданное количество столбцов. Направление сдвига задаёт пользователь.

srand(time(0));

int choice;

int count;

const int length = 8;

const int width = 5;

int A[width][length]{};

for (size_t i = 0; i < width; i++) {

for (size_t j = 0; j < length; j++) {
A[i][j] = rand() % 100;
cout.width(3);
cout << A[i][j];
}
cout << endl;

}

cout << endl << endl;

do {

cout << "Enter a shift direction of the array:" << endl
<< "1 - Left" << endl << "2 - Right" << endl;
cin >> choice;

} while (choice != 1 && choice != 2);

cout << "Enter count:" << endl;

cin >> count;



switch (choice) {

case 1:

for (int k = 0; k < count; k++) {
for (size_t i = 0; i < width; i++) {
int tmp = A[i][0];
for (size_t j = 0; j < length-1; j++)
A[i][j] = A[i][j + 1];
A[i][length-1] = tmp;
}
}
break;

case 2:

for (int k = 0; k < count; k++) {
for (size_t i = 0; i < width; i++) {
int tmp = A[i][length - 1];
for (size_t j = length - 1; j > 0; j--)
A[i][j] = A[i][j - 1];
A[i][0] = tmp;
}
}
break;

}

cout << endl << endl;

cout << "Result:" << endl;

for (size_t i = 0; i < width; i++) {

for (size_t j = 0; j < length; j++) {
cout.width(3);
cout << A[i][j];
}
cout << endl;

}

system("pause");

Дан двумерный массив размерностью N x M, заполненный случайными числами из диапазона от 0 до 100. Выполнить циклический сдвиг массива на заданное количество строк. Направление сдвига задаёт пользователь.

srand(time(0));

int choice;

int count;

const int length = 8;

const int width = 5;

int A[width][length]{};

for (size_t i = 0; i < width; i++) {

for (size_t j = 0; j < length; j++) {
A[i][j] = rand() % 100;
cout.width(3);
cout << A[i][j];
}
cout << endl;

}

cout << endl << endl;

do {

cout << "Enter a shift direction of the array:" << endl
<< "1 - Up" << endl << "2 - Down" << endl;
cin >> choice;

} while (choice != 1 && choice != 2);

cout << "Enter count:" << endl;

cin >> count;



switch (choice) {

case 1:

for (int k = 0; k < count; k++) {
for (size_t j = 0; j < length; j++) {
int tmp = A[0][j];
for (size_t i = 0; i < width-1; i++)
A[i][j] = A[i+1][j];
A[width-1][j] = tmp;
}
}
break;

case 2:

for (int k = 0; k < count; k++) {
for (size_t j = 0; j < length; j++) {
int tmp = A[width-1][j];
for (size_t i = width - 1; i > 0; i--)
A[i][j] = A[i-1][j];
A[0][j] = tmp;
}
}
break;

}

cout << endl << endl;

cout << "Result:" << endl;

for (size_t i = 0; i < width; i++) {

for (size_t j = 0; j < length; j++) {
cout.width(3);
cout << A[i][j];
}
cout << endl;

}

system("pause")

Поместить в одномерный массив числа от 1 до 30 (31). И показать календарь на текущий месяц.


int month;

do {

cout << "Enter number of month(1-12):";
cin >> month;

} while (month < 1 || month > 12);

cout << "Mo\tTu\tWe\tTh\tFr\tSa\tSu" << endl;

switch (month) {

case 1:{

int A[31]{};
for (int i = 1; i <= 31; i++)
A[i - 1] = i;
for (int i = 0; i < 35; i++) {
if (i < 4) {
cout << "\t";
continue;
}
cout << A[i - 4] << "\t";
if ((i + 1) % 7 == 0)
cout << endl;
}
cout << endl;
}

break;

case 2:{

int A[29]{};
for (int i = 1; i <= 29; i++)
A[i - 1] = i;
for (int i = 0; i <29; i++) {
cout << A[i] << "\t";
if ((i + 1) % 7 == 0)
cout << endl;
}
cout << endl;
}

break;

case 3: {

int A[31]{};
for (int i = 1; i <= 31; i++)
A[i - 1] = i;
for (int i = 0; i < 32; i++) {
if (i == 0) {
cout << "\t";
continue;
}
cout << A[i - 1] << "\t";
if ((i + 1) % 7 == 0)
cout << endl;
}
cout << endl;
}
break;

case 4: {

int A[30]{};
for (int i = 1; i <= 30; i++)
A[i - 1] = i;
for (int i = 0; i < 34; i++) {
if (i < 4) {
cout << "\t";
continue;
}
cout << A[i - 4] << "\t";
if ((i + 1) % 7 == 0)
cout << endl;
}
cout << endl;
}
break;

case 5: {

int A[31]{};
for (int i = 1; i <= 31; i++)
A[i - 1] = i;
for (int i = 0; i < 37; i++) {
if (i < 6) {
cout << "\t";
continue;
}
cout << A[i - 6] << "\t";
if ((i + 1) % 7 == 0)
cout << endl;
}
cout << endl;
}
break;

case 6: {

int A[30]{};
for (int i = 1; i <= 30; i++)
A[i - 1] = i;
for (int i = 0; i < 32; i++) {
if (i < 2) {
cout << "\t";
continue;
}
cout << A[i - 2] << "\t";
if ((i + 1) % 7 == 0)
cout << endl;
}
cout << endl;
}
break;

case 7: {

int A[31]{};
for (int i = 1; i <= 31; i++)
A[i - 1] = i;
for (int i = 0; i < 35; i++) {
if (i < 4) {
cout << "\t";
continue;
}
cout << A[i - 4] << "\t";
if ((i + 1) % 7 == 0)
cout << endl;
}
cout << endl;
}
break;

case 8: {

int A[31]{};
for (int i = 0; i < 31; i++) {
A[i - 1] = i+1;
cout << A[i-1] << "\t";
if ((i + 1) % 7 == 0)
cout << endl;
}
cout << endl;
}
break;

case 9: {

int A[30]{};
for (int i = 1; i <= 30; i++)
A[i - 1] = i;
for (int i = 0; i < 33; i++) {
if (i < 3) {
cout << "\t";
continue;
}
cout << A[i - 3] << "\t";
if ((i + 1) % 7 == 0)
cout << endl;
}
cout << endl;
}
break;

case 10: {

int A[31]{};
for (int i = 1; i <= 31; i++)
A[i - 1] = i;
for (int i = 0; i < 36; i++) {
if (i < 5) {
cout << "\t";
continue;
}
cout << A[i - 5] << "\t";
if ((i + 1) % 7 == 0)
cout << endl;
}
cout << endl;
}
break;

case 11: {

int A[30]{};
for (int i = 1; i <= 30; i++)
A[i - 1] = i;
for (int i = 0; i < 31; i++) {
if (i==0) {
cout << "\t";
continue;
}
cout << A[i - 1] << "\t";
if ((i + 1) % 7 == 0)
cout << endl;
}
cout << endl;
}
break;

case 12: {

int A[31]{};
for (int i = 1; i <= 31; i++)
A[i - 1] = i;
for (int i = 0; i < 34; i++) {
if (i < 3) {
cout << "\t";
continue;
}
cout << A[i - 3] << "\t";
if ((i + 1) % 7 == 0)
cout << endl;
}
cout << endl;
}
break;

}

system("pause");

Программа находит максимальное число в определённой области в двумерном массиве.

void main() {


srand(time(NULL));
setlocale(LC_ALL, "rus");

const int q = 6;
int A[q][q];
for (int i = 0; i<q; i++) {
for (int j = 0; j<q; j++) {
A[i][j] = rand() % 100;
cout << A[i][j] << "\t";
}
cout << "\n\n";
}
cout << "\n\n";
char choice;
do
{
int max = A[0][0];
cout << "Программа находит максимальное число в заданной области" << endl;
cout << "1-*** 2-*.. 3-*** 4-..." << endl
<< " .** **. .*. .*." << endl
<< " ..* *** ... ***" << endl << endl
<< "5-*** 6-*.* 7-*.. 8-..*" << endl
<< " .*. *** **. .**" << endl
<< " *** *.* *.. ..*" << endl << endl
<< "9-*** 0-..*" << endl
<< " **. .**" << endl
<< " *.. ***" << endl << endl
<< "I(i) - Инициализация нового массива" << endl;
cout << "Сделайте свой выбор:";
cin >> choice;
switch (choice) {
case '1':
for (int i = 0; i<q; i++) {
for (int j = 0; j<q; j++) {
if ( j >= i)
cout << A[i][j] << "\t";
else
cout << ".\t";
}
cout << "\n\n";
}
cout << "\n\n";
for (int i = 0; i<q; i++) {
for (int j = 0; j<q; j++) {
if (max < A[i][j] && j >= i)
max = A[i][j];
}
}
cout << "Максимальное число: " << max << endl;
break;
case '2':
for (int i = 0; i<q; i++) {
for (int j = 0; j<q; j++) {
if (j <= i)
cout << A[i][j] << "\t";
else
cout << ".\t";
}
cout << "\n\n";
}
cout << "\n\n";
for (int i = 0; i<q; i++) {
for (int j = 0; j<q; j++) {
if (max < A[i][j] && j <= i)
max = A[i][j];
}
}
cout << "Максимальное число: " << max << endl;
break;
case '3':
for (int i = 0; i<q; i++) {
for (int j = 0; j<q; j++) {
if (j >= i&&j <= q - i - 1)
cout << A[i][j] << "\t";
else
cout << ".\t";
}
cout << "\n\n";
}
cout << "\n\n";
for (int i = 0; i<q; i++) {
for (int j = 0; j<q; j++) {
if (max < A[i][j] && j >= i&&j <= q - i - 1)
max = A[i][j];
}
}
cout << "Максимальное число: " << max << endl;
break;
case '4':
for (int i = 0; i<q; i++) {
for (int j = 0; j<q; j++) {
if (j <= i&&j >= q - i - 1)
cout << A[i][j] << "\t";
else
cout << ".\t";
}
cout << "\n\n";
}
cout << "\n\n";
for (int i = 0; i<q; i++) {
for (int j = 0; j<q; j++) {
if (max < A[i][j] && j <= i&&j >= q - i - 1)
max = A[i][j];
}
}
cout << "Максимальное число: " << max << endl;
break;
case '5':
for (int i = 0; i<q; i++) {
for (int j = 0; j<q; j++) {
if (j <= i&&j >= q - i - 1 || j >= i&&j <= q - i - 1)
cout << A[i][j] << "\t";
else
cout << ".\t";
}
cout << "\n\n";
}
cout << "\n\n";
for (int i = 0; i<q; i++) {
for (int j = 0; j<q; j++) {
if ((max<A[i][j] && j <= i&&j >= q - i - 1) || (j >= i&&j <= q - i - 1 && max<A[i][j]))
max = A[i][j];
}
}
cout << "Максимальное число: " << max << endl;
break;
case '6':
for (int i = 0; i<q; i++) {
for (int j = 0; j<q; j++) {
if (j >= i&&j >= q - i - 1 || j <= i&&j <= q - i - 1)
cout << A[i][j] << "\t";
else
cout << ".\t";
}
cout << "\n\n";
}
cout << "\n\n";
for (int i = 0; i<q; i++) {
for (int j = 0; j<q; j++) {
if ((max<A[i][j] && j >= i&&j >= q - i - 1) || (j <= i&&j <= q - i - 1 && max<A[i][j]))
max = A[i][j];
}
}
cout << "Максимальное число: " << max << endl;
break;
case '7':
for (int i = 0; i<q; i++) {
for (int j = 0; j<q; j++) {
if (j <= i&&j <= q - i - 1)
cout << A[i][j] << "\t";
else
cout << ".\t";
}
cout << "\n\n";
}
cout << "\n\n";
for (int i = 0; i<q; i++) {
for (int j = 0; j<q; j++) {
if (max<A[i][j] && j <= i&&j <= q - i - 1)
max = A[i][j];
}
}
cout << "Максимальное число: " << max << endl;
break;
case '8':
for (int i = 0; i<q; i++) {
for (int j = 0; j<q; j++) {
if (j >= i&&j >= q - i - 1)
cout << A[i][j] << "\t";
else
cout << ".\t";
}
cout << "\n\n";
}
cout << "\n\n";
for (int i = 0; i<q; i++) {
for (int j = 0; j<q; j++) {
if (max<A[i][j] && j >= i&&j >= q - i - 1)
max = A[i][j];
}
}
cout << "Максимальное число: " << max << endl;
break;
case '9':
for (int i = 0; i<q; i++) {
for (int j = 0; j<q; j++) {
if (j <= q - i - 1)
cout << A[i][j] << "\t";
else
cout << ".\t";
}
cout << "\n\n";
}
cout << "\n\n";
for (int i = 0; i<q; i++) {
for (int j = 0; j<q; j++) {
if (max<A[i][j] && j <= q - i - 1)
max = A[i][j];
}
}
cout << "Максимальное число: " << max << endl;
break;
case '0':
for (int i = 0; i<q; i++) {
for (int j = 0; j<q; j++) {
if (j >= q - i - 1)
cout << A[i][j] << "\t";
else
cout << ".\t";
}
cout << "\n\n";
}
cout << "\n\n";
for (int i = 0; i<q; i++) {
for (int j = 0; j<q; j++) {
if (max<A[i][j] && j >= q - i - 1)
max = A[i][j];
}
}
cout << "Максимальное число: " << max << endl;
break;
case 'I':
case 'i':
for (int i = 0; i<q; i++) {
for (int j = 0; j<q; j++) {
A[i][j] = rand() % 100;
cout << A[i][j] << "\t";
}
cout << "\n\n";
}
cout << "\n\n";
break;
}
cout << "Если хотите продолжить, нажмите C: ";
cin >> choice;
} while (choice == 'C'|| choice == 'c');

system("pause");
}

22 февраля 2016 г.

Написать программу, которая выводит на экран елочку.

srand(time(0));
HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);

int length_of_sections, summ_sections;
cout << "Enter summ sections:";
cin >> summ_sections;
do
{
cout << "Enter length of sections(only even digit):";
cin >> length_of_sections;
} while (length_of_sections % 2 == 0);

int front=0;

for (int i = 1; i <= summ_sections; i++)
{
front++;
if (front == 15)
front = 1;

for (int i = 0; i<length_of_sections / 2 + 1; i++)
{
for (int j = 0; j<length_of_sections; j++)
{
if (j <= length_of_sections / 2 + i &&j >= length_of_sections / 2 - i)
{
SetConsoleTextAttribute(h, (WORD)(BLACK << 4 | front));
cout << "*";
}
else
{
SetConsoleTextAttribute(h, (WORD)(BLACK << 4 | front));
cout << " ";
}
}
cout << "\n";
}
}
system("pause");

Тоже самое, но фигура перевернута и пустая внутри.

HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
int length_of_sections, summ_sections;
cout << "Enter summ sections:";
cin >> summ_sections;
do
{
cout << "Enter length of sections(only even digit):";
cin >> length_of_sections;
} while (length_of_sections % 2 == 0);

int front=0;

for (int i = 1; i <= summ_sections; i++)
{
front++;
if (front == 15)
front = 1;

for (int i = 0; i<length_of_sections / 2 + 1; i++)
{
for (int j = 0; j < length_of_sections; j++)
{
if (i == 0 || j == i || j == length_of_sections - i - 1)
{
SetConsoleTextAttribute(h, (WORD)(BLACK << 4 | front));
cout << "*";
}
else
cout << " ";
}
cout <<endl;
}
}
system("pause");

Нарисовать звездочками флаг Великобритании.

HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
int digit;
do
{
cout << "Enter digit(only even digit):";
cin >> digit;
} while (digit % 2 == 0);

cout << endl;

for (int i = 0; i < digit; i++)
{
for (int j = 0; j < digit; j++)
{
if (j == i || j == digit - i-1||i==digit/2||j==digit/2)
{
SetConsoleTextAttribute(h, (WORD)(BLACK << 4 | RED));
cout << char(219);

}
else if (i == digit / 2 - 1 || j == digit / 2 - 1 || i == digit / 2 + 1 || j == digit / 2 + 1||
j == i-1 || j == digit - i - 2|| j == i+1 || j == digit - i)
{
SetConsoleTextAttribute(h, (WORD)(BLACK << 4 | WHITE));
cout << char(219);
}
else
{
SetConsoleTextAttribute(h, (WORD)(BLACK << 4 | BLUE));
cout << char(219);
}

}
cout << endl;
}
cout << endl;
system("pause");

Написать программу, которая выводят на экран флаг Украины (пользователь вводит ширину, высота равна трети ширины), состоящий из символа '*'. То же, но фигура внутри пустая.

HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
int digit;
do
{
cout << "Enter digit(digit>\9):";
cin >> digit;
} while (digit < 10|| (digit / 3 / 2)%2!=0);

for (int i = 0; i <= digit/3; i ++)
{
for (int j = 0; j < digit; j++)
{
if (i <= digit / 3 / 2)
{
SetConsoleTextAttribute(h, (WORD)(BLACK << 4 | L_BLUE));
cout << char(219);
}
else
{
SetConsoleTextAttribute(h, (WORD)(BLACK << 4 | YELLOW));
cout << char(219);
}
}
cout << endl;
}
SetConsoleTextAttribute(h, (WORD)(BLACK << 4 | WHITE));
cout << endl << endl;
for (int i = 0; i <= digit / 3; i++)
{
for (int j = 0; j < digit; j++)
{
if (i == 0||(j==0&&i<=digit/3/2)||(j==digit-1 && i <= digit / 3 / 2))
{
SetConsoleTextAttribute(h, (WORD)(BLACK << 4 | L_BLUE));
cout << char(219);
}
else if (i == digit / 3||(j==0&&i>= digit / 3 / 2) || (j == digit - 1 && i >= digit / 3 / 2))
{
SetConsoleTextAttribute(h, (WORD)(BLACK << 4 | YELLOW));
cout << char(219);
}
else
cout << " ";
}
cout << endl;
}
SetConsoleTextAttribute(h, (WORD)(BLACK << 4 | WHITE));
system("pause");

Поместить в одномерный массив числа от 1 до 30 (31). И показать календарь на текущий месяц.

int month;
do {
cout << "Enter number of month(1-12):";
cin >> month;
} while (month < 1 || month > 12);
cout << "Mo\tTu\tWe\tTh\tFr\tSa\tSu" << endl;
switch (month) {
case 1:{
int A[31]{};
for (int i = 1; i <= 31; i++)
A[i - 1] = i;
for (int i = 0; i < 35; i++) {
if (i < 4) {
cout << "\t";
continue;
}
cout << A[i - 4] << "\t";
if ((i + 1) % 7 == 0)
cout << endl;
}
cout << endl;
}
break;
case 2:{
int A[29]{};
for (int i = 1; i <= 29; i++)
A[i - 1] = i;
for (int i = 0; i <29; i++) {
cout << A[i] << "\t";
if ((i + 1) % 7 == 0)
cout << endl;
}
cout << endl;
}
break;
case 3: {
int A[31]{};
for (int i = 1; i <= 31; i++)
A[i - 1] = i;
for (int i = 0; i < 32; i++) {
if (i == 0) {
cout << "\t";
continue;
}
cout << A[i - 1] << "\t";
if ((i + 1) % 7 == 0)
cout << endl;
}
cout << endl;
}
break;
case 4: {
int A[30]{};
for (int i = 1; i <= 30; i++)
A[i - 1] = i;
for (int i = 0; i < 34; i++) {
if (i < 4) {
cout << "\t";
continue;
}
cout << A[i - 4] << "\t";
if ((i + 1) % 7 == 0)
cout << endl;
}
cout << endl;
}
break;
case 5: {
int A[31]{};
for (int i = 1; i <= 31; i++)
A[i - 1] = i;
for (int i = 0; i < 37; i++) {
if (i < 6) {
cout << "\t";
continue;
}
cout << A[i - 6] << "\t";
if ((i + 1) % 7 == 0)
cout << endl;
}
cout << endl;
}
break;
case 6: {
int A[30]{};
for (int i = 1; i <= 30; i++)
A[i - 1] = i;
for (int i = 0; i < 32; i++) {
if (i < 2) {
cout << "\t";
continue;
}
cout << A[i - 2] << "\t";
if ((i + 1) % 7 == 0)
cout << endl;
}
cout << endl;
}
break;
case 7: {
int A[31]{};
for (int i = 1; i <= 31; i++)
A[i - 1] = i;
for (int i = 0; i < 35; i++) {
if (i < 4) {
cout << "\t";
continue;
}
cout << A[i - 4] << "\t";
if ((i + 1) % 7 == 0)
cout << endl;
}
cout << endl;
}
break;
case 8: {
int A[31]{};
for (int i = 0; i < 31; i++) {
A[i - 1] = i+1;
cout << A[i-1] << "\t";
if ((i + 1) % 7 == 0)
cout << endl;
}
cout << endl;
}
break;
case 9: {
int A[30]{};
for (int i = 1; i <= 30; i++)
A[i - 1] = i;
for (int i = 0; i < 33; i++) {
if (i < 3) {
cout << "\t";
continue;
}
cout << A[i - 3] << "\t";
if ((i + 1) % 7 == 0)
cout << endl;
}
cout << endl;
}
break;
case 10: {
int A[31]{};
for (int i = 1; i <= 31; i++)
A[i - 1] = i;
for (int i = 0; i < 36; i++) {
if (i < 5) {
cout << "\t";
continue;
}
cout << A[i - 5] << "\t";
if ((i + 1) % 7 == 0)
cout << endl;
}
cout << endl;
}
break;
case 11: {
int A[30]{};
for (int i = 1; i <= 30; i++)
A[i - 1] = i;
for (int i = 0; i < 31; i++) {
if (i==0) {
cout << "\t";
continue;
}
cout << A[i - 1] << "\t";
if ((i + 1) % 7 == 0)
cout << endl;
}
cout << endl;
}
break;
case 12: {
int A[31]{};
for (int i = 1; i <= 31; i++)
A[i - 1] = i;
for (int i = 0; i < 34; i++) {
if (i < 3) {
cout << "\t";
continue;
}
cout << A[i - 3] << "\t";
if ((i + 1) % 7 == 0)
cout << endl;
}
cout << endl;
}
break;
}
system("pause");

Реализовать программу, которая позволит пользователю самому переставлять элементы массива. Изначально массив заполняется случайными значениями(диапазон этих значений задает пользователь). Затем пользователь указывает индекс того элемента, который он хочет переместить, на экран выводиться массив без этого элемента. Затем пользователь указывает индекс, того места куда необходимо вставить элемент, который достали. Если пользователь хочет завершить сортировку, достаточно ввести индекс несуществующего элемента.

setlocale(LC_ALL,"rus");
const int size = 10;
int A[size]{};
for (int i = 0; i < size; i++)
A[i] = rand() % 51;
int digit;
do
{
cout << "Massive A:" << endl;
for (int i = 0; i < size; i++) {
A[i] = rand() % 51;
cout.width(3);
cout << A[i];
}
cout << endl;
cout << "Выберите элемент, который хотите переставить:";
cin >> digit;
int tmp_value = A[digit - 1];
int tmp_index = digit - 1;
for (int i = 0; i < size; i++) {
if (i == digit-1)
continue;
cout.width(3);
cout << A[i];
}
cout << endl;
cout << "Выберите место, куда вы хотите его вставить:";
cin >> digit;

A[tmp_index] = A[digit - 1];
A[digit - 1] = tmp_value;
for (int i = 0; i < size; i++) {
cout.width(3);
cout << A[i];
}
cout << endl;
cout << "Если хотите продолжить, нажмите 1:";
cin >> digit;
} while (digit == 1);

system("pause");

25 января 2016 г.

Написать игру «Угадай число!».

setlocale(0,"rus");
int digit;
do
{
cout << "Введите число от 0 до 9:";
cin >> digit;
} while (digit <= 0 || digit >= 9);
cout << "Угадайте число" << endl;
int tmp;
for (int i=1;i<=3;i++)
{
cin >> tmp;
if (tmp == digit)
cout << "Вы угадали!" << endl;
else if (tmp > digit)
cout << "Не угадали! Ваше число больше загаданного!" << endl;
else
cout << "Не угадали! Ваше число меньше загаданного!" << endl;
}
system("pause");

22 сентября 2015 г.

Необходимо создать двумерный массив. Далее написать функцию, которая заполнит его случайными числами от 30 до 60. Создать еще две функции, которые находят максимальный и минимальный элементы этого двумерного массива

void init(int **a, int line, int column);
void out(int **a, int line, int column);
int max(int **a, int line, int column);
int min(int **a, int line, int column);

void main()
{
setlocale(LC_ALL, "rus");

int size_l, size_c;
do
{
cout << "Введите количество строк:";
cin >> size_l;
cout << "Введите количество столбцов:";
cin >> size_c;
//инициализация двумерного массива
int **mas = new int*[size_l];
for (int i = 0; i < size_l; i++)
mas[i] = new int[size_c];

init(mas, size_l, size_c);//вызов функции заполнения массива
out(mas, size_l, size_c);//вызов функции вывода массива на экран

cout << "Максимальное число в массиве: " << max(mas, size_l, size_c) << "\n";
cout << "Минимальное число в массиве: " << min(mas, size_l, size_c) << "\n";

//удаление двумерного массива
for (int i = 0; i < size_l; i++)
delete[]mas[i];
delete[] mas;
cout << "\nЕсли хотите продолжить, нажмите 1:";
cin >> size_l;
cout << "\n";
} while (size_l==1);
}

void init(int **a, int line, int column)
{
for (int i = 0; i < line; i++)
{
for (int j = 0; j < column; j++)
{
a[i][j] = rand() % 30 + 30;
}
}
}

void out(int **a, int line, int column)
{
cout << "\n";
for (int i = 0; i < line; i++)
{
for (int j = 0; j < column; j++)
{
cout<<a[i][j]<<" ";
}
cout << "\n";
}
cout << "\n";
}

int max(int **a, int line, int column)
{
int d = a[0][0];
for (int i = 0; i < line; i++)
{
for (int j = 0; j < column; j++)
{
if (d < a[i][j])
d = a[i][j];
}
}
return d;
}

int min(int **a, int line, int column)
{
int d = a[0][0];
for (int i = 0; i < line; i++)
{
for (int j = 0; j < column; j++)
{
if (d > a[i][j])
d = a[i][j];
}
}
return d;
}

Найти заданную степень числа, пользуясь только указателями.

void st(long int *digit, int *stepen, long int *res)
{
for (int i = *stepen; i > 1; i--)
{
*res *= *digit;
}
}

void main()
{
setlocale(LC_ALL, "Russian");
long int a, res;
int stepen;
do{
cout << "Эта программа считает степень числа с помощью указателей\n";
cout << "Введите число:";
cin >> a;
cout << "Введите степень:";
cin >> stepen;
res = a;
st(&a, &stepen, &res);
cout << "\n" << stepen << " степень числа " << a << " равна " << res << "\n\n";
cout << "Если хотите продолжить, нажмите 1:";
cin >> a;
} while (a == 1);
}

Через указатели на указатели посчитать сумму двух чисел и записать в третье.

void summ(int *x, int *y, int *tmp);

void main()
{
setlocale(LC_ALL, "Russian");
int a, b, res;
do{
cout << "Эта программа считает сумму двух чисел и записивает в третье\n";
cout << "\nВведите первое число:";
cin >> a;
cout << "\nВведите второе число:";
cin >> b;
summ(&a, &b, &res);
cout << "\nСумма равна " << res << ".\n";
cout << "Если хотите продолжить, нажмите 1:";
cin >> a;
} while (a == 1);
}

void summ(int *x, int *y, int *tmp)
{
*tmp= *x + *y;
}

Написать примитивный калькулятор, пользуясь только указателями.

void summ(int *x, int *y, int *tmp);
void razn(int *x, int *y, int *tmp);
void proizv(int *x, int *y, int *tmp);
void delen(int *x, int *y, int *tmp);

void main()
{
setlocale(LC_ALL, "Russian");
int a, b, res;
char dei;
do{
cout << "Этот примитивный калькулятор использует только указатели\n";
cout << "\nВозможно использовать только + - * / \n";
cin >> a >> dei >> b;
cout << "=\n";
switch (dei){
case '+':
summ(&a, &b, &res);
cout << res << "\n";
break;
case '-':
razn(&a, &b, &res);
cout << res << "\n";
break;
case '*':
proizv(&a, &b, &res);
cout << res << "\n";
break;
case '/':
delen(&a, &b, &res);
cout << res << "\n";
break;
default:
cout << "Error!";
}
cout << "Если хотите продолжить, нажмите 1:";
cin >> a;
} while (a == 1);
}

void summ(int *x, int *y, int *tmp)
{
*tmp = *x + *y;
}

void razn(int *x, int *y, int *tmp)
{
*tmp = *x - *y;
}

void proizv(int *x, int *y, int *tmp)
{
*tmp = *x * *y;
}

void delen(int *x, int *y, int *tmp)
{
*tmp = *x / *y;
}

Найти факториал числа, пользуясь только указателями.

void fact(long int *digit, long int *res)
{
for (int i = 1; i <= *digit; i++)
{
*res *= i;
}
}

void main()
{
setlocale(LC_ALL, "Russian");
long int a, res=1;
do{
cout << "Эта программа считает факториал с помощью указателей\n";
cout << "\nВведите число:";
cin >> a;
fact(&a, &res);
cout << "Факториал " << a << " равен " << res << "\n";
cout << "Если хотите продолжить, нажмите 1:";
cin >> a;
} while (a == 1);
}

21 сентября 2015 г.

Написать функцию, которая циклически сдвигает одномерный массив вправо или влево на указанное число позиций. Сдвиг также должен быть кольцевым, то есть те элементы, которые уходят вправо или влево за пределы массива, должны помещаться с другого его конца.



void init(int A[], int size);
void out(int A[], int size);
void sdvig(int A[], int size, int kol, int napr);

void main()
{
setlocale(LC_ALL, "Russian");

int size, kol, napr;

cout << "Введите размер массива: ";
cin >> size;
int* mass = new int [size];
init(mass, size);
out(mass, size);
cout << "Функция, которая циклически сдвигает одномерный массив вправо или влево на указанное число позиций";
cout << "\nВведите количество позиций: ";
cin >> kol;
do
{
cout << "\nВведите направление: 1 - влево, 2 - вправо\n";
cin >> napr;
} while (napr != 1 && napr != 2);
sdvig(mass, size, kol, napr);
out(mass, size);
delete[] mass;
}

void sdvig(int A[], int size, int kol, int napr)
{
if (napr == 1)
{
for (int i = 0; i <kol; i++)
{
int tmp = A[0];
for (int j = 1; j <size; j++)
{
A[j - 1] = A[j];
}
A[size - 1] = tmp;
}
}
if (napr == 2)
{
for (int i = 0; i < kol; i++)
{
int tmp = A[size-1];
for (int j = size-1; j > 0; j--)
{
A[j] = A[j-1];
}
A[0] = tmp;
}
}
}

void init(int *a, int size)
{
srand(time(NULL));
for (int i = 0; i < size; a++, i++)
*a = rand() % 99 + 1;
}

void out(int *a, int size)
{
cout << "\n";
for (int i = 0; i < size; a++, i++)
cout << *a << " ";
cout << "\n";
}

18 июня 2015 г.

По данному натуральному n вычислите сумму 1×2+2×3+...+(n-1)×n.

int sos(int digit);

void main()
{
setlocale(LC_ALL, "Russian");
int a, b;
do
{
cout<<"По данному натуральному n вычислите сумму произведений соседних чисел 1*2+2*3+...+(n-1)*n.\n";
cout<<"\nВведите число:";
cin>>a;
cout<<"Сумма кубов равна "<<sos(a)<<"\n";
cout<<"Если хотите продолжить, нажмите 1:";
cin>>a;
}
while(a==1);
}

int sos(int digit)
{
int res=0;
for(int i=2;i<=digit;i++)
res+=(i-1)*i;
return res;
}