Заранее спасибо.Составьте программу которая формирует массив из пятныдцати случайных целых чисел от -20 до 20 а)Четные числа делит на два , а четные усножает на три. б)отрицательные числа возводит в квадрат , а из не отрицательных извекает корень. Еще раз спасибо)
import random import math random_numbers = [random.randint(-20, 20) for _ in range(50)] print("Original random numbers array:", random_numbers) modified_numbers = [] for num in random_numbers: if num % 2 == 0: if num > 0: modified_numbers.append(num // 2) else: modified_numbers.append(num * 3) else: if num >= 0: modified_numbers.append(math.sqrt(num)) else: modified_numbers.append(num ** 2) print("Modified random numbers array:", modified_numbers)
This program generates an array of 50 random numbers between -20 and 20, then applies the rules specified in parts a) and b) to modify the numbers accordingly. Finally, it prints both the original and modified arrays. Let me know if you need any more help!
Sure! Here is the program written in Python:
import randomimport math
random_numbers = [random.randint(-20, 20) for _ in range(50)]
print("Original random numbers array:", random_numbers)
modified_numbers = []
for num in random_numbers:
if num % 2 == 0:
if num > 0:
modified_numbers.append(num // 2)
else:
modified_numbers.append(num * 3)
else:
if num >= 0:
modified_numbers.append(math.sqrt(num))
else:
modified_numbers.append(num ** 2)
print("Modified random numbers array:", modified_numbers)
This program generates an array of 50 random numbers between -20 and 20, then applies the rules specified in parts a) and b) to modify the numbers accordingly. Finally, it prints both the original and modified arrays. Let me know if you need any more help!