You are on page 1of 13

ISLAMIC REPUBLIC OF AFGHANISTAN

MINISTRY OF HIGHER EDUCATION


GHALIB UNIVERSITY
COMPUTER SCEINE FACULTY
2TH SEMESTER
Advance Programming Structure
LECTURER : Zabihullah Rahmani

1 Ghalib University
Table of contents
1. Arrays
2. Types of Arrays

2 Ghalib University
Arrays
 An array is a sequence of objects of same data type.

 The objects in array are also called elements of array.

 An array is represented in the computer memory by a consecutive

group of storage locations.

 These locations are referenced by a single variable called array name.

 Each element of an array is referenced by its position in the array.

 The position of an element in an array is represented by an index value

or subscript.

 An element of an array is accessed by its subscript of same type.

 The data is stored in an array. The array is accessed by a single

variable name.
3 Ghalib University
Types of Arrays
 1) One-dimensional arrays

 2) Multi-dimensional arrays

4 Ghalib University
One-Dimensional Array
 One-dimensional array is also called as a list or linear array.

 It consist only one column or one row.

 For example, the temperature of each hours of a day is stored in an


array. The name of the array is “temp” and its elements are
tem[0],temp[1],temp[2],temp[3],…. temp[23]
temp[0] 22.2
temp[1] 23.5
temp[2] 19.7

-
-
-
-
-
temp[23] 15.3
5 Ghalib University
Declaration of One-Dimensional Array
 Defining the name of array, its data type and total number of elements

of an array is called declaring of the array.

 When an array is declared , a memory block with required number of

locations is reserved in the computer memory for storing the data into
elements of the array.

 general syntax for declaring one dimensional array is:

 DataType Array_name[n];

 Where ‘n’ is an unsigned integer value. It represents the total number of

elements of array or called size of array


 To declare a one-dimensional array “temp” of type double with 24
elements, the statement is written as:
double temp[24]
6 Ghalib University
Accessing Data in One-Dimensional Arrays
 Each element of an array is referenced 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 brackets after the array name.

 Thus the first element of an array temp is referenced as temp[0].

 Similarly, the last element of an array temp of n elements is referenced

as temp[n-1].
 To entering data into array you can use either
 cin object or assignments statements.
 e.g cin>>a[1]; or a[1]=12;
 To get or print data of array you can use
cout object or assignments statements.
e.g cout<<a[1]; or int b=a[1];
7 Ghalib University
Example 1
Write a program in C++ to enter integer type data into an array and
then to print the values in reverse order.
#include<iostream>
using namespace std;
int main(){
int abc[5];
for(int i=0;i<=4;i++){
cout<<"Enter value in element "<<i<<endl;
cin>>abc[i];
}for(int i=4;i>=0;i--){
cout<<"Value in a["<<i<<"]"<<abc[i]<<endl;
} return 0;
}
8 Ghalib University
Example 2
Write a program in C++ to input data into an array of 5 elements .
Calculate the sum and average of the elements and then print the sum and average on
the screen.
#include<iostream>
using namespace std;
int main(){
float abc[5],sum,avg;
for(int i=0;i<=4;i++){
cout<<"Enter value in element "<<i<<endl;
cin>>abc[i];
} sum=avg=0.0;
for(int i=4;i>=0;i--){
sum=sum+abc[i];
}avg=sum/5;
cout<<"Sum of array values = "<<sum<<endl;
cout<<"Average of array values = "<<avg;

9 Ghalib University return 0;}


Example 3
Write a program in C++ to find out and print the maximum value in the array.
#include<iostream>
using namespace std;
int main(){
int abc[5], max;
for(int i=0;i<=4;i++){
cout<<"Enter value in element "<<i<<endl;
cin>>abc[i];
} max=abc[0];
for(int i=1;i<=4;i++){
if(max<abc[i])
max=abc[i];
}
cout<<"Maximum value is = "<<max;
return 0;
}

10 Ghalib University
Initializing One-Dimensional Arrays
 Like other variables, the values in the elements of an array can also be assigned when

the array is declared.

 The assigning of values to the elements of the array at the time of its declaration is

called initializing of the array.

 For example, to declare an array “temp” of type double with 5 elements, with values

66.3,77,7,99.2,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.2,63.9,59.3};

 If the number of elements in an array is greater than the values in the list, then the

remaining last elements are initialized to zero.

11 Ghalib University
Example 1
Write a program to initialize the values in an array and the print these values on the
screen.
#include<iostream>
using namespace std;
int main(){

double temp[5]={ 66.2,63.2,69.6,70.2,55.4};


for(int i=0;i<=4;i++){
cout<<temp[i]<<endl;
}
return 0;
}

12 Ghalib University
Any question…?????????????

13 Ghalib University

You might also like