Ansaid

Сумма сегмента массива

Dec 14th, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.84 KB | None | 0 0
  1. /*Сумма сегмента массива*/
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6. void sum(int* array) {
  7.     int s = 0;
  8.     int start, finish;
  9.     cout << "Введите индекс начала сегмента: ";
  10.     cin >> start;
  11.     cout << "Введите индекс конец сегмента: ";
  12.     cin >> finish;
  13.  
  14.     for (int i = start - 1; i < finish; i++) {
  15.         s += array[i];
  16.     }
  17.  
  18.     cout << "Сумма сегмента массива от " << start << " до " << finish << " равна " << s << endl;
  19.  
  20. }
  21.  
  22. int main() {
  23.     int size;
  24.     setlocale(LC_ALL, "Russian");
  25.     cout << "Введите размер массива: ";
  26.     cin >> size;
  27.     int* array = new int[size];
  28.     for (int i = 0; i < size; i++) {
  29.         cout << "Введите " << i + 1 << " элемент массива: ";
  30.         cin >> array[i];
  31.     }
  32.    
  33.     sum(array);
  34.  
  35.     delete array;
  36. }
Add Comment
Please, Sign In to add comment