More Related Content
Similar to Notes-10-Array.pdf
Similar to Notes-10-Array.pdf (20)
Notes-10-Array.pdf
- 2. © 2003 Prentice Hall, Inc. All rights reserved.
2
Introduction
• Arrays
– Collection of similar data items
– Static entity (same size throughout program)
- 3. © 2003 Prentice Hall, Inc. All rights reserved.
Array Declaration
• Syntax:
type arrayName[array_size]
Ex. int Ar[10];
• The array elements are (all) same type <type>.
• The size of the array is indicated by array_size, the
number of elements in the array.
• array_size must be an int constant or a constant
expression. Note that an array can have multiple dimensions.
- 4. © 2003 Prentice Hall, Inc. All rights reserved.
Array Declaration
// array of 10 uninitialized ints
int Ar[10];
-- -- --
--
Ar -- -- --
-- -- --
4 5 6
3
0 2 8 9
7
1
- 5. © 2003 Prentice Hall, Inc. All rights reserved.
5
• When declaring arrays, specify
– Name
– Type of array
• Any data type
– Number of elements
– type arrayName[ arraySize ];
int c[ 10 ]; // array of 10 integers
float d[ 3284 ]; // array of 3284 floats
• Declaring multiple arrays of same type
– Use comma separated list, like regular variables
int b[ 100 ], x[ 27 ];
- 6. © 2003 Prentice Hall, Inc. All rights reserved.
Subscripting
• Declare an array of 10 integers:
int Ar[10]; // array of 10 ints
• To access an individual element we must apply a subscript
to array named Ar.
– A subscript is a bracketed expression.
• The expression in the brackets is known as the index.
– First element of array has index 0.
Ar[0]
– Second element of array has index 1, and so on.
Ar[1], Ar[2], Ar[3],…
– Last element has an index one less than the size of the array.
Ar[9]
• Incorrect indexing is a common error.
- 7. © 2003 Prentice Hall, Inc. All rights reserved.
7
• Array
– Consecutive group of memory locations
– Same name and type (int, char, etc.)
• To refer to an element
– Specify array name and position number (index)
– Syntax: arrayname[ Index]
– First element at position 0 (elements start from index 0)
• N-element array c
c[ 0 ], c[ 1 ] … c[ n - 1 ]
– Nth element as position N-1
- 8. © 2003 Prentice Hall, Inc. All rights reserved.
Subscripting
// array of 10 uninitialized ints
int Ar[10];
Ar[3] = 1;
int x = Ar[3];
-- -- 1
--
Ar -- -- --
-- -- --
4 5 6
3
0 2 8 9
7
1
Ar[4]Ar[5]Ar[6]
Ar[3]
Ar[0] Ar[2] Ar[8]Ar[9]
Ar[7]
Ar[1]
1
-- -- --
--
--
- 9. © 2003 Prentice Hall, Inc. All rights reserved.
9
• Array elements like other variables
– Assignment, printing for an integer array c
c[ 0 ] = 3;
cout << c[ 0 ];
• Can perform operations inside subscript
c[ 5 – 2 ] same as c[3]
- 10. © 2003 Prentice Hall, Inc. All rights reserved.
10
c[6]
-45
6
0
72
1543
-89
0
62
-3
1
6453
78
Name of array (Note that all
elements of this array have the
same name, c)
c[0]
c[1]
c[2]
c[3]
c[11]
c[10]
c[9]
c[8]
c[7]
c[5]
c[4]
Position number of the element
within array c
- 11. © 2003 Prentice Hall, Inc. All rights reserved.
11
Examples Using Arrays
• Initializing arrays
– For loop
• Set each element
– Initializer list
• Specify each element when array declared
int n[ 5 ] = { 1, 2, 3, 4, 5 };
– To set every element to same value
int n[ 5 ] = { 0 };
// int n [5] = {1} ?????
– If array size omitted, initializers determine size
int n[] = { 1, 2, 3, 4, 5 };
• 5 initializers, therefore 5 element array
- 12. © 2003 Prentice Hall, Inc.
All rights reserved.
Outline
#include <stdio.h>
#include <iostream>
using namespace std;
int main ()
{
int n[ 10 ]; // n is an array of 10 integers
// initialize elements (all) of array n to 0
for ( int i = 0; i < 10; i++ )
n[ i ] = 0; // set element at location i to 0
cout<<"Element Value";
for ( int j = 0; j < 10; j++ )
cout<<"n"<< n [j];
}
- 13. © 2003 Prentice Hall, Inc.
All rights reserved.
Outline
13
Element values
0
0
0
0
0
0
0
0
0
0
- 14. © 2003 Prentice Hall, Inc.
All rights reserved.
Outline
14
#include <iostream>using
namespace std;
int main ()
{
int n[ 10 ] = {32, 27, 64, 18, 95, 14, 90, 70, 60, 37 };
cout<<"Element" ;
for ( int i = 0; i < 10; i++ )
cout<<"n"<<n[i];
}
- 15. © 2003 Prentice Hall, Inc.
All rights reserved.
Outline
15
Element
32
27
64
18
95
14
90
70
60
37
- 16. © 2003 Prentice Hall, Inc.
All rights reserved.
Outline
16
#include <iostream>using
namespace std;
int main ()
{
int n[ 10 ] = {32, 27, 64, 18, 95 };
cout<<"Element" ;
for ( int i = 0; i < 10; i++ )
cout<<"n"<<n[i];
}
- 17. © 2003 Prentice Hall, Inc.
All rights reserved.
Outline
17
Element
32
27
64
18
95
0
0
0
0
0
- 18. © 2003 Prentice Hall, Inc.
All rights reserved.
Outline
18
#include <iostream>using
namespace std;
int main ()
{
int n[ 10 ] = {32 };
cout<<"Element" ;
for ( int i = 0; i < 10; i++ )
cout<<"n"<<n[i];
}
- 19. © 2003 Prentice Hall, Inc.
All rights reserved.
Outline
19
Element
32
0
0
0
0
0
0
0
0
0
- 20. © 2003 Prentice Hall, Inc.
All rights reserved.
Outline
20
#include <iostream>using
namespace std;
int main ()
{
int n[ 10 ] = {0 };
cout<<"Element" ;
for ( int i = 0; i < 10; i++ )
cout<<"n"<<n[i];
}
- 21. © 2003 Prentice Hall, Inc.
All rights reserved.
Outline
21
Element
0
0
0
0
0
0
0
0
0
0
- 22. © 2003 Prentice Hall, Inc. All rights reserved.
Program to print negative elements in array
#include <iostream>
using namespace std;
int main ()
{
int n[ 10 ] = { 32, -27, 64, 18, -10,78,-56,100,-1 };
cout<<"Negative Elements are" ;
for ( int i = 0; i < 10; i++ )
if (n[i]<0)
{ cout<<"n"<<n[i]; }
}
22
- 23. © 2003 Prentice Hall, Inc. All rights reserved.
O/P
23
Negative Elements are
-27
-10
-56
-1
- 24. © 2003 Prentice Hall, Inc. All rights reserved.
Program to count even or odd elements in array
#include <iostream>
using namespace std;
int main ()
{ int even=0;
int odd=0;
int n[ 10 ] = { 32, -27, 64, 18, -10,78,-56,100,-1 };
for ( int i = 0; i < 10; i++ )
if(n[i]%2 == 0)
{ even++; }
else
{ odd++; }
cout<<"Number of Even Elements"<<even ;
cout<<"nNumber of odd Elements"<<odd ;
}
24
- 25. © 2003 Prentice Hall, Inc. All rights reserved.
O/P
• Number of Even Elements8
• Number of odd Elements2
25
- 26. © 2003 Prentice Hall, Inc. All rights reserved.
Program to copy all array elements to another array
#include <iostream>
using namespace std;
int main ()
{
int p[10];int
n[ 10 ] = { 32, -27, 64, 18, -10,78,-56,100,-1,76 };
for ( int i = 0; i < 10; i++ )
{p[i]=n[i];}
cout<<"second array elements aren";
for ( int j = 0; j < 10; j++ )
{ cout<<"n"<<p[j];
}
}
26
- 27. © 2003 Prentice Hall, Inc. All rights reserved.
O/P
27
32
-27
64
18
-10
78
-56
100
-1
76