Professional Documents
Culture Documents
CSC240 Introduction To Programming - I
CSC240 Introduction To Programming - I
INTRODUCTION TO
PROGRAMMING – I
Mr. Imran yar
Lecturer,
Department of Computer Science,
Jahan University
Kabul, Afghanistan.
Previous Lecture Outline
• Working with repetitive structures
• FOR, WHILE and DO-WHILE loops
Lecture Outline
• Introduction to Arrays
• Declaring and Initializing Arrays
Introduction to Arrays
• An array is a group of consecutive memory locations with same name
and type.
• Ordinary variable can be defined as a name given to single memory
location that can store single specific type of values.
• On the other hand, an array is a collection of different contiguous
memory locations.
Introduction to Arrays
• All these memory locations have one collective name and type. The
values stored in memory locations reserved by an array are known as
the elements of the array. The total number of elements in the array
is called its length or array size.
Introduction to Arrays
• Each element in the array is accessed with reference to its position of
location in the array. This position is called index or subscript. Each
element in the array has a unique index. The index of first element is
0 and the index of last element is length-1.
Introduction to Arrays
• Arrays are used to store a large amount of similar kind of data.
Suppose the user wants to store the marks of 100 students and
declares 100 variables.
• It is time consuming process to use these variables individually. The
process can be simplified by using array. An array of 100 elements can
be declared to store these values instead of 100 individual variables.
Declaring and Initializing Arrays
• It should be kept in mind that an array can be in the form of list,
matrix and etc. an array in which all elements are arranged in the
form of list is called one-dimensional array/single-dimensional or
linear list. A one-dimensional array can be represented in the form of
a single row or a single column.
Declaring and Initializing Arrays
• The term declaration is the process of specifying datatype, array
name and its length. The general form of declaring an array is as
follows:
Declaring and Initializing Arrays
• datatype identifier [array length];
• In the above general form the term
• Datatype describes the datatypes of the values that the array will
store.
• Identifier describes the name of the array.
• Length describes the total number of elements that can be stored in
the array.
Declaring and Initializing Arrays
• According to the above general form let’s declare an array by the
name of marks that stores 5 values.
• int marks[5];
• In the above declaration, an integer type array by the name of marks
of five elements is declared. It allocates five consecutive locations in
memory. The index of first element is 0 and index of last element is 4
(length – 1).
Declaring and Initializing Arrays
• The representation of the above array in memory is as follow:
• Index number of marks array
• Memory locations
Index number of marks array…. 0 1 2 3 4
Memory locations
for(i=0;i<5; i++){
for(j=0;j<4; j++) {
if(arr[j]>arr[j+1]) { // if 0 index greater then index 1
temp=arr[j]; // assgin 0 index into temp variable
arr[j]=arr[j+1]; // assgin index 1 into index 0;
arr[j+1]=temp; // assign temp variable into index 1 ;
}
}
}
Summery
• Introduction to Arrays
• Declaring and Initializing Arrays
Thank You
For your Patience