course title object oriented programming with c++ course instructor adeel anjum chapter no: 05 array...

Post on 02-Jan-2016

214 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Course Title Object Oriented Programming with C++

Course instructor ADEEL ANJUM

Chapter No: 05ARRAY

1BY ADEEL ANJUM (MSc-cs, CCNA,WEB DEVELOPER) 1

ARRAYS

• An array is a sequence of objects of same data type. The objects in array are also called elements of array.

• Array is represented in the computer memory by consecutive group of storage location. These location are referred by single variable called array name. Each element of an array is referred by its position in the array.

2

Continue……

• The position of an element in an array is represented by an index value or subscript. in an array with ‘n’ elements, the index value are 0,1,2….n-1.

• where 0 represent the index value of first element and n-1 represent the index value of last value.

3

Conti…..

An element of an array is accessed by its subscript value. The subscript or index value is written inside a pair of square brackets [ ] with the name of the array. An element of the array is referred by specifying name of the array and the index value of the element.

4

Continue….

• Array are used to process a large amount of data of same type. The data is stored in an array. The array is accessed by a single variable name. The index value are used to access individual elements of the array. A few statements are required to process data in an array. Therefore ,the use of arrays in a program reduce size of the program.

5

Continue…….

• Arrays are divided into two types. These are

1. One-dimensional array.

2. Multidimensional array.

6

One dimensional array..

• One dimensional array is also known as a list or a linear array. It consists of only one column or one row.

• For example ,the temperature of each hour of a day is stored in an array.The name of array ia “temp” and its elements are temp[0],temp[1],temp[2] temp[3]…………temp[23].

7

Continue……..

• Temp[0] 22.3

• Temp[1] 23.1

• Temp[2] 19.2

• Temp[3] 12.3

• -

• -

• Temp[23]20.0

8

Declaring one dimensional array

• Like the variable ,an array is also declared. Defining the name of array, its type and total number of elements of an array is called declaring of array.

• when an array is declared ,a memory block with required number of location is reserved in the computer memory for storing the data into elements of the array.

9

Continue…..

• The general syntax to declare a one dimensional array is :

type array-name [n];where “n” is an unsigned integer value. It represents the total number of elements of array.To declare an array of 24 elements the statement is

int temp[24];10

Accessing data in array

• Each element of an array is referred by its index. In an array of n elements ,each element has an index value. the index of the first element is 0 and of the last element is n-1.

• The index value is written within square bracket after the array name.

11

Conti….

Thus the first element of an array temp is referred as temp [0].similarly ,the element of an array temp of n elements is referenced as temp[n-1].

12

Input /output data in arrays

• Input/output statements is used to access data in each element of the array, the statement is written once and loop structure is used to repeat the input/output statement to access data of the elements of the array.

13

Initializing array

• Like the variable the value in the elements of an array can also be assigned when the array is declared. The assigning of value to the elements of the array at the time of its declaration is called initializing of array.

14

Conti…

For example ,to declare an array “temp” of type double with 5 elements with values 66.3,77.7,99.8,63.9 and 59.3 in elements temp[0],temp[1],temp[2],temp[3],temp[4] respectively. The statement is written as:double temp[5]= {66.3,77.7,99.8,63.9 ,59.3 }

15

program#include<iostream.h>#include<conio.h>Void main(){ int a[5];

int i;a[0]=6;a[1]=45;a[2]=33;a[3]=56;a[4]=45;for (i=0;i<=4;i++)cout<<“value in a [i]=“<<a [i];getch();}

16

Program #include<iostream.h>#include<conio.h>Void main(){ int temp[7];

int sum;int days;for (day=0;day<7;day++){ cout<<“Enter temperature for day:”<<endl; cin>>days; }sum=0;for (day=0;day<7;day++)sum+=temp[day];cout<<“average temperature is ”<<sum/7;getch();} 17

program #include<iostream.h>

#include<conio.h>void main(){

clrscr();double temp[5]={66.3,77.7,99.8,63.9 ,59.3 }int i;for (i=0;i<=4;i++)cout<<temp[i];getch();}

18

Two dimensional array

• A two dimensional array consists of column and row . Each element of the two dimensional array is referenced by its index values or subscripts.

• The index value of two dimensional array consists of two subscripts. One subscript represents the row number and the second subscript represents the column number.

19

Declaration of two dimensional array

A two dimensional array is declared by giving two indexed values enclosed in square brackets.

the first value represents the total number of rows and the second represents the total number of columns.

20

conti………

The syntax to declare a two dimensional array.

type array name [r][c];

type: represent data type of array.

array name: represent the name array.

r: represents total number of rows of table.

c: represents the total number of columns.

int abc [12][3];

21

#include<iostream.h>

#include<conio.h>

void main()

{

int xyz[3][4]={{1,2,3,4},{5,6,7,8},{9,10,11,12}};

clrscr();

int r,c;

for ( r=0; r <=2; r++)

{

for ( c=0; c<=3; c++)

{

cout<<xyz[r][c]<<"\t";

}

cout<<endl;

}

getch();

} 22

Program to input data into array of strings#include<iostream.h>#include<conio.h>Void main() { clrscr();

char A[5][5];int c;for (c=0;c<4;c++){ cout<<“Enter string ”<<c+1<<“:”; cin>>A[c];}for(c=0;c<4;c++)cout<<A[c]<<endl;getch(); }

23

.

24

top related