Advertisement
Sanlover

Untitled

Oct 25th, 2021
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.97 KB | None | 0 0
  1. #include <ctime>
  2. #include <iostream>
  3. using namespace std;
  4.  
  5. int main() {
  6. srand(time(0));
  7. const int n = 10;
  8. int array[n], negativePos = -1;
  9. long long int y = 0, temp = 1; // long long нужен, чтобы хранить большое значение
  10.  
  11. for (int i = 0; i < n; i++) {
  12. array[i] = rand() % 100 - 20; // Левая граница: (0-20) , Правая граница: (100 -20)
  13. }
  14.  
  15. cout << "Your array is:" << endl;
  16. for (int i = 0; i < n; i++) {
  17. cout << array[i] << " ";
  18. }
  19.  
  20. for (int i = 0; i < n && negativePos == -1; i++) {
  21. if (array[i] < 0) {
  22. negativePos = i + 1;
  23. cout << endl
  24. << "Negative number is found on " << i << "th position" << endl;
  25. }
  26. }
  27. if (negativePos == -1) {
  28. cout << endl << "Negative number is not found" << endl;
  29. negativePos = n;
  30. }
  31.  
  32. for (int i = 0; i < negativePos; i++) {
  33. temp *= array[i];
  34. y += temp;
  35. }
  36.  
  37. cout << "Y = " << y;
  38. return 0;
  39. }
  40.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement