Написать программу ,которая проверяет ,сколько раз введенное с клавиатуры число встречается в заданном массиве

19 Янв 2020 в 19:41
170 +1
0
Ответы
1

Sure! Here is an example of a program in Python that checks how many times a number entered from the keyboard appears in a given array:

def count_occurrences(arr, num):
count = 0
for n in arr:
if n == num:
count += 1
return count
arr = [2, 4, 6, 2, 8, 2, 10, 2]
num = int(input("Enter a number to search for in the array: "))
occurrences = count_occurrences(arr, num)
print(f"The number {num} appears {occurrences} times in the array.")

In this program, the count_occurrences function takes an array and a number as input and iterates through the array to count how many times the number appears. The user is prompted to enter a number, which is then passed to the function along with the array [2, 4, 6, 2, 8, 2, 10, 2]. The program then prints the number of occurrences of the entered number in the array.

18 Апр 2024 в 19:40
Не можешь разобраться в этой теме?
Обратись за помощью к экспертам
Гарантированные бесплатные доработки в течение 1 года
Быстрое выполнение от 2 часов
Проверка работы на плагиат
Поможем написать учебную работу
Прямой эфир