473,396 Members | 1,683 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

including array in structured module

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

Andy

Expand|Select|Wrap|Line Numbers
  1. from numarray import *
  2.  
  3. n=input("Enter number of elements: ")
  4. x=zeros(n,Int)
  5. for i in range(0,n):
  6.     print "Enter the value for x subscript ",i
  7.     x[i]=input()
  8.  
  9.     print x
  10.  
  11. print "The final output is a List of n elements where n is:",n
  12. raw_input("Press enter to exit.")
  13.  
Dec 9 '06 #1
2 1405
bartonc
6,596 Expert 4TB
Expand|Select|Wrap|Line Numbers
  1. from numpy import *
  2.  
  3. def InitArray(n):    # this is a comment. below is a docstring (self documenting code is a very good habit to get into)
  4.     """Given the number of elements (n), return a 1d array"""
  5.     return zeros(n, int16)
  6.  
  7. def GetInput():
  8.     return input("Enter number of elements: ")
  9.  
  10. def FillArray(xArray, nElems):
  11.     for i in range(nElems):
  12.         print "Enter the value for x subscript ",i
  13.         xArray[i]=input()
  14.  
  15.         print xArray
  16.  
  17. if __name__ == "__main__":
  18.     n = GetInput()
  19.     myArray = InitArray(n)
  20.     FillArray(myArray, n)
  21.     print "The final output is an Array of %d elements is:" %n
  22.     print myArray
  23.     for x in myArray:
  24.         print x
  25.     raw_input("Press enter to exit.")
  26.  
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.
Dec 9 '06 #2
bartonc
6,596 Expert 4TB
Expand|Select|Wrap|Line Numbers
  1. from numpy import *
  2.  
  3. def InitArray(n):    # this is a comment. below is a docstring (self documenting code is a very good habit to get into)
  4.     """Given the number of elements (n), return a 1d array"""
  5.     return zeros(n, int16)
  6.  
  7. def GetInput():
  8.     return input("Enter number of elements: ")
  9.  
  10. def FillArray(xArray, nElems):
  11.     for i in range(nElems):
  12.         print "Enter the value for x subscript ",i
  13.         xArray[i]=input()
  14.  
  15.         print xArray
  16.  
  17. if __name__ == "__main__":
  18.     n = GetInput()
  19.     myArray = InitArray(n)
  20.     FillArray(myArray, n)
  21.     print "The final output is an Array of %d elements is:" %n
  22.     print myArray
  23.     for x in myArray:
  24.         print x
  25.     raw_input("Press enter to exit.")
  26.  
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.
Dec 9 '06 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: WindAndWaves | last post by:
Hi Gurus I hope I am going to make sense with this question: I have an html page that I have turned into a php page with a bit of php code above the html (connect to database, massage data a...
0
by: Phil | last post by:
Hi, I don't understand this strange behaviour: I compile this code : #include <Python.h> #include"Numeric/arrayobject.h" static PyObject *
8
by: Bo Peng | last post by:
Dear list, I am writing a Python extension module that needs a way to expose pieces of a big C array to python. Currently, I am using NumPy like the following: PyObject* res =...
2
by: Cary | last post by:
This may reveal my poor programming skills, but here goes... I'm building a pricing tool for my business. I'm nearing the end of the project, and I've been asked to be able to save quotes in some...
11
by: godsent | last post by:
hi my question goes like this Write a program that can store stidents' marks into a structured array.the array will store the test marks for 10 students.Each student is required to take 3...
2
by: Wes Peters | last post by:
Does anyone know of an article that deals with the subject of reading a structured text file using VBA code in Access? Thanks, Wes
12
by: einsanic | last post by:
Dear everyone, I am new to this forum and I am realitevely new to C programming so please forgive me for any basic mistakes I'll be making. I am trying to dynamically allocate the space for...
0
by: Bob Alston | last post by:
I am doing volunteer work with a human services referral agency. The want me to build a database of referral data - other agencies, sources of information, etc. Ideally it would be a structured...
1
AmberJain
by: AmberJain | last post by:
HELLO, First of all, I accept that this is a too simple question but I got different opinions in different books and so I'm posting it here...... The question is simple.....Is C a block...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.