Thursday, November 15, 2007

Arrays

Understanding arrays isnt difficult if you are fimiliar with variables and their manipulation.

Take a look at the following points carefully

  • Variable is a short term memory space that can be changed manipulated during the program run-time and after the program ends.. it is lost.

c++ style ..........................................

int a,b ; // variable with the name 'a' and 'b' is set in memory (temprary) .


..................... .....................

Now that you have told the compiler to reserver momery (4 bits in integer case) for these variables you can manipulate them where ever you want in the program. Use them overwrite them do wateva you want. Please never hesitate to do whatever u think with variables computer will never crash. Our university's student have fear of something i dont know of what. But they really hesitate to write the code. I will definetly write another post about planning and implenting a program.

  • Array is a group of variables with the same name (not exactly). You have to actually understand where you need a group of variables with same name so that you can manipulate (use) them, easily and use all of them at once.

    Now suppose you need ten different variables of same datatype. A programmer who doesn't know about arrays is off course gonna do the following
    int var1, var2, var3, var4, ... till ... var10;
    Now if you observe the above you can still see the index i.e var "1" and "2" and goes till "10". Arrays are handled by the compiler the same way. Compiler makes a stack of variables but you have to tell the compiler the size of variable stack.
  • Note: Size is different thing and index (unique ID of the variable in the stack) is different thing.

c++ style ....................................................

int ar[10]; //This is the size of the array i. e how many variables the stack will contain

................................................

c++ style ....................................................

ar[0]=1;
ar[1]=4;
ar[2]=5;
.
.
.

//'[0]' this is the index (unique id here is '0') infact ar[0] is a varaible that has nothing to do with ar[1] or rest of the ar[]S.

.............................................

Last but not the least donnot forget that array is a group of variables.

1 comment:

Sibte7 said...

cool work..........