Python Array Solution Q3

 Write a program that takes an integer input from the user and creates an array with that many random integers. Print the array and calculate the average of the numbers in the array.


import array

import random


# Take an integer input from the user

array_size = int(input("Enter the size of the array: "))


# Create an array with random integers

random_integers = array.array('i', [random.randint(1, 100) for _ in range(array_size)])

# Print the array

print("Generated Array:", random_integers)


# Calculate and print the average of the numbers in the array

average = sum(random_integers) / len(random_integers)

print(f"The average of the numbers in the array is: {average}")


In this program:


We import the array module and the random module.

The user is prompted to enter the size of the array.

An array of random integers is created using a list comprehension and the random.randint() function.

The array is printed.

The average of the numbers in the array is calculated using the sum() function and division by the length of the array.

The average is printed.

This program uses the random module to generate random integers within a specified range.

Comments

Popular posts from this blog

Class X Test / Model Paper Chapter No 1 Computer

Inter Board Karachi Guess/Model Papers 2023 (1st year & 2nd year)

Dulla Bhatti Real Hero Of Muslims Rajput