Sunday, July 12, 2009

Teach me step-by-step? Making Array in C++?

I would just like to know how to create an array in C++, be it simple or not.


And, if you explained to me what each part means/does it'd help a ton.


Thanks!

Teach me step-by-step? Making Array in C++?
Hi, I am Ashim Kumar Sahoo, a software consultant.





An array is a series of elements of the same data type stored in a contiguous memory location.





First of all for creating an array you must declare it and after that initialise it.





Declaration of an array indicates the data type of the array, it may be integer, decimal, string and many others.





Syntax: data type array name[%26lt;size of the array%26gt;];


%26lt;size of the array%26gt; is optional.


If it is provided then it will store only that specified number of elements otherwise as many as you can.





After declaring you have to initialize it, otherwise some default value will be stored. So it always advisable to initialize the array.


Initialization means assigning some values to the array elements.


Syntax:


array name[%26lt;element position%26gt;] = value;


%26lt;element position%26gt; is also optional, it is used to store values on the specified position of the array. If not provided then it stores values in the array in the increasing order.The index position of an array always start from zero(remember that).





Example:


1. Without providing the length of the array


int maths[];


maths[]=1;


maths[]=2;


maths[]=9;





2. int maths[3];


maths[0] = 1;


maths[1] = 2;


maths[2] = 9;





Both the example will give the same result.


In the first case I haven't provided any size and store values into the array. I haven't also provided any index position for stroring value, so in this case it will store value in the array in the ascending order of index position starting from zero.





And in the second case I have instructed the array to hold particular values in a particular index position of an array.





If still you want further assistance then please contact me on ashimkumarsahoo@yahoo.co.in





Thanks %26amp; Regards


Ashim Kumar Sahoo
Reply:Static array





int myArray[10]; allocate an array of 10 integers





Dynamic array





int* myArray = new int[10]; allocate an array of 10 integers


The difference is static allocate memory during compile time and dynamic allocated during run time.
Reply:An array is very straightforward. I'm an old C programmer so I'll tend toward those concepts: Bear with me. They apply.





Think of an array as a block of memory set aside to hold a number of variables of the same type. If you know how many variables you are going to hold, or have a maximum size, you can declare it statically. Thus, int Geoff[5]; or float Nuisance[7];. One special type of array in C is the char array which is known as the ASCIIZ string because it's always terminated by a '\0' character. A string in the C++ Standard Template Library is a record and different, but a C string is a char array which should be at least one character longer than the text stored in it so you can store an '\0' and is mainly accessed by the string.h library in C and the cstring library in C++.





If you don't have a clue as to how many elements you are going to store in your array, and won't until run-time, then you can declare it dynamically, with pointers. This is unfortunate. A static declaration as in the paragraph above means that the compiler does all the housekeeping work of allocating and deallocating your memory for you. You declare it, it finds memory for it and deletes it at the end of the program. With a pointer you have to do it yourself. Thus in C with the variables int Geoff{} and HowBig you would do a if (Geoff=malloc(HowBig*sizeof(int))!=NULL) meaning if you do not fail to set aside HowBig*the size of an int in bytes bytes then -- the program can continue. C++ programmers tend to be less paranoid than us C programmers so more likely to do a simple Geoff=new int[HowBig]; The downside of that -- a dynamically allocated array -- is that when you are finished you have to delete it with a delete [] Geoff; in C++ or a free(Geoff); in C. If you forget to do this then that memory is gone until you reboot and you can crash other programs by tying up all the memory. C and C++ are heavy duty products.





After that there is a question of initialization. A shortcut is available. if you initialize the first variable in either, every variable you don't initialize gets set to zero. Thus always initialize at least the first variable is worth saying -- you don't know what kind of garbage is lurking in your computer unless you've been watching old episodes of ReBoot. And that said any Geoff[i] or Nuisance[i] can be accessed like any other variable of the same type, unless Geoff or Nuisance is a char array in which case you must use the string library to access them.


No comments:

Post a Comment