Thursday, July 30, 2009

Can someone explain arrays easier then this?

i am learning Arrays in C++ but I don't really know what it means.PLease help me on arrays.


This is the program i am working on:





#include %26lt;iostream%26gt;





using namespace std;





int main(){





int myArray[3];





cout %26lt;%26lt; "Enter a number: ";


cin %26gt;%26gt; myArray[0];





cout %26lt;%26lt; "Enter a second number: ";


cin %26gt;%26gt; myArray[1];





myArray[2] = myArray[0]+myArray[1];





cout %26lt;%26lt; "The sum is: " %26lt;%26lt; myArray[2];





system("pause");





}.

Can someone explain arrays easier then this?
I think learning by example is the easiest.





int myArray[3] means you have declare an array call: myArray and the data type is integer and total capacity of it is 3.





It is just like you declare a normal variable, just that array simplify your programming.





You could declare the 3 integer variable to store the values instead of an array but practically it is too much work and hard to maintain.





So, an array is an easy way for you to store related data using the same variable array name.





Example:





int myBankAccount1, myBankAccount2, myBankAccount3;





as compare to: int myBankAccount[3];





Now let say you need to initialize each bank account to start with 0:





so in the coding:





myBankAccount1 = 0;


myBankAccount2 = 0;


myBankAccount3 = 0;





and in array case:





for(int i=0; i%26lt;3; i++)


myBankAccount[i] = 0;








Now imagine you are dealing with 10,000 bank accounts. Then you will realise you have to use Array to handle it better than just declare 10,000 variables.
Reply:Here's a page with some basic explanations -





http://www.codersource.net/c++_arrays_tu...





Joyce


http://www.DesignByJoyce.com/
Reply:Arrays act as a container for values of similar data types (actually the addresses of the values) Think of an array as a box of index cards each numbered from front to back. Each card can hold only one value but the box keeps all these similar values in one place where you can find or use those values.


No comments:

Post a Comment