Question

(+30) Provide a python program which will Populate an array(list) of size 25 with integers in...

(+30) Provide a python program which will

  1. Populate an array(list) of size 25 with integers in the range -100 (negative 100)   to +100 inclusive
  2. Display the array and its length (use the len function)
  3. Display the average of all the integers in the array
  4. Display the number of even integers (how many)
  5. Display the number of odd integers (how many)
  6. Display the number of integers > 0   (how many)
  7. Display the number of integers < 0   (how many)
  8. Display the median.

NOTE In sorted order, the median equals the 13th == array[12] element in the array. Please do not use array[12] to display the median instead consider n/2 or n//2 or a ceil or floor functions etc. That is the median should be calculated using n

  1. Display the integers >= median   (See the : operator as in sub = array[2:6] know splice )
  2. Display the integers < median see the splice operator
  3. Ask the user for an integer and print the number of times the integer is in the array

HINT: see the count function below

  1. Display the maximum integer
  2. Display the minimum integer
  3. Display the array in reverse sorted order (largest to smallest) See the sort() and reverse() functions
  4. Display the gcd of the largest and smallest integers (See the lesson for an example ) using python’s gcd function

SAMPLE code Template ( Lab 05Template.py)

import random

import math         # needed for gcd

array = []          # empty list

a = -100     

b = 100

x = random.randint(a, b )  # Return a random integer a <= x <= b.

print("random integer x == ", x)

j = 0

n = 25                          # size of the array

while (j < n):                  # populate the array using the while loop

    x = random.randint(a, b )   # Return a random integer  a <= x <= b.

    array.append(x)             #APPEND adds to a list

    j = j + 1

print("array == ",array)        #  (2) now print the array

sub = []                     # subsequence

sub = array[2:6]                #  create a subsequence start at 2 end at 5

print ('sub ', sub)             #  (3)

# now sum

sum = 0                         #  (4)  sum

for k in array:

    sum = sum + k

print("sum == ", sum)

print(array)                     # print array and its length

print("array length is ", len(array))

array.sort()                      # sort array

print("now sorted...")

print(array)

array.reverse()                   # reverse the array

print("now sorted in reverse order...")

print(array)

print(" the first integer is ", array[0], " and the last integer...", array[len(array) -1])

odd = 0

# need a loop here...

print('the number of odd integers is ', odd  )

s = 5                          # search for an integer s using count() function

print("Is ", s, " in the array? ", array.count(s))
0 0
Add a comment Improve this question Transcribed image text
Answer #1

If you have any doubts, please give me comment...

import random

import math         # needed for gcd

array = []          # empty list

a = -100     

b = 100

x = random.randint(a, b )  # Return a random integer a <= x <= b.

# print("random integer x == ", x)

j = 0

n = 25                          # size of the array

while (j < n):                  # populate the array using the while loop

    x = random.randint(a, b)   # Return a random integer  a <= x <= b.

    array.append(x)             #APPEND adds to a list

    j = j + 1

print("array == ",array)        #  (2) now print the array

print("array length is ", len(array))

# now sum

sum = 0                         #  (4)  sum

for k in array:

    sum = sum + k

average = sum/len(array)

print("average:", average)

even_c = 0

odd_c = 0

for k in array:

    if k%2==0:

        even_c += 1

    else:

        odd_c += 1

pos_c = 0

neg_c = 0

for k in array:

    if k<0:

        pos_c += 1

    elif k>0:

        neg_c += 1

array.sort()

median_ind = len(array)//2

median = array[median_ind]

print("Median is:", median)

print(" >= median")

for k in array[0:median_ind+1]:

    print(k)

print(" < median")

for k in array[median_ind+1:]:

    print(k)

print("Max: "+str(array[-1]))

print("Min: "+str(array[0]))

array.reverse()

print("Reverse: "+str(array))

print("GCD: "+str(math.gcd(array[-1], array[0])))

Add a comment
Know the answer?
Add Answer to:
(+30) Provide a python program which will Populate an array(list) of size 25 with integers in...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Please write a Java program that does the following: Create an array of 100 integers. Store...

    Please write a Java program that does the following: Create an array of 100 integers. Store 100 random integers (between 1 and 100) in the array. Print out the elements of the array. Sort the array in ascending order. Print out the sorted array. Prompt the user to enter a number between 1 and 100. Search the array for that number and then display "Found" or "Not Found" message. Display each number from 1 to 100 and the number of...

  • Write a c++ program that does the following Create an integer array of size 30. Assign...

    Write a c++ program that does the following Create an integer array of size 30. Assign -1 to each location in the array indicating that the array is empty. Populate half of the array with random integer values between 100 and 500 inclusive. Use the following formula in order to hash and store each number in its proper position/location. Generated Number Table Size: Should a collision occurs, use linear probing to find next available position location. Use the following probing...

  • Given an array A[1..n] of positive integers and given a number x, find if any two...

    Given an array A[1..n] of positive integers and given a number x, find if any two numbers in this array sum upto x. That is, are there i,j such that A[i]+A[j] = x? Give an O(nlogn) algorithm for this. Suppose now that A was already sorted, can you obtain O(n) algorithm?

  • Write a C++ program that does the following : 1. Accepts array size from the keyboard....

    Write a C++ program that does the following : 1. Accepts array size from the keyboard. The size must be positive integer that is >= 5 and <= 15 inclusive . 2. Use the size from step 1 in order to create an integer array. Populate the created array with random integer values between 10 and 200. 3. Display the generated array. 4. Write a recursive function that displays the array in reverse order . 5. Write a recursive function...

  • Use the following code, which initializes an array x with 1000 random integers, to answer the...

    Use the following code, which initializes an array x with 1000 random integers, to answer the questions a and b. java.util.Random generator = new java.util.Random( );//accessing Random class final int MAX = 1000; int[] x = new int[MAX]; for( int j = 0; j < MAX; j++ ) { x[j] = generator.nextInt( MAX ) + 1; } a. Write partial code to print all array elements of x in reverse order (one array element per line) b. Write partial code...

  • 2. Write a C++ program, that generates a set of random integers and places them in...

    2. Write a C++ program, that generates a set of random integers and places them in an array. The number of values generated and their range are entered by the user. The program then continually prompts the user for one of the following character commands: a: display all the values I: display the values less than a given value g display the values greater than a given value e: display all odd values o: display all even values q: quit...

  • python la ab X, X Lab 7 Design and implement a Python program that uses a...

    python la ab X, X Lab 7 Design and implement a Python program that uses a while loop and selection statements to print the sum of the even numbers and the sum of the odd numbers (all integers) read from the keyboard. The sequence of numbers ends with a negative number: So, all numbers considered for printing the two sums are greater than or equal to 0. The sequence may be empty. Here is a possible interaction. Enter an integer:...

  • Suppose you have a sorted array of positive and negative integers and would like to determine...

    Suppose you have a sorted array of positive and negative integers and would like to determine if there exist some value of x such that both x and -x are in the array. Consider the following three algorithms: Algorithm #1: For each element in the array, do a sequential search to see if its negative is also in the array. Algorithm #2:For each element in the array, do a binary search to see if its negative is also in the...

  • Python 3.7 to be used. Just a simple design a program that depends on its own. You should also not need to import anythi...

    Python 3.7 to be used. Just a simple design a program that depends on its own. You should also not need to import anything. No code outside of a function! Median List Traversal and Exception Handling Create a menu-driven program that will accept a collection of non-negative integers from the keyboard, calculate the mean and median values and display those values on the screen. Your menu should have 6 options 50% below 50% above Add a number to the list/array...

  • asap please write a program that will: Have an array that can hold up to se0...

    asap please write a program that will: Have an array that can hold up to se0 Integers Put 30 random integers from e to 100 into spots 0-29 of the array Print the n Print the nu Report the MEAN Report the MEDIAN. [you may "hard-code" for 30 values situation] [Extra Credit] Report the MODE (S) Report the Minimum. Report the Maximum. Report the Range. [Extra Extra Credit Report the standard Deviation s in original random order. s in sorted...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT