Advertisement
Guest User

Untitled

a guest
Jan 19th, 2020
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.74 KB | None | 0 0
  1. #include <cstdio>
  2. #include <string.h>
  3.  
  4. /*
  5. Napisz funkcję, która otrzymuje w parametrach dynamicznie alokowaną dwuwymiarową tablicę
  6. liczb całkowitych oraz liczbę jej wierszy. Wiersze tej tablicy są różnej długości, jednak wiadomo, że każdy
  7. z nich kończy się liczbą 0. Funkcja powinna z każdego wiersza usunąć liczby będące wielokrotnościami
  8. liczby znajdującej się w pierwszym polu tego wiersza (łącznie z nią samą) pozostawiając 0 na końcu. Jeżeli
  9. wiersz zaczyna się liczbą 0, pozostawiany jest bez zmian. Kolejność liczb w wierszu po tej operacji jest
  10. dowolna.
  11. */
  12.  
  13. void print_array_2D(int** arr, int rows, int cols) {
  14. for (int y = 0; y < rows; ++y) {
  15. for (int x = 0; x < cols; ++x) {
  16. printf("%d, ", arr[y][x]);
  17. }
  18. printf("\n");
  19. }
  20. }
  21.  
  22. int length(int* arr) {
  23. int counter = 1;
  24. for (int x = 0; arr[x] != 0; ++x) {
  25. counter++;
  26. }
  27. return counter;
  28. }
  29.  
  30. void f(int** arr, int rows) {
  31. for (int y = 0; y < rows; ++y) {
  32. int* row = arr[y];
  33. int size = length(row);
  34. if (row[0] == 0) {
  35. continue;
  36. }
  37. for (int x = 1; x < size; ++x) {
  38. if (row[x] % row[0] == 0) {
  39. memmove(row + x, row + x + 1, sizeof(int) * (size - x - 1));
  40. }
  41. }
  42. memmove(row, row + 1, sizeof(int) * (size - 1));
  43. }
  44.  
  45. print_array_2D(arr, rows, 5);
  46. }
  47.  
  48. int main() {
  49. int** arr = new int*[2];
  50. arr[0] = new int[5];
  51. arr[1] = new int[5];
  52. arr[0][0] = 2;
  53. arr[0][1] = 2;
  54. arr[0][2] = 3;
  55. arr[0][3] = 4;
  56. arr[0][4] = 0;
  57.  
  58. arr[1][0] = 3;
  59. arr[1][1] = 4;
  60. arr[1][2] = 2;
  61. arr[1][3] = 6;
  62. arr[1][4] = 0;
  63.  
  64. f(arr, 2);
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement