Advertisement
enkov

Функция и параметър едномерен масив

Feb 8th, 2019
290
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.13 KB | None | 0 0
  1. // Functions and arrays
  2.  
  3. #include "stdafx.h"
  4.  
  5. #include <iostream>
  6. #include <ctime>
  7. using namespace std;
  8.  
  9. double average(int arr[], const int size)
  10. {
  11.     double sum = 0.0;
  12.     for (int i = 0; i<size; ++i)
  13.         sum += arr[i];
  14.     return sum / size;
  15. }
  16.  
  17. void Plus1(int arr[], const int size)
  18. {
  19.     for (int i = 0; i < size; ++i)
  20.         arr[i]++;
  21.     return;
  22. }
  23.  
  24. void PrintArray(int arr[], const int size, const char* S)
  25. {
  26.     cout << S;
  27.     for (int i = 0; i < size; ++i) cout << arr[i] << " ";
  28.     cout << endl;
  29. }
  30.  
  31. int main()
  32. {
  33.     srand((unsigned)time(NULL));
  34.     const int n = 10;
  35.     // Пример със статичен масив
  36.     int A[n] = { 4,6,78,3,1,3,5,7,4,2 };
  37.     cout << "Average of A = " << average(A, n) << endl;
  38.     PrintArray(A, n, "Array A now: ");
  39.     Plus1(A, n);
  40.     PrintArray(A, n, "Array A after Plus1: ");
  41.     cout << "Average of A +1 = " << average(A, n) << endl;
  42.     // Пример с динамичен масив със случайни числа
  43.     int* p = new int[n];
  44.     for (int i = 0; i<n; ++i)
  45.         p[i] = rand() % 100;
  46.     PrintArray(p, n, "Array p now: ");
  47.     cout << "Average of p = " << average(p, n) << endl;
  48.     delete[] p;
  49.     return 0;
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement