Topic :Arrays in C++ Programming: Concept | Declaration | Initialization | Using



Arrays:

An Array is a Variable with a single name that can store multiple values, but with each value indexed by a number. We can think of an Array as a numbered list where we can access elements by number.
Array is an Data Structure which can stores a fixed-size sequential collection of elements of the same type.



Arrays , Infobrother:

Array Can store fixed numbers of elements of the same data type sequentially in Memory. Therefore an integer Arrays holds some number of integers, a Character Array holds some number of characters, and so on. The size of Array is referred to as its dimension as shown in above figure.



Clear Your Concept:

Let's have an short Example to clear our concept of Arrays: in this example we will store the Result of 10 different students and will try to show all of them using Function. in the Example there are two function. first is getData() function used to get all data from user. and the second one is showData() Function used to display all entered Data.



System("CLS");
This is an Built-in Function known as "Clear-Screen" Function. this function is used to clear the previous Screen. This Function is Defined in "Window.h" Header file. for using this function in your Program, make sure you include "Window.h" File in your Program Header.



/*If there was NO Array in C++ ? - InfoBrother */
 
#include <iostream>
#include<Windows.h>  /*For (system("CLS")) method*/
 
using namespace std;
void getData(); //function prototype
void showData(); //function prototype
 
/*We declare 10 different Global Variable to store student Max. as 
these variables are Global, so our both function can access all variables.*/
 
int student0, student1, student2, student3, student4, student5,student6
,student7,student8,student9;
 
 
main() //Main Function Begin.
{
    getData(); //function Calling to Get data From user:
    return 0;
}
 
/*This function will get data from user.*/
void getData() //Function Definition. 
{
 
    cout<<"Enter Student Number: "; 
    cin>>student0;
 
    cout<<"Enter Student Number: "; 
    cin>>student1;
 
    cout<<"Enter Student Number: "; 
    cin>>student2;
 
    cout<<"Enter Student Number: "; 
    cin>>student3;
 
    cout<<"Enter Student Number: "; 
    cin>>student4;
 
    cout<<"Enter Student Number: "; 
    cin>>student5;
 
    cout<<"Enter Student Number: "; 
    cin>>student6;
 
    cout<<"Enter Student Number: "; 
    cin>>student7;
 
    cout<<"Enter Student Number: "; 
    cin>>student8;
 
    cout<<"Enter Student Number: "; 
    cin>>student9;
 
    showData();   //calling of another function inside the function.
}
 
/*Display all entered data:*/
void showData() //Function Defination.
{
    system("CLS");  //clear the screen.
 
    cout<<"Student 0) = "<<student0<<endl;
    cout<<"Student 1) = "<<student1<<endl;
    cout<<"Student 2) = "<<student2<<endl;
    cout<<"Student 3) = "<<student3<<endl;
    cout<<"Student 4) = "<<student4<<endl;
    cout<<"Student 5) = "<<student5<<endl;
    cout<<"Student 6) = "<<student6<<endl;
    cout<<"Student 7) = "<<student7<<endl;
    cout<<"Student 8) = "<<student8<<endl;
    cout<<"Student 9) = "<<student9<<endl;
}


system("CLS") is Built-in function in Window.h Library to use clear the previous screen and display the new output in new window. In order To use this function, We Need to include Window.h Library.


The Above program is absolutely Correct, it's produce the same result that we want. but the code is boring, we repeat same code again and again. we just add here only the result of 10 Student. but what's about if we Need to do same work for 1000 Students. do we Need to declare 1000 different variables, and repeat same code 1000 times. Yep we Have to do this if We don't Know about Array. Array don't let us bore by declaring variables having same types. Let's have a same example of showing student Result. at this time we will create same program using Array.



/*Uses Of Array: InfoBrother */
 
#include <iostream>
#include<Windows.h> 
 
using namespace std;
void getData(); //Function prototype.
void showData(); //Function prototype
 
/* Declare Array for 10 integers to store different student Marks.*/
int student[10];
 
 
main() //Main Function Begin.
{
    getData(); //function Calling to get data from
    return 0;
}
 
//This function will get data from user.
void getData() //Function Definition. 
{	    
	for(int i=0; i<10; i++)//we can used Loop to enter data.
	{
    	cout<<" Enter Student Number: ";
    	cin>>student[i];
	}
 
        showData();     //calling another function inside the function.
}
 
//this function will display all entered data.
void showData() //Function Defination.
{
    system("CLS");  //Clear screen;
    for(int i=0; i<10; i++) //we can used loop to display all entered Data.
    {
        cout<<" Student "<<i<<") = "<<student[i]<<endl;
    }
}


This is how Array help us to create Program Easily. An Array Name actually represents a memory address. when we declare an Array, we tell the compiler to use the Array name to indicate the beginning address of a Series of elements. For Example: if we declare an Array int Array[10]; we are telling the compiler (which is telling the operating system) to reserve memory locations for 10 integers beginning at the memory address that is represented by Array.


we will discuss about memory location in our "Data Structure" course for the time being just clear your concept about Array.


Declaring Arrays:

To declare an Array, we specify three things, The Data type, Variable name and the size of Array. The basic Syntax of Array in C++ is:


Data-Type Variable-Name[Array-Size];


The Array_size Must be an integer constant greater than zero and type can be any valid C++ Data-type. For Example: to declare an Array for 10 Elements having variable name "Element" and data type "int", use this statement to declare Array-


int element[10];


Initialization of Arrays:

There are many ways to initialize an Array. Arrays can be initialized when we define them. Don't use the default Initialization of Arrays. compiler may assign some value to each declared Array. Always initialize the Array in Such a manner that the process is Clear.

We can initialize C++ Array elements either one by one or using a single statement as shown in the given example:


// Array initialization using Single Statement
int Element[ 10 ] = { 10,20,30,40,50,60,70,80,90,100 }; 
 
// Array initialization one by one:
int Element[ 0 ] = 10; 
int Element[ 1 ] = 20;
...
...
int Element[ 10 ] = 100;


The Number of values between braces { } cannot be larger than the number of elements that we declare for the Array between square brackets [ ]. if we Omit the size of the Array, an Array just big enough to hold the initialization is Created. Therefore, if we write-

int Element[ ] = { 10,20,30,40,50,60,70,80,90,100 }; 

We will create exactly the same Array as we did in the previous Example. We can initialize an Array using a Loop while assigning some value. Let's take same Array example to initialize value using For-Loop


int Element [ 10 ]; 
 
for (int i = 0 ; i < 10 ; i ++)
{ 
     Element[ i] = 0; 
}


in the above Example, we have initialized all element of Array "Element" to Zero.



Note that: Array index is one less than the size of the Array. because it will start from 0. so if we declare Array of 10. so the index will be "0 to 9".


/* Array Initialization: InfoBrother */
 
#include <iostream>
using namespace std;
 
main() //Main Function Begin.
{
    //initialization of Arrays using Single Statement:
    int element[10]={10,20,30,40,50,60,70,80,90,100}; 
 
    //Using Loop to display stored data in Arrays:
    for(int i=0; i<10; i++)
    {
        cout<<"Element ["<<i<<"] = "<<element[i]<<endl;
    }
}


Accessing Array Elements:

Once an Array is filled with useful values, we can access and use an individual array element in the same manner we would access and use any single variable of the same type. Any Element is accessed by indexing the array name. This is done by placing the index of the element within square brackets after the name of the array. For Example:


int Item = Element[ 7 ];


In the above statement, the Assignment (=) operator will assign the value of 7th Element of Array to variable "Item". Let's have a clear example to understand this.


/* Array Initialization:  InfoBrother*/
 
#include <iostream>
using namespace std;
 
main() 
{    
    //initialization of Arrays using Single Statement:
    int element[10]={10,20,30,40,50,60,70,80,90,100}; 
 
    //Using Loop to display stored data in Arrays:
    for(int i=0; i<10; i++)
    {
        cout<<"Element ["<<i<<"] = "<<element[i]<<endl;
    }    
 
    int item= element[7]; //Assign 7th element of Arrays to Item.
    cout<<" \n\n The value of Item is: "<< item;
}




When We work with Arrays, it is easy to forget that the first element is 0, not 1. so always pay attention while using Arrays. Always try to allocate Memory one greater than to use. For Example: if you Need to store "10" integers in Arrays, then Allocate Memory for "11" Integers.







I Tried my Best to Provide you complete Information regarding this topic in very easy and conceptual way. but still if you have any Problem to understand this topic, or do you have any Questions, Feel Free to Ask Question. i'll do my best to Provide you what you need.

Sardar Omar.
InfoBrother





WRITE FOR INFOBROTHER

Advertising






Advertisement