24 апреля 2016 г.

Дан двумерный массив размерностью 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")

Комментариев нет:

Отправить комментарий