Conversazione Messaggio #29809
16 messaggi · –M S M S M
A M A M
A M A #include <iostream> #include <stack> #include <time.h> #include <cctype> using namespace std; int main() { srand(time(0)); stack<int> s; int n; cout << "Enter the number of elements: "; cin >> n; // Stacking the elements for(int i=0; i<n; i++){ int element; cout << "Enter element " << i+1 << ": "; element=rand()%100; s.push(element); } // Finding the first negative element while(!s.empty()){ int element = s.top(); s.pop(); if(element < 0){ cout << "The first negative element is: " << element << endl; return 0; } } cout << "There is no negative element in the stack" << endl; return 0; }M
M A #include <iostream> #include <valarray> using namespace std; int main() { valarray<int> arr = {1, 2, 3, 4, 5}; // Dekrementation using prefix (--i) cout << "Prefix decrementation: "; for(int i=0; i<arr.size(); i++){ cout << --arr[i] << " "; } cout << endl; // Resetting array to original values arr = {1, 2, 3, 4, 5}; // Dekrementation using postfix (i--) cout << "Postfix decrementation: "; for(int i=0; i<arr.size(); i++){ cout << arr[i]-- << " "; } cout << endl; return 0; }