You are on page 1of 17

1

Fundamentals of Computer
Programming
Arrays (CLO3)

Lecture 06
By
Ms. Rukaiya

Note: Please run slide show to listen this lecture


 2003 Prentice Hall, Inc. All rights reserved.
2

What is Array?
• Array
– group of consecutive memory locations
– Same name and data type.
• A simple variable is a single memory location with
unique name and a type.
• The memory locations in the array are known as
elements of array.
• The total number of elements in the array is called
length of array.
• The elements of an array are accessed with
reference to its position in array, that is called
index or subscript.
 2003 Prentice Hall, Inc. All rights reserved.
3

Array

name

int c[12]

Data type Size/ length

 2003 Prentice Hall, Inc. All rights reserved.


4

Access Array Elements

• To refer to an element, specify


– Array name and position number
• Format: arrayname[ position number ]
– First element at position 0
– n element array c:
c[ 0 ], c[ 1 ]…c[ n - 1 ]
• Array elements are like normal variables
c[ 0 ] = 3;
cout << c[ 0 ];
• Performing operations in subscript. If x = 3,
c[ 5 – 2 ] == c[ 3 ] == c[ x ]

 2003 Prentice Hall, Inc. All rights reserved.


5

Declaring Arrays

• Declaring arrays - specify:


– Name
– Type of array
– Number of elements
– Examples
int c[ 10 ];
float hi[ 3284 ];
• Declaring multiple arrays of same type
– Similar format as other variables
– Example
int a[ 100 ], x[ 27 ];

 2003 Prentice Hall, Inc. All rights reserved.


6

Array Initialization

• Initializers
int n[ 5 ] = { 1, 2, 3, 4, 5 };
– If not enough initializers, rightmost elements become 0
– If too many initializers, a syntax error is generated
int n[ 5 ] = { 0 }
– Sets all the elements to 0
• If size omitted, the initializers determine it
int n[] = { 1, 2, 3, 4, 5 };
– 5 initializers, therefore n is a 5 element array

 2003 Prentice Hall, Inc. All rights reserved.


1 // Fig. 4.4: fig04_04.cpp
2 // Initializing an array with a declaration Outline
3 #include <iostream>
4
5 using namespace std;

11
12 int main()
13 {
14 int n[ 10 ] = { 32, 27, 64, 18, 95, 14, 90, 70, 60, 37 };
15
16 cout << "Element" << "Value" << endl;
17
18 for ( int i = 0; i < 10; i++ )
19 cout << i << n[ i ] << endl;
20
21 return 0;
22 }
Element Value
0 32
1 27
2 64
3 18
4 95
5 14
6 90
7 70
8 60
9 37  2003 Prentice Hall, Inc.
All rights reserved.
1 // Fig. 4.7: fig04_07.cpp
2 // A const object must be initialized Outline
3
4 int main()
5 {
6 const int x; // Error: x must be initialized
7
8 x = 7; // Error: cannot modify a const variable
9
10 return 0;
11 }

Fig04_07.cpp:
Error E2304 Fig04_07.cpp 6: Constant variable 'x' must be
initialized in function main()
Error E2024 Fig04_07.cpp 8: Cannot modify a const object in
function main()
*** 2 errors in Compile ***

 2003 Prentice Hall, Inc.


All rights reserved.
9

Types of Array

• One dimensional
• Two dimensional
• Multi-dimensional

 2003 Prentice Hall, Inc. All rights reserved.


10

One-dimensional Array (one-D)

• All elements are arranged in the form of a list


• Known as 1-D array or single-dimensional or
linear list
• Declaring 1-D array
data_type identifier[length];
e.g. int marks[5];

0 1 2 3 4
int marks

 2003 Prentice Hall, Inc. All rights reserved.


11

One-D Array Initialization

• The process of assigning values to array elements


at the time of array declaration is called array
initialization
data_type identifier[length];
e.g. int marks[5]={70,54,82,96,49}

0 1 2 3 4
int marks 70 54 82 96 49

• Initializing values must be constant

 2003 Prentice Hall, Inc. All rights reserved.


12

Accessing elements of array

• Individual element
Array_name[index];

• Using loop
int marks[5];
for (int i=0;i<=4;i++)
cout << marks[i] < “\n”;

 2003 Prentice Hall, Inc. All rights reserved.


13
Runtime initialization and accessing
elements of array

int main()
{
int age[4];
for(int j=0;j<4;j++){
cout<<“Enter an age”;
cin >> age[j];}
for(j=0;j<4;j++)
cout <<“ you entered” << age[j] <<endl;
return 0;
}

}
 2003 Prentice Hall, Inc. All rights reserved.
14

Example

#define SIZE 10; /* symbolic constant SIZE can be used to specify array size */
int main( ){
int s[SIZE];
int j; /* counter */

for (j=0;j < SIZE; j++){


s[j]=2+2*j;}
cout << "Element"<<"Value" ;

/* output contents of array s in tabular format */

for ( j = 0; j < SIZE; j++ ) {


cout << j<< s[ j ] ;}
return 0; /* indicates successful termination */
} /* end main */

 2003 Prentice Hall, Inc. All rights reserved.


15

Advantages and Uses of Array

• It can store a large number of value with single


name
• Use to process many values easily and quickly
• Values in array can be sorted easily
• The search process can be applied on arrays easily

 2003 Prentice Hall, Inc. All rights reserved.


16

Practice problems

1. Write a program to read numbers from an array and graphs the


information in the form of a bar chart or histogram—each number is
printed, then a bar consisting of that many asterisks is printed beside
the number. Following output should be printed on your screen

 2003 Prentice Hall, Inc. All rights reserved.


17

Practice problems
1. Write a program C++ program to create an array with reverse
elements of one dimensional array
2. Write a program C++ program to find largest element in a one
dimensional array
3. Suppose X. Y, Z are arrays of integers of size M, N, and M + N
respectively. The numbers in array X and Y appear in descending
order. Write a C++ program to produce third array Z by merging
arrays X and Y in descending order
4. Print following triangle using one dimensional array

 2003 Prentice Hall, Inc. All rights reserved.

You might also like