python class 12 computer science
note - without using inbuilt function
WAP to create a nested list of numbers and display menu
a) Maximum in each row
b) Sum in each row
c) Minimum in each column
d) Average in each column
Answers
The given problem is solved using language - Python.
matrix=[]
rows=int(input('Enter the number of rows for the matrix: '))
columns=int(input('Enter the number of columns for the matrix: '))
print('\nEnter the matrix elements...')
for i in range(rows):
x=[]
for j in range(columns):
x.append(int(input(f'a[{i}][{j}] = ')))
matrix.append(x)
print('\nGiven Matrix:')
for i in matrix:
for j in i:
print(j,end=' ')
print()
print('\n1. Maximum value in each row.')
print('2. Sum of each row.')
print('3. Minimum in each column.')
print('4. Average in each column.')
choice=int(input('\nEnter your choice: '))
if choice==1:
print()
for i in range(rows):
max=matrix[i][0]
for j in range(columns):
if matrix[i][j]>max:
max=matrix[i][j]
print('Largest Element in Row',i+1,'is:',max)
elif choice==2:
print()
k=1
for i in matrix:
sum=0
for j in i:
sum+=j
print('Sum of elements in row',k,'is:',sum)
k+=1
elif choice==3:
print()
for i in range(columns):
cols=[m[i] for m in matrix]
min=cols[0]
for j in range(1,rows):
if cols[j]<min:
min=cols[j]
print('Minimum element in column',i+1,'is:',min)
elif choice==4:
print()
for i in range(columns):
cols=[m[i] for m in matrix]
sum=0
for j in cols:
sum+=j
sum/=rows
print('Average of elements in column',i+1,'is:',sum)
else:
print('Invalid Choice.')
Note: No inbuilt functions are used. The given code calculates all the operations according to the user's choice. For an invalid choice, an error message is displayed.