Download presentation
Presentation is loading. Please wait.
Published byDayna Cameron Modified over 9 years ago
1
Course Title Object Oriented Programming with C++ Course instructor ADEEL ANJUM Chapter No: 05 ARRAY 1 BY ADEEL ANJUM (MSc-cs, CCNA,WEB DEVELOPER) 1
2
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
3
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
4
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
5
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
6
Continue……. Arrays are divided into two types. These are 1.One-dimensional array. 2.Multidimensional array. 6
7
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
8
Continue…….. Temp[0]22.3 Temp[1]23.1 Temp[2]19.2 Temp[3]12.3 - Temp[23]20.0 8
9
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
10
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
11
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
12
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
13
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
14
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
15
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
16
program #include 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
17
Program #include 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
18
program #include 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
19
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
20
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
21
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
22
#include 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
23
Program to input data into array of strings #include 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
. 24
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.