Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 46

CSC240

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

Memory Address 1012 1014 1016 1018 1020 1022


Declaring and Initializing Arrays
• On the other hand the process of assigning values to the memory
locations of an array or in other words the process of assigning values
to array elements at the time of array declaration is called array
initialization.
Declaring and Initializing Arrays
• The initialization process provides a list of initial values for array
elements.
• The values are separated with commas and enclosed in curly braces.
There must be at least one initial value between curly braces.
• A syntax error occurs if the values in the braces are more than the
length of array. If the number of initial values are less than the array
size, the remaining array elements are initialized to zero.
Declaring and Initializing Arrays
• The general form of initializing an array is as follows:
• datatype identifier [length] = {list_of_values};
• as per above general form, the marks array can be initialized as under:
• int marks[5]={56,76,87,56,87};
• The representation of the above array in memory is as follow after
initialization:
Declaring and Initializing Arrays

Index number of marks array 0 1 2 3 4


Values stored in the array 56 76 87 56 87
Memory address 1012 1014 1016 1018 1020 1022
Declaring and Initializing Arrays
• The above statement declares an integer array marks with five
elements. It also initialize the array with five values given in the
curly braces.
• In this declaration, marks[0] is initialized to 56, marks[1] is
initialized to 76 and so on.
Declaring and Initializing Arrays
• The total size of an array depends on the total elements like the
size of above array is 10 bytes because each location is of type
integer, and a single integer reserves 2 bytes of memory so 5
integers reserves 10 bytes of memory.
• An array size can be omitted when it is initialized in the declaration
as follow:
• int age[] = {23, 56, 87, 92, 38, 23, 56,55};
Declaring and Initializing Arrays
• The compiler determines the size of the age array according to the
initialized values on the right side. The size of array in above
example is 8.
• It should be kept in mind that if there are fewer initializers than
elements in the array, the remaining elements are initialized to
zero. For example, the elements of the array n could have been
initialized to zero as follows:
• int n[10] = {0};
Declaring and Initializing Arrays
• This explicitly initializes the first element to zero and initializes the
remaining nine elements to zero because there are fewer
initializers than there are elements in the array.
• It’s important to remember that arrays are not automatically
initialized to zero. You must at least initialize the first element to
zero for the remaining elements to be automatically zeroed.
• This method of initializing the array elements to 0 is performed at
compile time for static arrays and at runtime for automatic arrays.
The key points in the above topics are:
• All the values in initializer list must have the same type as that of
array.
• If there are less number of values in the initializer list than
elements in array. The remaining elements are automatically
initialized to zero.
• If there are larger numbers of values in the initializer list than the
elements in array, there will be a syntax error.
The key points in the above topics are:
• Without an initializer list, the array elements will contain
junk(unwanted items) values.
• If you are declaring an array with an initializer list, the size of array
can be omitted.
• The number of elements in the array will then be the same as the
number of initializing values i.e., int a[ ] ={1,2,3}; -and- int a[3]
={1,2,3}; are equivalent.
The key points in the above topics are:
• Since in C++, arrays cannot have zero size so the initializer list must
always contain one initializing value if you omit the array size.
• To initialize whole of array to zero, following statements can be
used.
• dataType arrayName[size] = {0};
• or dataType arrayName[size] = { };
• Both these statements are equivalent, however, first should be
preferred.
The key points in the above topics are:
• An array can also be initialized after the declaration with the help of
an assignment operator. The example demonstrates the initializing
method after the array declaration.
• int myArray[5]; //declaration
• myArray[0] = 0; // initialization
• myArray[1] = 2;
• myArray[2] = 4;
• myArray[3] = 6;
• myArray[4] = 8;
Accessing the Elements of an Array
• To access a particular location or element in the array, you specify
the name of array and the position number (index) of the
particular element in the array. The general form to access an
element is:
• arrayName [index];
• The index of the first element is always 0, whereas index of the last
element is equivalent to size-1 for a particular array. Note that
index must be an integer.
Accessing the Elements of an Array
Accessing the Elements of an Array
• You can also use loops to perform the operation of traversing in an
array or in other words to access the elements of an array. The
loop uses counter variable for taking care of index number. The
program below demonstrates the use of accessing the elements
through loops.
Accessing the Elements of an Array using
loop
Accessing the Elements of an Array
• in the program above, when the loop executes and the statement
inside the loop executes. Inside the statement the variable written
in the curly braces of array name is automatically converted to the
value and as you know that this is a counter variable and changes
its value every time the loop executes.
• It is to be kept in mind that the loop must be continued to specific
number of times means if there are 10 array element so the loop
should be repeated for 10 number of times.
Perform I/O operation on arrays
• The process of input and output with arrays is similar to the input
and output with simple variables.
• The cin object is used to input values in the arrays.
• The cout object is used to display values of arrays.
• However, the use of these objects with each individual elements
becomes very time consuming because each elements is treated
as simple variable.
Perform I/O operation on arrays
• Loops are frequently used to input and output data in an array.
The use of loops with array makes the process of input and output
faster and easier and also reduces the code.
Perform I/O operation on arrays
Perform arithmetic operation on arrays
Search Methods
• Searching is a process of finding the required data in the array.
Searching becomes more important when the length of the array is
very large.
Linear Search Methods
• Linear search is a simple way to search an array for the desired
value. The linear search compares each element of the array with the
search key.
• The value to be found may be the first as well as the last one to be
compared. It is supposed that on the average, a program must
compare the search key with half of the elements of the array.
Linear Search Methods
• The linear search works well for small and unsorted arrays. For larger
arrays, this technique is inefficient. It follows the following steps to
search a value in array.
• Visit the first element of the array and compare its value with the
required value.
• If the value of array matches with the desired value, the search is
completed.
• If the value of array does not match, move to next element and
repeat same process.
Linear Search Methods
• Loops are frequently used to visit elements of array for searching a
value. You can start the counter variable of loop from 0 and move it to
last index of array.
• For example if the array to be searched has 10 elements, the loop will
start the counter variable from 0 and move to 9.
Linear Search Methods
Sorting Arrays
• Sorting is a process of arranging the values of array in a particular
order is called sorting. Numeric type data may be arranged either in
ascending or in descending order.
• Similarly character type data may be arranged in alphabetical order.
• There are number of sorting techniques, here you are going to get
familiar with bubble sort.
Bubble sort
• Bubble sort is also known as exchange sort. It repeatedly visits the
array and compares two items at a time.
• It swaps these two items if they are in the wrong order. It continues to
visit the array until no swaps are needed that means the array is
sorted. It works as follows:
Bubble sort
• Compare adjacent elements. If the first is greater than the second,
swap them.
• Repeat this for each pair of adjacent elements, starting with the first
two and ending with the last two. At this point the last element
should be the greatest.
• Repeat the steps for all elements except the last one.
• Keep repeating for one fewer element each time until there are no
pairs to compare.
Bubble sort
Bubble sort
for(i=0;i<5; i++){

main () for(j=0;j<4; j++) {

{ if(arr[j]>arr[j+1]) { // if 0 index greater then index 1

int arr[5], i, j, temp; temp=arr[j]; // assgin 0 index into temp variable

for (i=0;i<5;i++){ arr[j]=arr[j+1]; // assgin index 1 into index 0;

cout<<"enter value:"; arr[j+1]=temp; // assign temp variable into index 1 ;


}
cin>>arr[i];
}
}
} cout<<" \n the sorted array :\n ";
cout<<" the original values in array :\n";
for(i=0; i<5; i++)
for(i=0;i<5; i++)
cout<<arr[i] <<" ";
cout<<arr[i]<<" ";
}
Bubble sort

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

You might also like