C++ Array

declare arrays:

arraytype arrayName[arraySize]; //basic syntax

example:

int zakiLive[10];

Initialize arrays:

int zakiLive[5]={4,5,6,8};

zakiLive[3]=8
It means value 8 is in 3rd index in array zakiLive as index in array count from 0 and it goes on..
Here is an example of array

#include<iostream>
using namespace std;

#include<iomanip>
using std::setw;

int main(){

int n[10]; //n is an array of 10 integers

for(int i=0;i<10;i++){
    n[i]=i+100;
}
cout<<"Element"<<setw(15)<<"Value"<<endl; //here setw() built in library function is using to make a gap between Element and Value

//output
for(int j=0;j<10;j++){
    cout<<setw(2)<<j<<setw(19)<<n[j]<<endl;
}

return 0;
}

C++ Arrays have some types:

Multi-Dimensional Arrays:

 

syntax can be like this

array_data_type  array_name[size1][size2][size3]……..[sizeN];

Two Dimensional Arrays(It is one kind of multidimensional arrays):

type arrayName[x][y];

Initialize Two Dimensional Arrays:

int a[3][4]={
{0,1,2,3},
{4,5,6,7},
{8,9,10,11}
};

We can also write equivalent like this

int a[3][4]={0,1,2,3,4,5,6,7,8,9,10,11};

We can accesss Two-Dimensional Array elemets by

int valueArray=a[2][3];

Here is a code

#include<iostream>
using namespace std;

int main()
{
    //declaring an array with 5 rows and 2coloumns
int a[5][2]={
{0,0},
{1,2},
{2,4},
{3,6},
{4,8},
};
//output
for(int i=0;i<5;i++){
    for(int j=0;j<2;j++){
        cout<<"a["<<i<<"]"<<"["<<j<<"] : ";
        cout<<a[i][j]<<endl;
    }
}

    return 0;
}

C++ Pointer to an Array

#include<iostream>
using  namespace std;


int main(){

double balance[5]={100.09,2.8,3.4,17.8,8.8};
double *p;

p=balance;

//output eache array elements value by pointer p
cout<<"endl";
cout<<"Array values using pointer"<<endl<<endl;
for(int i=0;i<5;i++){
cout<<"*(p+"<<i<<"): ";
cout<< *(p+i) <<endl;
}

//the output means it is pointing to the array balance by *p to p=balance;

//now the bvalance will give some other output also
cout<<"Array values using balance as  address"<<endl<<endl;
for(int i=0/* we are giving here 0 as any array starts from 0*/;i<5;i++){
cout<<"*(balance+"<<i<<" )";
cout<<*(balance+i)<<endl;
}

return 0;
}

Here it is happeing like this shown above.

There are another 2 types of C++ array functioning with pointer

Passing Arrays to Function and Returning Array From Functions

Now let discuss in details

Passing Arrays to Function:

What we know from the function  synatx is

function_type function_name(arguments_type arguments_name,arguments_type arguments_name){

function_body;

return function_type;

}

But it’s not possible to pass a full array to the argument by using an index number..But we can pass an array by using without an index..This process can be done by three steps

Way-1
Formal Parameters as a pointer

void myFunction(int *param)
{

//Statements

}

Way-2
Formal Parameters as a size array as follows

void myFunction(int param[10])
{
//statements
}

Way-3
Formal Paramaeters as an unsized array as follows

void myFunction(int param[])
{

//statements

}

The example given here with array without size

#include<iostream>
using namespace std;

double getAverage(int arr[],int sizes);

int main(){

int balance[5]={1000, 2, 3, 17, 50};
double avg;

//passing pointer to the array as an argument.
avg=getAverage(balance,5);

//output the returned value
cout<<"Average value is : "<<avg<<endl;

return 0;
}

double getAverage(int arr[], int sizes)
{

int i,sum=0;
double avg;

for(i=0;i<sizes;++i)
{
    sum+=arr[i];
}

avg=double(sum)/sizes;

return avg;
}

 

Return Array from Functions:

#include<iostream>
#include<ctime>
using namespace std;

int *getRandom()
{

static int r[10];
//set the seed for random numbers

srand( (unsigned)time( NULL ) );
for(int i=0;i<10;++i)
{
r[i]=rand();
cout<<r[i]<<endl;
}
 return r;
}

int main()
{

int *p;

p=getRandom();
// a pointer to an int

for(int i=0;i<10;i++){
    cout<<"(p+"<<i<<") :";
    cout<<*(p+i)<<endl;
}


    return 0;
}

We will discss this later more In Sha Allah 🙂

It would be a great help, if you support by sharing :)
Author: zakilive

Leave a Reply

Your email address will not be published. Required fields are marked *