Explanation::
- Code in PYTHON is given below.
- Please read all the comments for better
understanding of the code.
- As required, elements from array1 and array2 are
added into a text file named data.txt.
- Tough I have noticed one mistake you did while
calculating results array(This is not asked but still I think it's
a mistake. There is no need of three for loops! Just one for loop
is enough. I have provided the code in comments
format.).
- Screenshots of the CODE is also provided for the
problem of IDENTATION in PYTHON.
- Screenshot of the OUTPUT of data.txt is also
provided at the end.
Code in PYTHON::
import math
from numpy import zeros
array1 = zeros(100, float)
for x in range(100):
array1[x] = math.sin((2*math.pi*x/100))
array2 = zeros(100, float)
for y in range(100):
array2[y] = math.cos((2*math.pi*y/100))
results = zeros(100, float)
"""
I think yoou are doing this calculation wrong..
There is no need of nested 3 for loops.
See below I have provided in comment format the code.
Please verify.
"""
for i in range(len(array1)):
for j in range(len(array2)):
for k in range(len(array2)):
results[i] += array1[i]*array2[i]
"""
for i in range(len(array1)):
results[i] += array1[i] * array2[i]
"""
totresults = sum(results)
print (totresults)
"""
Now the part to write data into file.
The file name is data.txt in which I append the array1 and array2 elements.
An file object is created to write data into the file data.txt
"""
file=open("data.txt","w")
"""
Following for loop iterates through both the arrays named array1 and array2.
In each iteration we add current value from array1 and array2 separating with a tab length space.
"""
for i in range(len(array1)):
"""
All the values added into file are in string format.
"""
file.write(str(array1[i]))
file.write("\t")
file.write(str(array2[i]))
file.write("\n")
file.close()
Screenshot of the code::
Screenshot of data.txt OUTPUT::
Note:: As there are 100 lines just first few lines
of data.txt is given::
Please provide the feedback.
Thank You!!!
PC GuiWithTkinter [CUsers Bhushan\PycharmProjectsl GuiWithTkinter]-.SinCos,py - PyCharm Community Edition 2017.1.5 Eile Edit View Navigate Code Befactor Run Iools VCS Window Help GuiWith Tkinter SinCos.py Vertical Line.py х 나 TeacherPayment y ㄨ -Orthogonal py × MakeString y food utilities py х 나 Food Main y GSincos py MouseEvent y x ClassPey.py import math E-from numpy import zeros arrayl-zeros (100, float) for x in range (100): array! [x] math. sin((2*math . pi*x/100)) = array2-zeros (100, float) for y in range (100) 10 array2 [y]math.cos ( (2*math.pity/100) ) resultszeros (100, float) 13 15 16 17 18 19 I think yoou are doing this calculation There is no need of nested 3 for loops See below I have provided in comment format the code Please verifv for i in range (len (array1)): for j in range (len (array2) ) : 21 for k in range (len (array2)): results [i]+arrayl[il array2[i] 23 39:37 n/a UTF-81 Q
PC GuiWithTkinter [CUsers Bhushan\PycharmProjectsl GuiWithTkinter]-.SinCos,py - PyCharm Community Edition 2017.1.5 Eile Edit View Navigate Code Befactor Run Iools VCS Window Help SinCosQ GuiWith Tkinter SinCos.py Vertical Line.py х 나 TeacherPayment y ㄨ -Orthogonal py × MakeString y food utilities py х 나 Food Main y GSincos py MouseEvent y x ClassPey.py results [i]arrayl[ilarray2[i] 23 24 25 26 27 for i in range (Len (array1)): results[i arrayl[i] array2[i] totresults-sum (results) 29 print (totresults) 31 Now the part to write data into file The file name is data.txt in which I append the arrayl and array2 elements An file object is created to write data into the file data.txt file-open ("data.txt""w") 38 39 ollowing for loop iterates through both the arrays named arrayl and array2 In each iteration we add current value from arrayl and array2 separating with a tab length space. 41 for i in range (len (arrayl)): 43 All the values added into file are in string format. 45 file.write (st.r (arravlil) 13:4 n/a UTF-81 Q
PC GuiWithTkinter [CUsers Bhushan\PycharmProjectsl GuiWithTkinter]-.SinCos,py - PyCharm Community Edition 2017.1.5 Eile Edit View Navigate Code Befactor Run Iools VCS Window Help .GuiWithTkinter İl SinCos.py SinCosQ Vertical Line.py х 나 TeacherPayment y ㄨ -Orthogonal py × MakeString y food utilities py х 나 Food Main y GSincos py MouseEvent y x ClassPey.py the Tile name 1s data.txt in which Iappend the arrayl and arrayz elements An file object is created to write data into the file data.txt 34 35 36 37 file-open ("data.txt"") Following for loop iterates through both the arrays named arrayl and array2 In each iteration we add current value from arrayl and array2 separating with a tab length space 39 for i in range (len (array1)): 43 All the values added into file are in string format file.write(str (arrayl [i])) file.write("t" file.write (str (array2 [i])) file.write("In") file.close () 13:4 n/a UTF-81 Q
CNUsers1Bhushan\PycharmProjects GuiWith Tkinterldata.tt - Notepad-- File Edit Search View Encoding Language Settings Tools Macro Run Plugins Window 1 0.0 1.0 2 0.0627905195293 0.998026728428 3 0.125333233564 0.992114701314 4 0.187381314586 0.982287250729 5 0.248689887165 0.968583161129 6 0.309016994375 0.951056516295 7 0.368124552685 0.929776485888 8 0.425779291565 0.904827052466 9 0.481753674102 0.876306680044 10 0.535826794979 0.844327925502 11 0.587785252292 0.809016994375 12 0.637423989749 0.770513242776 13 0.684547105929 0.728968627421 14 0.728968627421 0.684547105929 15 0.770513242776 0.637423989749 16 0.809016994375 0.587785252292 17 0.844327925502 0.535826794979 18 0.876306680044 0.481753674102 19 0.904827052466 0.425779291565 20 0.929776485888 0.368124552685 21 0.951056516295 0.309016994375 22 0.968583161129 0.248689887165 23 0.982287250729 0.187381314586 24 0.992114701314 0.125333233564 25 0.998026728428 0.0627905195293 26 1.0 6.12323399574e-17 27 0.998026728428 -0.0627905195293 28 0.992114701314 -0.125333233564 29 0.982287250729 -0.187381314586 30 0.968583161129 -0.248689887165 31 0.951056516295 -0.309016994375 32 0.929776485888 -0.368124552685 Normal tet file length: 3,161 lines 101 Ln: 17 Col: 31 Sel: 010 Windows (CR LF) UTF-8 INS