C++ gurus and learners, we program ourselves :P all work is CC licensed

Tuesday, November 12

Arrays in C++

01:36 By

Hey Guys.
This blog is related to a datatype in c++ that you'll find enormously helpful in your future assignments and semester projects.

So, to answer the very first question : "What is an Array ?"
Formally, An array is a series of elements of the same type placed in contiguous memory locations that can be individually referenced by adding an index to a unique identifier.
Confused ? I'd be surprised if you weren't.

Think of it like this, you have 5 numbers. Now as a beginner in C++, i'll make ten different variables and the store them in separately in these variables. Like this :

int a=1;
int b=5;
int c=11;
int d=19;
int e=30;
Now in order to access these stored values, we'll have to call each element by its distinct name.

By using array, we can use a single datatype to store multiple related elements within a closed structure. This makes it easier for us to access the values and acquire less space in the memory (10 elements in an array use less space than 10 different variables).
A typical way to initialize an array in c++ is :

datatype name [no. of elements] ;


//we shall now declare some arrays

//Suppose we want to make an array of 10 integer numbers. 
int array1[10];

// we can assign elements to the array like this:
int array1[10] = {2,4,6,8,10,12,14,16,18,20};
float array2[5] = {3.14, 2.27, 0.6990, 4.5, 123.4};
string array3[3] = {"Porsche", "Lamborghini" , "Maserati"};

//Note that in an array of 10 elements, values are indexed as 0-9


// now we can access the values in the array using their index number
print(array1[0]); // 0 is the index number of the element in the array
// we'll get 2 in the output

//similarly 

print(array2[3]); //This will print us 4.5
print(array3[1]); //This will print us Lamborghini


//In order to print every element of the array, we use for loop
for (int i=0; i<10; i++)
{
print(array1[i])
print(endl);
}
//This will print every element in the array1
//value of i will determine the index number of the element to be printed

//P.s. use cout statement instead of print command :)

The concept of arrays is very helpful for data structures.
You get a very good idea of memory management in c++. This concept will help you guys a lot in your 1st semester projects. Be it small databases, games, or anything. You'll always find arrays quite helpful.

Until next time.

6 comments :

  1. how will we use pointer for array declaration?

    ReplyDelete
  2. kindly read this new post related to concept :)
    It is much informative in a way for people new to 2d array pointers
    http://cppqa.blogspot.com/2013/11/two-dimension-pointers-c.html

    ReplyDelete
  3. One thing that i forgot to mention,
    you can also assign values in an array seperately using index number. Like

    int arr5[8];

    arr5[0]=10;
    arr5[1]=20;
    arr5[2]=30;
    arr5[3]=40;
    arr5[4]=50; and so on.



    so if u want to edit some value, you don't have to give values to whole array again.

    ReplyDelete
  4. this will print us lamborghini :D :D

    ReplyDelete