Question

Recreate the one hundred element arrays of sin and cos you created in Exercise 3.10. Print these arrays out to a file in the form of a table. The first line of the table should contain the first element of both arrays, the second line the second element, an so on. Keep the file as we will use it later.

Exercise 3.10:

EXERCISE 3.10 Using for loops, range ), and the zeros ) function, construct two 100 element arrays, such that element i of one array contains sin(2πǐ/100) and the corresponding element of the other contains cos(2πǐ/100). Compute the scalar (i.e. dot) products of the two arrays, to check that sin and cos are orthogonal, i.e. their dot product is zero. The scalar, or dot, product is defined as: x.y-Σ zi.yi Note: We have only considered the features of arrays that are common to most other programming languages. How ever, Pythons arrays are extremely powerful and can do some stuff that would have to be done manually (perhaps using for loops) in other languages. If you find you are using arrays in the problem then it is worth taking a look at Section 4.2,Arrays. You will also note there is a function in the Numeric library that will calculate the dot product of two arrays for you! We want you to do it the hard way though.

My solution to 3.10:

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)

for i in range(len(array1)):
for j in range(len(array2)):
for k in range(len(array2)):
results[i] += array1[i]*array2[i]
totresults = sum(results)
print (totresults)

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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::

PC GuiWithTkinter [CUsers Bhushan\PycharmProjectsl GuiWithTkinter]-.SinCos,py - PyCharm Community Edition 2017.1.5 Eile EditPC GuiWithTkinter [CUsers Bhushan\PycharmProjectsl GuiWithTkinter]-.SinCos,py - PyCharm Community Edition 2017.1.5 Eile EditPC GuiWithTkinter [CUsers Bhushan\PycharmProjectsl GuiWithTkinter]-.SinCos,py - PyCharm Community Edition 2017.1.5 Eile Edit

Screenshot of data.txt OUTPUT::

Note:: As there are 100 lines just first few lines of data.txt is given::

CNUsers1Bhushan\PycharmProjects GuiWith Tkinterldata.tt - Notepad-- File Edit Search View Encoding Language Settings Tools Ma

Please provide the feedback.

Thank You!!!

Add a comment
Know the answer?
Add Answer to:
Recreate the one hundred element arrays of sin and cos you created in Exercise 3.10. Print...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT