Person people[1000]; |
{ Person temp("Fred","Smithhh","123 Ant St","Frog City"); people[next]=temp; next+=1; } |
Person *people[1000]; |
{ Person *temp; temp=new Person("Fred","Smithhh","123 Ant St","Frog City"); people[next]=temp; next+=1; } |
{ Person *temp=new Person("Fred","Smithhh","123 Ant St","Frog City"); people[next]=temp; next+=1; } |
people[deadun]=NULL; |
delete people[deadun]; people[deadun]=NULL; |
{ int *p; int a; int b[1000]; |
{ int *A=new int[100]; // happily use the array A[0] to A[99] for a while for (int i=0; i<100; i+=1) { sum = sum + A[i]; product = product * A[i]; } // happy happy happy smile smile smile // Now realise that A isn't big enough. We need 10000 elements, so... A=new int[10000]; // That's all we need to do. Now we can happily use A[0] to A[9999]. // It could hardly be easier. |
int *ChangeIntArraySize(int *A, int oldsize, int newsize) { int *newarray=new int[newsize]; int i, smallest; if (oldsize<newsize) smallest=oldsize; else smallest=newsize; for (i=0; i<smallest; i+=1) newarray[i]=A[i]; delete[] A; return newarray; } |
{ int *A=new int[100]; // happily use the array A[0] to A[99] for a while for (int i=0; i<100; i+=1) { sum = sum + A[i]; product = product * A[i]; } // happy happy happy smile smile smile // Now realise that A isn't big enough. We need 10000 elements, so... A=ChangeIntArraySize(A,100,10000); // That's all we need to do. Now we can happily use A[0] to A[9999]. // It could hardly be easier. |
{ Person **everyone=new (Person*)[100]; int array_size=100; // happily use the array everyone[0] to everyone[99] for a while sum=0; for (int i=0; i<100; i+=1) { sum = sum + everyone[i]->weight; } printf("Average weight = %d\n", sum/100); // happy happy happy smile smile smile // Now realise that it isn't big enough. We need 10000 elements, so... everyone=IncreaseArray(everyone,array_size,array_size+100); array_size=array_size+100; // That's all we need to do. Now we can happily use everyone[0] // to everyone[199]. It could hardly be easier. |
class Population { public: Person **everyone; int array_size; Population(void); // the constructor void GrowArray(int newsize); Person *person(int i); // the access function etc etc etc; }; |
void main(void) { Population world; // add data here. // play around with the data world.GrowArray(1000); // play around with the larger array printf("This is the entire population of the world:\n"); for (int i=0; i<1000; i+=1) world.person(i)->print(); // continue playing... |
void main(void) { Population world; world[1]=new Person("Fred","Astaire","3982492 NE 13342nd St"); world[100000]=new Person("Ginger","Rogers","3982494 NE 13342nd St"); printf("This is the entire population of the world:\n"); for (int i=0; i<world.size; i+=1) world[i]->print(); } |
Person * & Population::operator[](int n) { if (n>=array_size) { Person **temp=new (Person *)[n+1]; for (int i=0; i<array_size; i+=1) temp[i]=everyone[i]; array_size=i+1; } return (everyone[i]); } |