Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstdio>
- #include <string.h>
- /*
- Napisz funkcję, która otrzymuje w parametrach dynamicznie alokowaną dwuwymiarową tablicę
- liczb całkowitych oraz liczbę jej wierszy. Wiersze tej tablicy są różnej długości, jednak wiadomo, że każdy
- z nich kończy się liczbą 0. Funkcja powinna z każdego wiersza usunąć liczby będące wielokrotnościami
- liczby znajdującej się w pierwszym polu tego wiersza (łącznie z nią samą) pozostawiając 0 na końcu. Jeżeli
- wiersz zaczyna się liczbą 0, pozostawiany jest bez zmian. Kolejność liczb w wierszu po tej operacji jest
- dowolna.
- */
- void print_array_2D(int** arr, int rows, int cols) {
- for (int y = 0; y < rows; ++y) {
- for (int x = 0; x < cols; ++x) {
- printf("%d, ", arr[y][x]);
- }
- printf("\n");
- }
- }
- int length(int* arr) {
- int counter = 1;
- for (int x = 0; arr[x] != 0; ++x) {
- counter++;
- }
- return counter;
- }
- void f(int** arr, int rows) {
- for (int y = 0; y < rows; ++y) {
- int* row = arr[y];
- int size = length(row);
- if (row[0] == 0) {
- continue;
- }
- for (int x = 1; x < size; ++x) {
- if (row[x] % row[0] == 0) {
- memmove(row + x, row + x + 1, sizeof(int) * (size - x - 1));
- }
- }
- memmove(row, row + 1, sizeof(int) * (size - 1));
- }
- print_array_2D(arr, rows, 5);
- }
- int main() {
- int** arr = new int*[2];
- arr[0] = new int[5];
- arr[1] = new int[5];
- arr[0][0] = 2;
- arr[0][1] = 2;
- arr[0][2] = 3;
- arr[0][3] = 4;
- arr[0][4] = 0;
- arr[1][0] = 3;
- arr[1][1] = 4;
- arr[1][2] = 2;
- arr[1][3] = 6;
- arr[1][4] = 0;
- f(arr, 2);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement