0% found this document useful (0 votes)
43 views23 pages

4 Array

The document provides an overview of arrays in C++, detailing their types, declaration, initialization, and operations. It covers one-dimensional and two-dimensional arrays, including examples of how to declare, access, and manipulate them. Additionally, it includes sample C++ programs demonstrating the use of arrays for various tasks such as reading, summing, and searching elements.

Uploaded by

ae202403428
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views23 pages

4 Array

The document provides an overview of arrays in C++, detailing their types, declaration, initialization, and operations. It covers one-dimensional and two-dimensional arrays, including examples of how to declare, access, and manipulate them. Additionally, it includes sample C++ programs demonstrating the use of arrays for various tasks such as reading, summing, and searching elements.

Uploaded by

ae202403428
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 23

ARRAY

1
Arrays in C++
• Data types are of two kinds:
- simple data types (e.g. int, float, double, char)
- Structured data type: (e.g. arrays)

• An array is a collection of two or more adjacent memory cells, called


array elements, that are associated with a particular symbolic name.

• In C++ each array has: name, data type, size

• Several operations are allowed on the array: Read, Write, Search, Sum,
Min, Max, Sort, etc..

• Arrays are of two kinds:


- Arrays of one-dimension
- Arrays of two-dimension

2
One-Dimensional Arrays
• Declaration of one-dimension array
Syntax: atype aname [ size ] ; // uninitialized array
atype aname [ size ] = { initialization list } ;

where
atype is any data type;
aname is the name given to the array;
size represents the number of elements in the array.
initialization list is the list of initial values given to
the array.
3
Declaration of Arrays
For example ,
int x [ 3 ];
- This tells the compiler to associate 3 memory cells
with name x.
- These cells will be adjacent to each other in memory.
- Each element of array x contains a value of integer
type
• More than one array can be declared on a line
int age [10] , height [10] , names [20] ;

• Mix declaration of variables with declaration of arrays


int i , j , age [10] ;

4
Initializing an Array
Example1:
int Y [ 4 ] = { 2, 4, 6, 8 } ;
This initializes array Y to have 4 elements which contain 2, 4, 6, 8.
2 4 6 8
Example2:
int age [ 10 ] = { 0,0,0,0,0,0,0,0,0,0 } ;
Example3:
int age[ 10 ] = { 0 } ;
0 0 0 0 0 0 0 0 0 0

Example4:
int age [ ] = { 1,2,3,4,5,6,7,8,9,10 } ;
Accessing One-Dimensional Array
int x [3]={24,20,10} ;
Array x in memory:
24 20 10

How to process the data stored in an array?

Syntax:
aname [ index ]
- index is the subscript that is used to reference the
desired element.
The array indexing starts from 0 until the fixed size -1.

6
Accessing One-Dimensional Array

Array x in memory:

Index 0 1 2
Values
24 20 10

Accessing the array:


x [0] to access the first element of x
x [1] to access the second element of x
x [2] to access the third element of x
7
Storage of an array in memory
Name memory

C[0] 24
Example:
int C[10]; C[1] 59
C[0]=24; C[2] 35
C[1]=59;
C[3] ...
C[2]=35;
C[4] ...
C[5] ...

C[6] ...

C[7] ...
C[8] ...
C[9] ...
Index
• Example: Write C++ program that output the array elements.
Where L[5]={1,2,3,4,5};

#include <iostream>
using namespace std;
void main ()
{
int L [5]={1,2,3,4,5};
for( int i=0;i<5;i++)
{
cout<<L[i] ;
}
}

9
• Example: Write C++ program that read array of size 10
integer numbers.

#include <iostream>
using namespace std;
void main ()
{
int L [10];
cout<<"please enter 10 integer number";
for(int i=0;i<10;i++)
{
cin >> L[i] ;
}
}

10
Examples on One-Dimensional Arrays
• Example 1: Write a C++ program that stores the first 5
integers that are multiples of 5 into array A and reads data into
array B; computes the sum of the two arrays and stores the
result in array C.
# include <iostream.h>
void main ( )
{ int A [5] ; //declaring array A of 10 integers
int B [5] , C [5]; //declaring arrays B and C of 10 integers
for ( int i = 0; i <5 ; i++ )
{ A[ i ] = ( i + 1 ) * 5;
cout << “enter new element in array B”;
cin >> B[ i ] ;
C [ i ] = A[ i ] + B [ i ] ;
cout << C [ i ] << “ “ ;
} }
11
Example1..Cont.
The trace of the previous example shows the arrays as
follows if B elements are 7 6 10 13 23:
0 1 2 3 4
A 5 10 15 20 25

0 1 2 3 4
B 7 6 10 13 23

0 1 2 3 4
12 16 25 33 48
C
12
Copying Arrays

• Data types should be identical


• Size should be same
int a [ 10 ] ={7,1,3,5,8,9,1,2,8,4};
int b [ 10 ] ;
for ( i =0 ; i < 10 ; i ++ )
;b[i]=a[i]
Example 2

Example 2:
Write a C++ program to read 10 integers and store them
in array A. Then it finds the even numbers to store them
in array B and the odd numbers to store them in array C.

14
Example 2 .. Cont.
#include <iostream.h>
void main ( )
{ int i;
int A [10], B[10], C[10] ;
cout << “ Enter 10 integers : “ ;
for (i=0 ; i <10; i++)
{ cin >> A[i] ;
if (A[i] % 2 == 0)
B[i] = A[i] ;
else
C[i] = A[i];
}
cout << “B element = “ << “ C element = “ << endl;
for (i=0; i<10; i++)
{ cout << B[i] << “ “ << C[i] << endl ;
}
}
15
Examples on One-Dimensional Arrays

Example 3:
The resultant arrays B and C in example 2 contain data
stored in non consecutive locations in the arrays.
Modify the program of example 2 so that the data are
stored in consecutive locations.

16
#include <iostream.h>
void main ( )
{ int i, j = 0 , k = 0 ;
int A [10], B[10], C[10] ;
for ( i= 0 ; i <10; i++)
{ cin >> A[i] ;
if (A[i] % 2 == 0)
{ B[j]=A[i];
j ++ ; }
else
{ C [k] = A [ i ];
k ++ ;
} }
cout << “B element = “ ;
for (i=0; i<j; i++)
cout << B[i] <<“ “ ;
cout<<endl;
cout << “C element = “ ;
for (i=0; i<k; i++)
cout << C[i] << “ “ ;
}
17
Example 4
The problem:
Write C++ program that searches for an integer in array of 10
integers. A proper message should be printed out.

The Analysis:
A given array of integer numbers is going to be searched in order
to find a given number.

Requirements:
Input: an integer number n, an array A of 10 integers
Output: a message “yes found” or “no not found” according to
whether the number is found or not.

18
The C++ Program
#include <iostream.h>
void main ( )
{
int L [10] ;
int CurElem, SearchVal;
bool Found;
// ------------------------ Reading L ------------------------------
cout<<"Enter 10 integers:";
CurElem = 0 ;
while (CurElem < 10 )
{
cin>> L[CurElem] ;
CurElem = CurElem + 1;
}
//------------------------------ End Reading L ------------------
19
cout<< "Enter the number that you want to search for: ";
cin>>SearchVal;
Found = false;
CurElem = 0;
while (CurElem < 10 && !Found )
{
if (L[CurElem] == SearchVal)
Found = true;
else
CurElem =CurElem + 1;
}
if ( Found == true)
cout<<" Yes, the number is found ";
else
cout<<" No, the number is not found" ;
}
20
Arrays of Two-Dimensions

Syntax ( in C++ ):
atype aname [nrows] [ncolumns] ;

Example:

int B [2] [3] ;

Array in diagram:

21
Arrays of Two-Dimensions

• Reading array of two-dimensions:

Syntax ( in C++ ): After first outer loop Input

]0[ 5 2 9
int B [2] [3] ;
int i , j ;
for ( i = 0 ; i < 2 ; i++ )
for ( j = 0 ; j < 3 ; j++ )
cin >> B[ i ] [ j ] ; Input
After second outer loop
]0[ 5 2 9
]1[ 7 0 4

22
Write C++ program that
reads array C of size (8 x
5) and finds the product
of the elements in each
row.

23

You might also like