Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- int main()
- {
- const int arrSize{ 10 };
- int arr[arrSize]{ 1,3,4,-2,6,-3,8,-9,7,0 };
- //int arr[arrSize]{ 1,3,4,2,6,3,8,9,7,0 };
- int firstNegIdx{ -1 };
- int lastNegIdx{ -1 };
- int prod{ 1 };
- for (int i{ 0 }; i < arrSize; ++i)
- {
- if ( arr[i] < 0 and (firstNegIdx == -1) ) { firstNegIdx = i; };
- if ( arr[i] < 0 ) { lastNegIdx = i; };
- }
- if (lastNegIdx >= 0)
- {
- std::cout << "Last negative elem index is: " << lastNegIdx <<
- " and elem value is " << arr[lastNegIdx] << '\n';
- }
- else
- {
- std::cout << "There are no negative elements in this array!\n";
- }
- if (firstNegIdx >= 0)
- {
- std::cout << "First negative elem index is: " << firstNegIdx <<
- " and elem value is " << arr[firstNegIdx] << '\n';
- }
- else
- {
- std::cout << "There are no negative elements in this array!\n";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement