Масиви – Примери част.1




Примери с обхождане на масив:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
using namespace std;
int main() {
  const int arr_size = 10;
  int arr[arr_size] = {-1,5,6,-23,123,-7,0,8,9,-7};
  //print array
  cout << "Print array:"<<endl;
  for(int i = 0; i < arr_size; i++){
    cout << arr[i] << '\t';
  }
  cout << endl << "Print array in reversed order:"<<endl;
  //print array reversed
  for(int i = arr_size-1; i >= 0; i--){
    cout << arr[i] << '\t';
  }
  //print array with indexes
  cout << endl << "Print array with indexes:"<<endl;
  for(int i = 0; i < arr_size; i++){
    cout << "arr[" << i << "]=" << arr[i] << '\t';
  }
  //Find if there are negatives in array
  bool check = 0;
  for(int i = 0; i < arr_size; i++){
    if(arr[i] < 0){
      check = 1;
      break;
    }
  }
  cout << endl << "Are there negatives in the array: "<< (check?"Yes":"No") << endl;
}