В С++ В одномерном массиве, состоящем из п вещественных элементов, вычислить: 1) количество отрицательных элементов массива; 2) сумму модулей элементов массива, расположенных после минимального по модулю элемента. Заменить все отрицательные элементы массива их квадратами и упорядочить элементы массива по возрастанию
int main() { int n; cout << "Enter the number of elements in the array: "; cin >> n;
double* arr = new double[n]; cout << "Enter the elements of the array: "; for(int i = 0; i < n; i++) { cin >> arr[i]; } // 1) Counting the number of negative elements int count_negative = 0; for(int i = 0; i < n; i++) { if(arr[i] < 0) { count_negative++; } } cout << "Number of negative elements in the array: " << count_negative << endl; // 2) Finding the minimum element by absolute value double min_abs = abs(arr[0]); int min_index = 0; for(int i = 1; i < n; i++) { if(abs(arr[i]) < min_abs) { min_abs = abs(arr[i]); min_index = i; } } // Calculating the sum of absolute values of elements after the minimum element double sum_abs = 0; for(int i = min_index + 1; i < n; i++) { sum_abs += abs(arr[i]); } cout << "Sum of absolute values of elements after the minimum element: " << sum_abs << endl; // Replacing negative elements with their squares for(int i = 0; i < n; i++) { if(arr[i] < 0) { arr[i] = pow(arr[i], 2); } } // Sorting the array in ascending order sort(arr, arr + n); // Printing the modified array cout << "Modified array in ascending order: "; for(int i = 0; i < n; i++) { cout << arr[i] << " "; } delete [] arr; return 0;
using namespace std;
int main() {
double* arr = new double[n];int n;
cout << "Enter the number of elements in the array: ";
cin >> n;
cout << "Enter the elements of the array: ";
for(int i = 0; i < n; i++) {
cin >> arr[i];
}
// 1) Counting the number of negative elements
int count_negative = 0;
for(int i = 0; i < n; i++) {
if(arr[i] < 0) {
count_negative++;
}
}
cout << "Number of negative elements in the array: " << count_negative << endl;
// 2) Finding the minimum element by absolute value
double min_abs = abs(arr[0]);
int min_index = 0;
for(int i = 1; i < n; i++) {
if(abs(arr[i]) < min_abs) {
min_abs = abs(arr[i]);
min_index = i;
}
}
// Calculating the sum of absolute values of elements after the minimum element
double sum_abs = 0;
for(int i = min_index + 1; i < n; i++) {
sum_abs += abs(arr[i]);
}
cout << "Sum of absolute values of elements after the minimum element: " << sum_abs << endl;
// Replacing negative elements with their squares
for(int i = 0; i < n; i++) {
if(arr[i] < 0) {
arr[i] = pow(arr[i], 2);
}
}
// Sorting the array in ascending order
sort(arr, arr + n);
// Printing the modified array
cout << "Modified array in ascending order: ";
for(int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
delete [] arr;
return 0;
}