including array in structured module

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • coolindienc
    New Member
    • Aug 2006
    • 48

    #1

    including array in structured module

    I got the program below and now I want to put this in a structured module. Andy suggestions?

    Andy

    Code:
    from numarray import *
    
    n=input("Enter number of elements: ")
    x=zeros(n,Int)
    for i in range(0,n):
        print "Enter the value for x subscript ",i
        x[i]=input()
    
        print x
    
    print "The final output is a List of n elements where n is:",n
    raw_input("Press enter to exit.")
  • bartonc
    Recognized Expert Expert
    • Sep 2006
    • 6478

    #2
    Code:
    from numpy import *
    
    def InitArray(n):    # this is a comment. below is a docstring (self documenting code is a very good habit to get into)
        """Given the number of elements (n), return a 1d array"""
        return zeros(n, int16)
    
    def GetInput():
        return input("Enter number of elements: ")
    
    def FillArray(xArray, nElems):
        for i in range(nElems):
            print "Enter the value for x subscript ",i
            xArray[i]=input()
    
            print xArray
    
    if __name__ == "__main__":
        n = GetInput()
        myArray = InitArray(n)
        FillArray(myArray, n)
        print "The final output is an Array of %d elements is:" %n
        print myArray
        for x in myArray:
            print x
        raw_input("Press enter to exit.")
    I don't have numarray module. numpy is more up to date and complete. You can change it back to numarray, but this is tested.

    Comment

    • bartonc
      Recognized Expert Expert
      • Sep 2006
      • 6478

      #3
      Originally posted by bartonc
      Code:
      from numpy import *
      
      def InitArray(n):    # this is a comment. below is a docstring (self documenting code is a very good habit to get into)
          """Given the number of elements (n), return a 1d array"""
          return zeros(n, int16)
      
      def GetInput():
          return input("Enter number of elements: ")
      
      def FillArray(xArray, nElems):
          for i in range(nElems):
              print "Enter the value for x subscript ",i
              xArray[i]=input()
      
              print xArray
      
      if __name__ == "__main__":
          n = GetInput()
          myArray = InitArray(n)
          FillArray(myArray, n)
          print "The final output is an Array of %d elements is:" %n
          print myArray
          for x in myArray:
              print x
          raw_input("Press enter to exit.")
      I don't have numarray module. numpy is more up to date and complete. You can change it back to numarray, but this is tested.
      This shows how mutable objects get passed by reference and can be change in a fuction without a need for returning the reference. This is advanced stuff to use, but a basic concept that needs to be understood early in your education.

      Comment

      Working...