Answer:
(1) The program in Python is as follows:
rows, cols = (3, 3)
arr =[[0,0,0],[2,2,2],[-2,-2,-2]]
print(arr)
arr[0][0] = 10
arr[2][2] = -10
print(arr)
for i in range(rows):
for j in range(cols):
arr[i][j]-=2
print(arr)
for i in range(rows):
for j in range(cols):
if arr[i][j]< 0:
print(arr[i][j], end = ", ")
(2) The program in Python is as follows:
import numpy as np
x = [10000, 1000, 100, 10, 1, 0, -10, -100]
p = [0.05, 0.05, 0.20, 0.20, 0.1, 0.1, 0.1, 0.2]
q = np.dot(x,p)
print(q)
Step-by-step explanation:
(1)
This initializes the rows and columns of the array to 3
rows, cols = (3, 3)
1. This creates and array and also populates it with the given data
arr =[[0,0,0],[2,2,2],[-2,-2,-2]]
Print the array
print(arr)
2. This changes index 0,0 to 10 and index 2,2 to -10
arr[0][0] = 10
arr[2][2] = -10
Print the array
print(arr)
This iterates through the rows and the columns of the array
for i in range(rows):
for j in range(cols):
3. This subtracts 2 from each array element
arr[i][j]-=2
Print the array
print(arr)
This iterates through the rows and the columns of the array
for i in range(rows):
for j in range(cols):
If array element is negative
if arr[i][j]< 0:
4. Print the array element
print(arr[i][j], end = ", ")
(2)
Line 1 and 2 are given as part of the program
x = [10000, 1000, 100, 10, 1, 0, -10, -100]
p = [0.05, 0.05, 0.20, 0.20, 0.1, 0.1, 0.1, 0.2]
This uses np dot to multiply x and p
q = np.dot(x,p)
This prints the result of the product
print(q)