jelyslime

Untitled

Jan 22nd, 2019
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.61 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <string>
  4. #include <cstdlib>
  5. #include <iomanip>
  6. using namespace std;
  7. #define N 5
  8.  
  9. void print24(string);
  10. void fillArray(int**);
  11. void printArray(int **);
  12. void halfArray(int **);
  13. int non_zero(int *, int);
  14.  
  15. int main()
  16. {
  17.  
  18. char str[20];
  19. int ** arr = new int*[N];
  20. for (unsigned i = 0; i<N; i++) {
  21. arr[i] = new int[N];
  22. }
  23.  
  24. fillArray(arr);
  25.  
  26. printArray(arr);
  27.  
  28. halfArray(arr);
  29.  
  30. printArray(arr);
  31.  
  32. int sz;
  33. srand(time(NULL));
  34. cout << "Vyvedete razmer na masiva: ";
  35. cin >> sz;
  36. int* arr2 = new int[sz];
  37. for (int i = 0; i < sz; i++) {
  38. arr2[i] = rand() % 20 - 10;
  39. cout << arr2[i] << " ";
  40. }
  41.  
  42. cout << endl << non_zero(arr2, sz) << endl;
  43.  
  44. for (unsigned i = 0; i<N; i++) {
  45. delete arr[i];
  46. }
  47. delete[] arr;
  48.  
  49.  
  50.  
  51. return 0;
  52. }
  53. //zadacha 1
  54. void fillArray(int** arr) {
  55. srand(time(NULL));
  56. for (unsigned i = 0; i<N; i++) {
  57. for (unsigned j = 0; j<N; j++) {
  58. arr[i][j] = rand() % 10;
  59. }
  60. }
  61. }
  62.  
  63. void printArray(int ** arr) {
  64. for (int i = 0; i<N; i++) {
  65. cout << " ";
  66. for (int j = 0; j < N; j++) {
  67. cout << setw(3) << arr[i][j];
  68. if (j<N - 1) cout << " ";
  69. }
  70. cout << " ";
  71. cout << endl;
  72. }
  73. cout << endl;
  74. }
  75. void halfArray(int ** arr) {
  76. for (int i = 0; i < N; i++) {
  77. int counter = 1;
  78. int cnt = N;
  79. for (int j = N-1; j >= 0; j--) {
  80.  
  81. if (counter < i+1 ) {
  82. arr[i][j] = 0;
  83. }
  84. else {
  85. arr[i][j] = cnt--;
  86. }
  87. counter++;
  88.  
  89. }
  90. }
  91.  
  92. }
  93.  
  94. //Zadacha 3
  95. int non_zero(int * arr, int sz) {
  96. if (sz > 0) return ((arr[sz - 1])>0) ? 1 + non_zero(arr, sz - 1) : non_zero(arr, sz - 1);
  97. return 0;
  98. }
Add Comment
Please, Sign In to add comment