English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

Arrays used in Computer Subject.

2006-07-23 23:13:31 · 3 answers · asked by Jonyl 1 in Computers & Internet Programming & Design

3 answers

I created a list of instances, call it pop, so pop[i] is an instance of
> the boat class, and I made pop[0], pop[1], etc., all boats.

'pop' is not a good choice for a name, but that's not causing the
symptoms that you mentioned.

> I tried passing pop[i] to the function, so it would know that pop[i] was
> a boat, but that didn't work either. (same error)

Each object carries a pointer to its own type information. There are
no such things as declarations. If pop[i] is an int, then it's an int,
period. This isn't C; you can't do a cast like (boat instance
*)(pop[i]) ...
>
> pop, the list of boats, is global. I put a global statement in the
> function.

Presence/absence of "global pop" should make no difference.

As another poster suggested, you should post code; the minimal amount
that demonstrates the problem. In the meantime, here is a minimal code
that follows your description and doesn't have the problem.

pop = []
class boat:
def go(self):
print "go:", repr(self)
def the_func(i):
global pop # irrelevant
pop[i].go()
if __name__ == "__main__":
for k in range(5):
obj = boat()
print "creation:", k, repr(obj)
pop.append(obj)
print repr(pop)
for k in range(5):
the_func(k)

You should compare this with your code --- one glaring difference will
be the print statements at instance creation time. You are getting
ints instead of boat instances. Putting in print statements just might
help you find out why.
What are the values of the ints that you are creating? If pop refers
to [0, 1, 2, 3, 4, ....] that should give you a big hint :-)

Below is what I get when I run my code. You should get something
similar; the hex addresses will of course differ.

creation: 0 <__main__.boat instance at 0x007677E8>
creation: 1 <__main__.boat instance at 0x007680D0>
creation: 2 <__main__.boat instance at 0x00767DB0>
creation: 3 <__main__.boat instance at 0x007688A8>
creation: 4 <__main__.boat instance at 0x00769700>
[<__main__.boat instance at 0x007677E8>, <__main__.boat instance at
0x007680D0>, <__main__.boat instance at 0x00767DB0>, <__main__.b
oat instance at 0x007688A8>, <__main__.boat instance at 0x00769700>]
go: <__main__.boat instance at 0x007677E8>
go: <__main__.boat instance at 0x007680D0>
go: <__main__.boat instance at 0x00767DB0>
go: <__main__.boat instance at 0x007688A8>
go: <__main__.boat instance at 0x00769700>

2006-07-23 23:53:06 · answer #1 · answered by Anonymous · 0 0

Most often its sequential access and storing. that is one big problem.

Go got to store it that way.. and to process a particular element in array.. u will have to transverse all the way.

if at all you want to process all elements in the array.. the array works well and good.

2006-07-23 23:18:49 · answer #2 · answered by juljulabie 3 · 0 0

1----INSERTION & DELETION is not possible in arrays.
2----Sequential representation
3----loss of memory(since wat ever the array size we specify that much of memory is allocated to array. if we not use totally memory losses.)
4---alterations are not possible
5---it is not possible to give inputs more than the array size
6---we cannot initialize an array using a variable
ex:- int x=5;
int ia[x]; ///////illegal usage
7---array size is not known at compile time
ex:-int ia[ ];
thats y we have dynamic memory allocation
8---arrays cannot be copied or compared

2006-07-23 23:45:06 · answer #3 · answered by ramya 1 · 0 0

fedest.com, questions and answers