0% found this document useful (0 votes)
18 views21 pages

7-Arrays

Uploaded by

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

7-Arrays

Uploaded by

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

Array - One Dimensional Array

A one dimensional array is a


list of related variables have
the same data type and the
same name .
 Represented as a group of
consecutive memory locations .
 To refer to a particular location
or element in the array, we
specify the name of the array
and the position number of the
particular element in the array.
Defining Arrays
2

 When defining arrays, specify :

1. Array Name .
2. Type of array .
3. size of the arrays .
Defining Arrays
3

 The general form :


Type array_Name [ size ];

 Type declares the base type of the array that


determines the data type of each element in the
array .
 Size defines how many elements the array will
hold. The Size must be an integer constant greater
than zero.
Defining Arrays - Examples-
4

 int A[10];
• An array of ten integers .

 Char str[20];
• An array of twenty characters .

 int a[ 100 ], b[ 27 ] ;
• Defining multiple arrays of same type .
Defining Arrays - Examples-
5

 The Size must be an integer constant greater than


zero.
Defining and Initializing an Array
6

 Define an array temperature of 5 elements contains


float numbers , array size 5 :

double temperature [5];

 Initialize it with these numbers :


12.3 , 7.5 , 65 , 72.1, 87.5 .

double temperature [5] = {12.3 , 7.5 , 65 , 72.1, 87.5 } ;


Defining and Initializing an Array
7

 double temperature [5] = {12.3 , 7.5 , 65 , 72.1, 87.5 };

temperature [0] 12.3

temperature [1] 7.5

temperature [2] 65.0 Elements

temperature [3] 72.1

temperature [ 4 ] 87.5

Index
Initializing Arrays
8

 int N[ ] = { 1, 2, 3, 4, 5 };
 If size omitted, the size is determined from the 5 initializers .
 int N[5] = { 0 } ;
 int B[20] = {2, 4, 8, 16, 32};
 Unspecified elements are guaranteed to be zero .
 If not enough initializers, rightmost elements become 0 .
 int C[4] = {2, 4, 8, 16, 32};
 Error — compiler detects too many initial values .
 C++ arrays have no bounds checking .
 int D[5] = {2*n, 4*n, 8*n, 16*n, 32*n};
 Automatically only ; array initialized to expressions .
Accessing Array Elements
9

 An individual element within array is accessed by use


of a subscript (index) that describes the position of an
element in the array , it must be an integer or integer
expression .
 Array named c of size n:
 c[0],c[1],...,c[ n–1 ]
 int A[10];
A[0], A[1], …, A[9]

 In C++ , all array have zero as the index of first


element .
Get values into array elements
10

1. Initialize arrays with the variable declaration .


double temperature [ 5 ] = { 12.3 , 7.5 , 65 , 72.1 , 87.5 } ;

2. Use Assignment statements .

3. Read input values into the array from the


keyboard.
Accessing Array Elements
11

 Array elements are like normal variables :

temperature [3] = 12.7 ;


cin >> temperature [3] ;

or
N = 3;
temperature [N] = 12.7;

 temperature [5-2] == temperature [3] == temperature [N]


Assigning Values
12

 You cannot assign one array to another :


int a[10] , b[10] ;
a=b ; //error … illegal
 Instead, we must assign each value individually .

 We can use them as values in assignment statements:


char str[ ] =“Hello amal”;
str[6] = ‘A’;
Examples
13

 For example, if we assume that variable a is equal


to 5 and that variable b is equal to 6, then the
following statement adds 2 to array element c[11].

 To print the sum of the values contained in the first


three elements of array c, we’d write

 To divide the value of c[6] by 2 and assign the


result to the variable x, we would write
Manipulating Arrays
14

 For loops are used to:


 Initializingarray elements .
 Reading elements .

 Printing elements .

 Performing operations :
 Sum elements .
 Find largest/ smallest element .
 Search for an element .
 Sort elements .
Manipulating Arrays
15

Initializing Array Elements :

 To access all elements of an array a for loop is


used. It will start from index 0 to the last element in
the array which has an index of array size-1.

 Define and initialize array a of size 10 with zeros


int a[10],index;
for(index =0; index <= 9; index++)
a[index] = 0;
Manipulating Arrays
16

 Reading Elements :

for (index =0; index <= size-1; index++ )


{
cout << “Enter value : ”;
cin >> a[index];
}
 Printing Elements :

for (index =0; index <= size-1; index++)


cout << a[index];

 Sum Array elements Elements :

for (sum = 0, j=0; j < size; j++)


sum += a[j];
Examples
17

 All of the following are valid:


• score[0] = 4 ;
• score[0] += 7 ;
• score[0] = x + y ;
• x = y – score[0] ;
• score[2] = score[1] + 5 * score[0] ;
• score[ j ] = score[ j + 1] ;

 Note: index can be any integral expression.


Example –sum of elements
18
1 /* Fig. 6.4: fig06_04.c
2 Initializing an array with an initializer list */
3 #include <iostream >
4 using namespace std;
5 /* function main begins program execution */
6 int main()
7 {
8 /* use initializer list to initialize array n */
9 int n[ 10 ] = { 32, 27, 64, 18, 95, 14, 90, 70, 60, 37 };
10 int i; /* counter */
11
12 cout<< "Element Value"<< end1
Element Value
13 0 32
14 /* output contents of array in tabular format */ 1 27
15 for ( i = 0; i < 10; i++ ) { 2 64
16 cout<< i<< "\t"<< n[ i ]<< endl ; 3 18
17 } /* end for */ 4 95
18 5 14
19 return 0; /* indicates successful termination */ 6 90
20 7 70
8 60
21 } /* end main */
19 9 37
1 /* Fig. 6.3: fig06_03.c
2 initializing an array */
3 #include <iostream >
4 using namespace std;
5 /* function main begins program execution */
6 int main()
7 {
8 int n[ 10 ]; /* n is an array of 10 integers */
9 int i; /* counter */
10
11 /* initialize elements of array n to 0 */
12 for ( i = 0; i < 10; i++ ) {
13 n[ i ] = 0; /* set element at location i to 0 */
14 } /* end for */ Element Value
15 0 0
16 cout<<"Element Value" << end1 ; 1 0
17 2 0
18 /* output contents of array n in tabular format */ 3 0
19 for ( i = 0; i < 10; i++ ) { 4 0
20 cout<< i<<"\t"<<n[ i ]<<endl ; 5 0
21 } /* end for */ 6 0
22 7 0
23 return 0; /* indicates successful termination */ 8 0
24 9 0
2520} /* end main */
1 /* Fig. 6.5: fig06_05.c
2 Initialize the elements of array s to the even integers from 2 to 20
*/
3 #include <iostream >
4 #define SIZE 10 /* maximum size of array */
5
6 /* function main begins program execution */
7 int main( void )
8 {
9 /* symbolic constant SIZE can be used to specify array size */
10 int s[ SIZE ]; /* array s has SIZE elements */
11 int j; /* counter */
12
13 for ( j = 0; j < SIZE; j++ ) { /* set the values */
14 s[ j ] = 2 + 2 * j;
15 } /* end for */
16
17 cout <<"Element Value" << end1 ;
18
19 /* output contents of array s in tabular format */
20 for ( j = 0; j < SIZE; j++ ) {
20 cout<< i<<"\t"<<n[ i ]<<endl ;
22 } /* end for */
23
24
21 return 0; /* indicates successful termination */
25

You might also like