You are on page 1of 11

Edward Ku

MIS 301
4/4/2021
Programming Assignment 4

SOURCE CODE:

def info():

print ("The purpose of is to test if a regular list will be considered a magic square.")

def magic_testing(all_data):

index=0

size=0

number=1

while size !=-1:

size=int(all_data[index])

print("The size of the square is:", size)

print("*"*5,"Square",number, "*"*5)

square= [0]*size**2

#counts the values and then prints them as a square

count=index
for i in range(size):

square[i]=[0]*size

for i in range(size):

for j in range(size):

count+= 1

square[i][j]=all_data[count]

print(square[i][j],end=" ")

print()

sum_rows(square, size)

sum_columns(square, size)

sum_main_diag(square, size)

sum_off_diag(square,size)

print()

index=index+1+len(square)

#computes the sum of rows

def sum_rows(square,size):

sum_rows=[0]*size

for i in range(size):

for j in range(size):

sum_rows[i]=sum_rows[i]+int(square[i][j])
print("Sum of row:", sum_rows[i])

return sum_rows

#computes the sum of columns

def sum_columns (square, size):

sum_columns=[0]*size

for i in range (size):

for j in range(size):

sum_columns[j]=sum_columns[j]+int(square[i][j])

print("Sum of Columns:", sum_columns[j])

return sum_columns

#computes the sum of the main diagonal

def sum_main_diag(square, size):

sum_main_diag=0

for i in range(size):

sum_main_diag=sum_main_diag+int(square[i][i])

print("Sum of the main diagonal:", sum_main_diag)

return sum_main_diag

#Computes all the other diagonal

def sum_off_diag(square,size):

sum_off_diag=[0]*size

for i in range(size):

for j in range(size):
sum_off_diag[j]=sum_off_diag[j]+int(square[i][j])

print("Sum of the other diagonal:",sum_off_diag[j])

return sum_off_diag

#This main funtion puts everything together

def main():

info()

square_data=open("\\Users\\Edward\\Desktop\\magicData.txt")

all_data=square_data.read()

all_data=all_data.split()

magic_testing(all_data)

#runs the code

main()
SCREENSHOT OF CODE:
SCREENSHOT OF RESULTS

You might also like