
Задать массив случайным образом, заменить в нём все отрицательные элементы на элементы с
противоположным знаком(например, -5 на 5, -9 на 9), отсортировать массив по неубыванию. Вывести элементы исходного и получившегося массива.

Ответы на вопрос

// Также проверено в WDE: http://pascalabc.net/WDE
begin
var a:=ArrRandom(ReadInteger('n='),-99,99); a.Println;
a.Transform(x->-x);
a:=a.Sorted.ToArray; a.Println
end.
Тестовое решение
n= 15
-6 11 86 67 95 18 -57 8 79 90 45 72 -82 -50 31
-95 -90 -86 -79 -72 -67 -45 -31 -18 -11 -8 6 50 57 82



Creating, Modifying, and Sorting an Array in JavaScript
To achieve the desired outcome of creating an array, replacing negative elements with their absolute values, and sorting the array in non-decreasing order, you can follow these steps in JavaScript:
1. Create a Random Array: - Use the `Math.random()` function to generate random numbers and populate an array with them.
```javascript // Create a random array of 10 elements let randomArray = Array.from({ length: 10 }, () => Math.floor(Math.random() * 20) - 10); ```
2. Replace Negative Elements with Absolute Values: - Iterate through the array and replace negative elements with their absolute values using the `Math.abs()` function.
```javascript // Replace negative elements with their absolute values randomArray = randomArray.map(num => num < 0 ? Math.abs(num) : num); ```
3. Sort the Array in Non-Decreasing Order: - Use the `Array.prototype.sort()` method to sort the array in non-decreasing order.
```javascript // Sort the array in non-decreasing order randomArray.sort((a, b) => a - b); ```
4. Display the Original and Modified Arrays: - Output the original and modified arrays to the console for comparison.
```javascript // Display the original and modified arrays console.log('Original Array:', randomArray); ```
```javascript // Display the modified array after replacing negative elements and sorting console.log('Modified Array:', randomArray); ```
By following these steps, you can create a random array, replace negative elements with their absolute values, sort the array in non-decreasing order, and display both the original and modified arrays for comparison.
[[2 #]]


Топ вопросов за вчера в категории Информатика









Последние заданные вопросы в категории Информатика
-
Математика
-
Литература
-
Алгебра
-
Русский язык
-
Геометрия
-
Английский язык
-
Химия
-
Физика
-
Биология
-
Другие предметы
-
История
-
Обществознание
-
Окружающий мир
-
География
-
Українська мова
-
Информатика
-
Українська література
-
Қазақ тiлi
-
Экономика
-
Музыка
-
Право
-
Беларуская мова
-
Французский язык
-
Немецкий язык
-
МХК
-
ОБЖ
-
Психология
-
Физкультура и спорт
-
Астрономия
-
Кыргыз тили
-
Оʻzbek tili