You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi, i'm newbee in coding and my teacher gave me the task to create two matrices, type = m*n, the code has to summarize, subtract and transpose. I have a bit of code, but idk what to do next.
The code:
from tkinter import *
from tkinter import messagebox
def create_matrices():
try:
n = int(entry_rows.get())
m = int(entry_cols.get())
# Create matrices filled with zeros
global matrix1, matrix2, matrix3
matrix1 = [[0 for _ in range(m)] for _ in range(n)]
matrix2 = [[0 for _ in range(m)] for _ in range(n)]
matrix3 = [[0 for _ in range(m)] for _ in range(n)]
messagebox.showinfo("Success", f"Matrices {n}x{m} created!")
except ValueError:
messagebox.showerror("Error", "Please enter whole numbers for rows and columns!")
def plus():
for i in range(len(matrix1)):
for j in range(len(matrix1[0])):
matrix3[i][j] = matrix1[i][j] + matrix2[i][j]
show_result("Sum of matrices:", matrix3)
def minus():
for i in range(len(matrix1)):
for j in range(len(matrix1[0])):
matrix3[i][j] = matrix1[i][j] - matrix2[i][j]
show_result("Difference of matrices:", matrix3)
def trans():
transposed = [[matrix1[j][i] for j in range(len(matrix1))] for i in range(len(matrix1[0]))]
show_result("Transposed matrix1:", transposed)
for i, row in enumerate(matrix):
for j, val in enumerate(row):
Label(result_window, text=str(val), width=5, borderwidth=1, relief="solid").grid(row=i, column=j)
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
Hi, i'm newbee in coding and my teacher gave me the task to create two matrices, type = m*n, the code has to summarize, subtract and transpose. I have a bit of code, but idk what to do next.
The code:
from tkinter import *
from tkinter import messagebox
def create_matrices():
try:
n = int(entry_rows.get())
m = int(entry_cols.get())
def plus():
for i in range(len(matrix1)):
for j in range(len(matrix1[0])):
matrix3[i][j] = matrix1[i][j] + matrix2[i][j]
show_result("Sum of matrices:", matrix3)
def minus():
for i in range(len(matrix1)):
for j in range(len(matrix1[0])):
matrix3[i][j] = matrix1[i][j] - matrix2[i][j]
show_result("Difference of matrices:", matrix3)
def trans():
transposed = [[matrix1[j][i] for j in range(len(matrix1))] for i in range(len(matrix1[0]))]
show_result("Transposed matrix1:", transposed)
def show_result(title, matrix):
result_window = Toplevel()
result_window.title(title)
Main window
root = Tk()
root.title('Matrix Operations')
Input for number of rows and columns
Label(root, text="Number of rows (n):").grid(row=0, column=0)
entry_rows = Entry(root)
entry_rows.grid(row=0, column=1)
Label(root, text="Number of columns (m):").grid(row=1, column=0)
entry_cols = Entry(root)
entry_cols.grid(row=1, column=1)
Button to create matrices
Button(root, text="Create Matrices", command=create_matrices).grid(row=2, columnspan=2)
Operation buttons
Button(root, text="Add", command=plus).grid(row=3, column=0)
Button(root, text="Subtract", command=minus).grid(row=3, column=1)
Button(root, text="Transpose matrix1", command=trans).grid(row=4, columnspan=2)
Start the GUI loop
root.mainloop()
Beta Was this translation helpful? Give feedback.
All reactions