You are on page 1of 14

Lecture 10

Copying array, Multi-Dimensional array

Prepared by Aqsa Gul, Lecturer Institute of Management Sciences Peshawar


Copying Arrays

 Data types should be


identical

 Size should be same


int a [ 10 ] ;
int b [ 10 ] ;

Prepared by Aqsa Gul, Lecturer Institute of Management Sciences Peshawar


To copy from array “ a ” to array “ b ” :

b[0]=a[0];
b[1]=a[1];
b[2]=a[2];
b[3]=a[3];
………
………
b [ 10 ] = a [ 10 ] ;

Prepared by Aqsa Gul, Lecturer Institute of Management Sciences Peshawar


for ( i =0 ; i < 10 ; i ++ )
{
b[i]=a[i];
}

Prepared by Aqsa Gul, Lecturer Institute of Management Sciences Peshawar


Example
int main()
{
int source[10]={12,31,24,15,17,34,32,81,78,20};
int dest[10], i;
/*Copy all elements from source array to dest array */
for( i=0; i<10; i++)
{
dest[i] = source[i];
}
/*Print all elements of dest array */
cout<<"Elements of dest array are : "<<endl;
for(i=0; i<10; i++)
{
cout<<dest[i]<<"\t";
}
return
Prepared by Aqsa0;
Gul, Lecturer Institute of Management Sciences Peshawar

}
Task

 Input your name and display it in reverse order

Prepared by Aqsa Gul, Lecturer Institute of Management Sciences Peshawar


Comparing Two arrays
int equal = 0 ;
int num1 [ 100 ] , num2 [ 100 ] ;
for (int i = 0 ; i < 100 ; i ++ )
{
if ( num1 [ i ] != num2 [ i ] )
{
equal = 1 ;
break ;
}
}
if ( equal ==1 )
cout << “ The arrays are not equal” ;
else
cout << “ The arrays are equal” ;
Prepared by Aqsa Gul, Lecturer Institute of Management Sciences Peshawar
Multi Dimensional array

Input
Row 1 1 2 3
Row 2 Memory
4 5 6
Row 3 7 8 9 Row 3 7 8 9
Row 2 4 5 6
Row 1 1 2 3

Prepared by Aqsa Gul, Lecturer Institute of Management Sciences Peshawar


Addressing Multi-D array

 a [rowIndex ] [ columnIndex ]

maxRows = 3;
maxCols = 3;

Index of Start [0] 1 2 3


[1]
Index of Last Row = maxRows - 1 [2]

Prepared by Aqsa Gul, Lecturer Institute of Management Sciences Peshawar


Example
int row ;
int col ;
const int maxRows = 3 ; //identifier maxRows
const int maxCols = 3 ;

int a [ 3 ] [ 3 ] ; // 2-D array

for ( row = 0 ; row < maxRows ; row ++ )


{
for ( col = 0 ; col < maxCols ; col ++ )
{
cout<<"Please enter value element number "<<row<< "," << col<<"\t";
cin >> a [ row ] [ col ] ;
}
}
Prepared by Aqsa Gul, Lecturer Institute of Management Sciences Peshawar
Tasks

 Write a Program in C++ to add two arrays


Int Array1[] ={2,3,4,5,6}
Int array2[] ={1,2,3,4,5}
Int sumarray[]={3,5,7,9,11}

 Program to count negative elements in array

 Insert values in two-D array then print it

Prepared by Aqsa Gul, Lecturer Institute of Management Sciences Peshawar


Task 1
int main() {
int array1[] ={2,3,4,5,6}
int array2[] ={1,2,3,4,5}
int sumarray[5];
/* sum the value at index i of both arrays and store it at sumarray index i */
for(int i=0; i<5; i++) {
Sumarray[i] = array1[i]+array2[i];
}
for(int j=0; j<5; j++)
{
Cout<<sumarray[j]<<endl;
}
return 0;
Prepared by Aqsa Gul, Lecturer Institute of Management Sciences Peshawar

}
Task 2
int main() {
int arr[size];
int i, size, count = 0;
/* Input array elements */
cout<<"Enter elements in array : “<<endl;
for(i=0; i<size; i++)
{
cin>>arr[i];
cout<<endl;
}
/* Count total negative elements in array */
for(i=0; i<size; i++)
{
/* Increment count if current array element is negative */
if(arr[i] < 0)
{
count++;
}
}
Prepared by Aqsa Gul, Lecturer Institute of Management Sciences Peshawar
cout<<Total negative elements in array =“<< count;
return 0;
Task 3
Int main(){
Cout<<“Enter number of rows”<<endl;
Cin>>maxRows;
Cout<<“Enter number of columns”<<endl;
Cin>>maxCols;
int array[ maxRows ][ maxCols ] ; // 2-D array
//inserting values in array
for ( int row = 0 ; row < maxRows ; row ++ )
{
for (int col = 0 ; col < maxCols ; col ++ )
{
cout<<"Please enter value element number "<<row<< "," << col<<"\t";
cin >> a [ row ] [ col ] ;
}
}
//printing values from array
for(int i= 0; i<maxRow; i++)
{
for(int j=0; j<max Cols; j++)
{
cout<<a[i][j]<<“\t”;
Prepared by Aqsa Gul, Lecturer Institute of Management Sciences Peshawar
}
}

You might also like