Beruflich Dokumente
Kultur Dokumente
Arrays
Structures of related data items
Static entity - same size throughout
program
A few types
C-like, pointer-based arrays
C++, arrays as objects
int count Enough memory for 1 int
12345
2
Array - Memory Layout
The definition:
int tests[5];
allocates the following memory:
c[0] -45
c[1] 6
c[2] 0
c[3] 72
c[4] 1543
c[5] -89
c[6] 0
c[7] 62
c[8] -3
c[9] 1
c[10] 6453
c[11] 78
int main()
{ int i,array1[7],max;
for (i=0;i<7;i++)
cin>> array1[i] ;
max=array1[0];
for (i=0;i<7;i++)
if (array1[i] > max )
max=array1[i];
cout<< "max number in array1 is="<<max;}
Adding +5 or multiply
array1[i]=2* array1[i]; //Multiply by *2
array1[i]= array1[i] /4; //divided by 4
#include<iostream>
using namespace std;
main()
{ int i,array1[5]={10,15,30,32,21};
for (i=0;i<5;i++)
{ array1[i]= array1[i]+5; // *3
cout<< array1[i]<<"\t";}
}
8.5 Processing Array
Contents
Array elements can be treated as
ordinary variables of the same type as
the array
When using ++, -- operators, dont
confuse the element with the subscript:
tests[i]++; // add 1 to tests[i]
tests[i++]; // increment i, no
// effect on tests
Strings
Can be processed using array name
(entire string at once) or using
subscripts(element at a time):
string city;
cout << "Enter city name: ";
cin'S'>> city;
'a' 'l' 'e' 'm'
18
8.9 Two-Dimensional Arrays
Can define one array for multiple sets of
data
Like a table in a spreadsheet
Use two size declarators in definition:
int exams[4][3];
First declarator is number of rows;
second is number of columns
A dynamic 2D array is basically an array of pointers to arrays. You should initialize it
using a loop:
20
Two-Dimensional Array
Representation
int exams[4][3];
columns
exams[0][0] exams[0][1] exams[0][2]
r exams[1][0] exams[1][1] exams[1][2]
o
w exams[2][0] exams[2][1] exams[2][2]
s
exams[3][0] exams[3][1] exams[3][2]
int main( )
{
int myArray[4][4], index1, index2;
for (index1 = 0; index1 < 4; index1++)
for (index2 = 0; index2 < 4; index2++)
myArray[index1][index2] = index2;
for (index1 = 0; index1 < 4; index1++)
{
for (index2 = 0; index2 < 4; index2++)
cout << myArray[index1][index2] << " ";
cout << endl;}
}
25
Two-Dimensional Arrays
Two-dimensional array (table): consists of both rows and columns of
elements
Example: two-dimensional array of integers
8 16 9 52
3 15 27 6
14 25 2 10
Array declaration: names the array val and reserves storage for it
int val[3][4];
26
Locating array elements (Figure 8.9)
val[1][3] uniquely identifies element in row
1, column 3
Examples using elements of val array:
price = val[2][3];
val[0][0] = 62;
newnum = 4 * (val[1][0] - 5);
sumRow = val[0][0] + val[0][1] + val[0][2]
+ val[0][3];
The last statement adds the elements in row 0
and sum is stored in sumRow
A First Book of C++: 27
From Here To There,
Two-Dimensional Arrays
(continued)
28
Two-Dimensional Arrays
(continued)
Initialization: can be done within
declaration statements (as with single-
dimension arrays)
Example:
int val[3][4] = { {8,16,9,52},
{3,15,27,6},
{14,25,2,10} };
First set of internal braces contains values for
row 0, second set for row 1, and third set for
row 2
Commas in initialization braces are required;
inner braces can be omitted
29
Two-Dimensional Arrays
(continued)
33
#include <iostream> // matrix multiplication
using namespace std;
int main()
{ int m, n, c, d, MatA[100][100], MatB[100][100], Result[100][100];
cout<<"Enter the number of rows and columns of matrices \n";
cin>>m>>n;
cout<<"Enter the elements of Matrix A\n";
for ( c = 0 ; c < m ; c++ )
{ for ( d = 0 ; d < n ; d++ )
{
cin>>MatA[c][d]; } }
cout<<"Enter the elements of Matrix B\n";
for ( c = 0 ; c < m ; c++ )
{
for ( d = 0 ; d < n ; d++ )
{
cin>>MatB[c][d]; } }
for ( c = 0 ; c < m ; c++ )
{
for ( d = 0 ; d < n ; d++ )
{
Result[c][d] = MatA[c][d] + MatB[c][d]; } }
34
#include <iostream>
using namespace std;
int main()
{ int m, n, p, q, c, d, k, sum = 0;
int first[10][10], second[10][10], multiply[10][10];
cout<<"Enter the number of rows and columns of first matrix\n";
cin>>m>>n;
cout<<"Enter the elements of first matrix\n";
for ( c = 0 ; c < m ; c++ )
for ( d = 0 ; d < n ; d++ )
cin>>first[c][d];
cout<<"Enter the number of rows and columns of second matrix\n";
cin>>p>>q;
if ( n != p )
cout<<"Invalid Matrix Columns or Rows.\n";
else {
cout<<"Enter the elements of second matrix\n";
for ( c = 0 ; c < p ; c++ )
for ( d = 0 ; d < q ; d++ )
cin>>second[c][d];
for ( c = 0 ; c < m ; c++ )
{ for ( d = 0 ; d < q ; d++ )
{ for ( k = 0 ; k < p ; k++ )
{
sum = sum + first[c][k]*second[k][d];
}
multiply[c][d] = sum;
sum = 0;
}
}
cout<<"Resultant Matrix is: \n";
35
#include<iostream.h> // matrix multiplication
#include<conio.h>
main()
{ //declare variable type int
int a[2][2],b[2][2],i,j,k,s;
//Input the numbers of first matix
cout<<"First Matrix"<<endl;
for(i=0;i<2;i++)
{ for(j=0;j<2;j++)
{ cout<<"Enter number :";
cin>>a[i][j]; } }
//Input the numbers of second matix
cout<<"Second Matrix"<<endl;
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
cout<<"Enter number :";
cin>>b[i][j];
} }
//display the multipication of matrices
cout<<"Multiplication is"<<endl;
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
for(k=0;k<2;k++)
s=s+a[i][k]*b[k][j];
cout<<s<<"\t";
s=0;
}
cout<<endl;
}
getch();
}
36
#include <iostream>
using namespace std;
int main( )
{
const int arraysize =12;
int a[arraysize] = {1, 3, 5, 4, 7, 2, 99, 16, 45, 67, 89,
45};
int total = 0;
for (int i= 0; i<arraysize ; i++)
total += a[i];
cout <<" total of array element values is "<< total <<
endl;
return 0;
}
Chapter 8 slide 37
Multi dimensional array
Chapter 8 slide 38
Printing matrix
Chapter 8 slide 39
Adding two matrix
Chapter 8 slide 40
Chapter 8 slide 41
Array problem solving in C++
Dr Ahmed Telba
7.10 Two-dimensional Arrays
43
Multidimensional Array Initialisation
You can initialise a multidimensional array in more than one way. Consider this examples to
initialise two dimensional array.
44
Initialization of three dimensional array
int test[2][3][4] = {3, 4, 2, 3, 0, -3, 9, 11, 23, 12, 23,
2, 13, 4, 56, 3, 5, 9, 3, 5, 5, 1, 4, 9};
Better way to initialize this array with same elements as above.
int test[2][3][4] = {
{ {3, 4, 2, 3}, {0, -3, 9, 11}, {23, 12, 23, 2} },
{ {13, 4, 56, 3}, {5, 9, 3, 5}, {3, 1, 4, 9} }
};
45
Better way to initialize this array with
same elements as above.
int test[2][3][4] = {
{ {3, 4, 2, 3}, {0, -3, 9, 11},
{23, 12, 23, 2} },
{ {13, 4, 56, 3}, {5, 9, 3, 5},
{3, 1, 4, 9} }
};
46
Example
#include <iostream>
using namespace std;
int main() {
int test[3][2] = { {2, -5}, {4, 0}, {9, 1} };
for(int i = 0; i < 3; ++i) {
for(int j = 0; j < 2; ++j) {
cout<< "test["<< i << "][" << j << "] = " << test[i][j]<<endl;
}
}
return 0;
}
47
#include <iostream>// three dimension array
using namespace std;
int main() {
int test[2][3][2]; // this array can store 12 elements
cout<<"Enter 12 values: \n";
for(int i = 0; i < 2; ++i) {
for (int j = 0; j < 3; ++j) {
for(int k = 0; k < 2; ++k ) {
cin>>test[i][j][k];
}
}
}
cout<<"\nDisplaying Value stored:"<<endl;
/* Displaying the values with proper index. */
for(int i = 0; i < 2; ++i) {
for (int j = 0; j < 3; ++j) {
for(int k = 0; k < 2; ++k ) {
cout<< "test["<<i<<"]["<<j<<"]["<<k<<"] = "<< test[i][j][k]<<endl;
}
}
}
return 0;
} 48
C++ Program to Add Two Matrix Using Multi-dimensional Arrays
#include <iostream>
using namespace std;
int main(){
int r,c,a[100][100],b[100][100],sum[100][100],i,j;
cout << "Enter number of rows (between 1 and 100): ";
cin >> r;
cout << "Enter number of columns (between 1 and 100): ";
cin >> c;
cout << endl << "Enter elements of 1st matrix: " << endl;
/* Storing elements of first matrix entered by user. */
for(i=0;i<r;++i)
for(j=0;j<c;++j)
{
cout << "Enter element a" << i+1 << j+1 << " : ";
cin >> a[i][j]; }
/* Storing elements of second matrix entered by user. */
cout << endl << "Enter elements of 2nd matrix: " << endl;
for(i=0;i<r;++i)
for(j=0;j<c;++j)
{
cout << "Enter element b" << i+1 << j+1 << " : ";
cin >> b[i][j]; }
/*Adding Two matrices */
for(i=0;i<r;++i)
for(j=0;j<c;++j)
sum[i][j]=a[i][j]+b[i][j];
/* Displaying the resultant sum matrix. */
cout << endl << "Sum of two matrix is: " << endl;
for(i=0;i<r;++i)
for(j=0;j<c;++j)
{
cout << sum[i][j] << " ";
if(j==c-1)
cout << endl;
}
return 0; }
49
//C++ Program to Find Largest Element of an Array
#include <iostream>
using namespace std;
int main(){
int i,n;
float arr[100];
cout << "Enter total number of elements: ";
cin >> n;
cout << endl;
while (n>100 || n<=0)
{
cout << "Error! number should in range of (1 to 100)." << endl;
cout << "Enter the number again: ";
cin >> n;
}
for(i=0;i<n;++i) /* Stores number entered by user. */
{
cout << "Enter Number " << i+1 << " : ";
cin >> arr[i];
}
for(i=1;i<n;++i) /* Loop to store largest number to arr[0] */
{
if(arr[0]<arr[i]) /* Change < to > if you want to find smallest element*/
arr[0]=arr[i];
}
cout << "Largest element = " << arr[0];
return 0;
}
50
#include <iostream>//Source Code to Find Transpose of a Matrix Colom change to row and row change Colom
using namespace std;
int main()
{
int a[10][10], trans[10][10], r, c, i, j;
cout << "Enter rows and columns of matrix: ";
cin >> r >> c;
/* Storing element of matrix entered by user in array a[][]. */
cout << endl << "Enter elements of matrix: " << endl;
for(i=0; i<r; ++i)
for(j=0; j<c; ++j)
{ cout << "Enter elements a" << i+1 << j+1 << ": ";
cin >> a[i][j]; }
/* Displaying the matrix a[][] */
cout << endl << "Entered Matrix: " << endl;
for(i=0; i<r; ++i)
for(j=0; j<c; ++j)
{ cout << " " << a[i][j];
if(j==c-1)
cout << endl << endl; }
/* Finding transpose of matrix a[][] and storing it in array trans[][]. */
for(i=0; i<r; ++i)
for(j=0; j<c; ++j)
{
trans[j][i]=a[i][j]; }
/* Displaying the transpose,i.e, Displaying array trans[][]. */
cout << endl << "Transpose of Matrix: " << endl;
for(i=0; i<c; ++i)
for(j=0; j<r; ++j)
{ cout << " " << trans[i][j];
if(j==r-1)
cout << endl << endl;
}
return 0;
}
51
#include<iostream>// matrix addation
using namespace std;
int main() {
int a[10][10]; int b[10][10];
int x,y,i,j;
cout<<"\nEnter the number of rows and columns :::\n\n";
cin>>x>>y;
cout<<"\n\nEnter elements for Matrix A :::\n\n";
for(i=0;i<x;i++) {
for(j=0;j<y;j++) {
cin>>a[i][j]; }
cout<<"\n"; }
cout<<"\n\nEnter elements for Matrix B :::\n\n";
for(i=0;i<x;i++) {
for(j=0;j<y;j++) {
cin>>b[i][j]; }
cout<<"\n"; }
cout<<"\n\nMatrix A :\n\n";
for(i=0;i<x;i++) {
}
52
//Counted matrix add
for(j=0;j<y;j++) {
cout<<"\t"<<a[i][j]; }
cout<<"\n\n"; }
cout<<"\n\nMatrix B :\n\n";
for(i=0;i<x;i++) {
for(j=0;j<y;j++) {
cout<<"\t"<<b[i][j]; }
cout<<"\n\n"; }
for(i=0;i<x;i++) {
for(j=0;j<y;j++) {
cout<<"\t"<<a[i][j]+b[i][j]; }
cout<<"\n\n"; }
53
#include <iostream>
using namespace std;
int main()
{
int i1=9,i2=10,i3=11,i4=12,i5=13;
double ava=i1+i2+i3+i4+i5;
ava=ava/5;
cout << "avarage= "<< ava<<endl;
return 0;
}
#include<iostream>// cin num& print it
using namespace std;
int main()
{
int a[10],i;
for(i=0;i<=9;i++)
cin>>a[i];
for(i=0;i<=9;i++)
cout<<a[i]<<" ";
}
#include<iostream> // change firstA[0] to third{2]
using namespace std;
int main()
{
int A[4],M=0,i;
for(i=0;i<=3;i++)
cin>>A[i];
{
M=A[0];
A[0]=A[2];
A[2]=M;}
for(i=0;i<=3;i++)
cout<<A[i]<<" ";}
#include<iostream> // find minimum & maximum of array
using namespace std;
int main()
{
int a[10];
for(int i=0;i<=9;i++)
cin>>a[i];
int max=a[0];
int min=a[0];
for(int i=0;i<=9;i++)
{
if(max<a[i])
max=a[i];
if(min>a[i])
min=a[i];}
cout<<"Max="<<max<<"\n";
cout<<"Min="<<min<<"\n";}
#include<iostream>// sum of array element & avarage
#include<iostream>
using namespace std;
int main()
{
int A[2][2]; int sum=0,i,j,ava;
cout<<"Please enter the elements ofarray\n";
for(i=0;i<=1;i++)
for(j=0;j<=1;j++)
cin>>A[i][j];
for(i=0;i<=1;i++)
for(j=0;j<=1;j++)
sum+=A[i][j];
ava=sum/4;
cout<< "avarage=\n"<<ava<< endl;
cout<<"sum = "<<sum;}
#include<iostream>// change Coolum by row
using namespace std;
int main()
{
int array[3][3],z,i,j;
for(i=0;i<=2;i++)
for(j=0;j<=2;j++)
cin>>array[i][j];
for(i=0;i<=2;i++){
for(j=0;j<=2;j++)
z=array[0][i];
array[0][i]=array[i][1];
array[i][1]=z;}
cout<<"..........\n";
for(i=0;i<=2;i++){
for(j=0;j<=2;j++)
cout<<array[i][j]<<" ";
cout<<endl;}}
#include<iostream>// matrix 3 by 3 change in row
using namespace std;
int main()
{
int p[3][3],m,i,j;
for(i=0;i<=2;i++)
for(j=0;j<=2;j++)
cin>>p[i][j];
for(i=0;i<=2;i++){
for(j=0;j<=2;j++)
m=p[i][0];
p[i][0]=p[i][1];
p[i][1]=m;}
cout<<"..........\n";
for(i=0;i<=2;i++){
for(j=0;j<=2;j++)
cout<<p[i][j]<<" ";
cout<<endl;}}
#include<iostream>// adding two array
using namespace std;
int main()
{
int a[4]={2,4,6,8},i,b[4]={1,3,5,6},c[4];
for(i=0;i<=3;i++)
{c[i]=a[i]+b[i];
//cout<<a[i]<<"\t";
//cout<<b[i]<<" ";
cout<<c[i]<<" ";
}
}
#include<iostream> // look for number in array and yes or NO
using namespace std;
int main()
{ int a[7]={1,3,5,4,6,7,8};
int i,m=0,n;
cout<<"Enter the number :";
cin>>n;
for(i=0;i<=6;i++)
{if(n==a[i])
{m=1;
break;}
else
m=0;}
if(m==1)
cout<< "The number is found. ";
else
cout<<
"The number is not found. ";
}
#include<iostream> // enter array then sort from min to max
using namespace std;
int main()
{
int a[7],i,j,t;
for(i=0;i<7;i++)
{cout<<"Enter a["<<i<<"]= ";
cin>>a[i];}
for(i=0;i<7;i++)
{for( j=0; j<7; j++)
if(a[i]<a[j])
{t=a[i];
a[i]=a[j];
a[j]=t;}}
for(i=0;i<7;i++)
cout<<a[i]<<" ";
}
#include<iostream>// enter array then delete one number
using namespace std;
int main()
{
int a[6],x,i,j,d=5,k;
for(i=0; i<=5;i++)
{cout<<"enter a["<<i<<"]=";
cin>>a[i]; }
cout<<"enter number for delete it: ";
cin>>x;
k=0;
for(i=0; i<=5;i++)
if(x==a[i])
{k=k+1;
for(j=i; j<=5;j++)
a[j]=a[j+1];
d=d-1;}
if(k==0)
cout<<"not found"<<endl;
for(i=0;i<=d;i++)
cout<<a[i]<<" ";
}
#include<iostream>//Reverse the array element
using namespace std;
int main()
{
int a[10],i;
for(i=0;i<=9;i++)
cin>>a[i];
for(i=9;i>=0;i--)
cout<<a[i]<<" ";
}
66
#include<iostream>// sort odd & even in array
using namespace std;
int main()
{
int a[10],i,j,z;
for(i=1; i<=10;i++)
{cout<<"Enter a["<<i<<"]=";
cin>>a[i]; }
for(i=0; i<=9;i++)
for(j=i+1; j<=9;j++)
if(a[i]%2!=0)
{z=a[i];
a[i]=a[j];
a[j]=z; }
for(i=1; i<=10;i++)
cout<<a[i]<<" ";
}
#include<iostream>//enter two Dim matrix & print it
using namespace std;
int main()
{
int a[2][2],i,j;
for(i=0;i<=1;i++)
for(j=0;j<=1;j++)
{
cout<<"Enter a["<<i<<"]["<<j<<"]= ";
cin>>a[i][j];}
for(i=0;i<=1;i++)
{cout<<endl;
for(j=0;j<=1;j++)
cout<<a[i][j] <<"\t";}
}
#include<iostream> // minimum number in matrix
#include<iostream>
using namespace std;
int main()
{
int A[2][2]={5,6,2,8};
int i,j,min=A[0][0];
for(i=0;i<=1;i++)
for(j=0;j<=1;j++)
{cout<<"Enter A["<<i<<"]["<<j<<"]= ";
//cin>>A[i][j]; }
for(i=0;i<=1;i++)
{for(j=0;j<=1;j++)
if(A[i][j]<min)
min=A[i][j];}
cout<<"\n min = " <<min;}
}
#include<iostream> //diameter of matrix
using namespace std;
int main()
{
int a[3][3]={{5,6,2},{8,7,4},{1,3,9}};
int i,j;
for(i=0;i<3;i++)
for(j=0;j<3;j++)
if(i==j)
cout<<a[i][j]<<" ";
}