Uploaded by Alvina Irfan

3. Arrays

advertisement
Lecture 3
Arrays
1
Lecture Outline
 Data type and Data structure
 Data types in C
 Arrays
 Declaration
 1D Array
 Initialization
 Representation
 Operations
 Examples
 Arrays and functions
 2D Arrays
 Declaration, Initialization
 Program
 Lab Task
2
Basic Terms
 Data
 means a value or set of values
 34, 10/01/1965, Pascal, (21,25,28,30,35,37,38)
 Entity
 Is one that has certain attributes and which may be assigned values.
 Entity
 Attribute Name
 Value
Employee
DOB
Sex
Ahmed 31/12/99 M
Designation
Director
 Domain
 Set of all possible values that could be assigned to a particular
attribute
 Information is processed data or meaningful data
3
Data Type and Data Structure
 Data Type
 Defines the specification of a set of data and the characteristics for that data.
 Is derived from the basic nature of data that are stored for processing rather
from their implementation.
 Data Structure
 Refers to the actual implementation of the data type and offers a way of
storing data in an efficient manner.
 Any data structure is designed to organize data to suit a specific purpose so
that it can be accessed and worked in appropriate ways both effectively and
efficiently
 Are implemented using the data types, references and operations on them
that are provided by a programming language.
4
Data Structure
 Is a particular way of storing and organizing data in a computer so that it
can be used efficiently
 Different kinds of data structures are suited to different kinds of
applications and some are highly specialized to specific tasks
 provide a means to manage huge amounts of data efficiently, such as large
databases and internet indexing services
 Usually, efficient data structures are a key to designing efficient
algorithms.
5
Arrays
 Each element in array is accessed with reference to its position of location in
the array. The position is called index or subscript.
 Each element in the array has unique index. The index of the first element is 0
and index of the last element is (length-1).
 The value of the index is written in brackets along with the name of array.
 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 variables individually. The process
can be simplified using array.
 An array of 100 elements can be declared to store these values instead of 100
individual variables.
6
Advantages/Uses of Arrays
Some advantages of arrays are as follows:
 Arrays can store large number of values with single name.
 Arrays are used to process many values easily and quickly.
 The values in arrays can be sorted easily.
 A search process can be applied on arrays easily.
7
One-Dimensional Arrays
 A type of array in which all elements are arranged in the form of
a list is known as one-dimensional array.
 It is also called single-dimensional array or linear list. It consist of
one column and one row.
 The process of specifying array name, length and data type is
called array declaration.
8
Declaring of one-Dimensional Arrays
Syntax:
The syntax of declaring one-dimensional array is as follows:
Data_Type identifier[length]
 Data_Type: It indicates the data types of the values to be sorted in the array.
 Identifier: It indicates the name of the array
 Length: It indicates the total number of elements in the array. It must be a literal
constant or symbolic constant.
9
Declaring of 1-Dimensional Arrays
Example
int marks[5]
The above example declares an integer array marks of five elements. It allocates
five consecutive memory location in memory. The index of first element is 0
and index of last element is 4.
Index of each element
Name of array
0
1
2
3
4
Marks
10
Array Initialization
 The process of assigning values to array elements at the time of array
declaration is called array initialization.
 The initialization process provides a list initial values for array elements.
 The values are separated with commas and enclosed within braces.
There must be at least one initial value between the braces.
 A syntax error occurs if the value in the braces are more than the length of
array. If the number of initial values is less than the array size, the remaining
array elements are initialized to zero.
11
Array initialization
Syntax:
The syntax to initialize array is as follows:
Data_Type identifier[length]={List of value}
 Data_Type: It indicates the data types of the values to be sorted in the array.
 Identifier: It indicates the name of the array
 Length: It indicates the total number of elements in the array. It must be a literal
constant or symbolic constant.
 List of vales: It indicates values to initialize the array. These values must be constant.
12
Array Initialization
Example
int marks[5]={70, 54, 82, 96, 49};
Index of each element
Name of array
Marks
0
1
2
3
70
54
82
96
4
49
 An array initialization can be omitted when it is initialized in the
declaration as follows:
int age[]={23, 56, 87, 92, 44, 12, 15, 8, 3};
 The compiler determine the size of the age array according to the initialized
values on the right side. The size of an array in the above example is 9.
13
Accessing Individual Element of Array
 Each individual element of array is accessed by specifying the
following:
 Name of the array
 Index of element
Syntax:
 The syntax of accessing an individual element in an array as follows:
Array_Name[Index];
 Array_Name: It indicates the name of array.
 Index: It indicates the index of element to be accessed.
14
Accessing Individual Element of Array
Example:
int marks[5];
marks[0]=20;
marks[1]=50;
marks[2]=70;
marks[3]=80;
marks[4]=90;
 The above example declares an array marks of type int with five elements. It
access all elements of the array individually to store different values.
 marks[0]
indicates the first element and marks[4] indicates the last
element of the array.
15
Accessing Individual Element of Array
marks[0]
marks[2]
marks[4]
Index of each element
Name of array
marks
marks[1]
marks[3]
16
Using #define for Array Sizes
#define SIZE 39
#define GRADES 5
int main ( void )
{
int score [ SIZE ] ;
int gradeCount [ GRADES ] ;



}
17
One-Dimensional Arrays
Example: write a program that inputs five integers from the user and stores
them in an array. It then displays all the values in the array without using loops.
#include<iostream.h>
#include<conio.h>
void main()
{
int arr[5];
cout<<"Enter Five Integers:"<<endl;
cin>>arr[0];
cin>>arr[1];
cin>>arr[2];
cin>>arr[3];
cin>>arr[4];
cout<<"The values in array are:"<<endl;
cout<<arr[0];
cout<<arr[1];
cout<<arr[2];
cout<<arr[3];
cout<<arr[4];
}
18
19
One-Dimensional Arrays
Example: Write a program that inputs ten numbers from the user in an array and
displays the maximum number.
#include<iostream.h>
void main()
{
int arr[10], i,max;
for(i=0;i<10;i++)
{
cout<<"Enter "<<i<<" integer:";
cin>>arr[i];
}
max=arr[0];
for(i=0;i<10;i++)
if(max<arr[i])
max=arr[i];
cout<<"Maximum Value: "<<max;
}
20
21
Call (Pass) by Value
 So far, we have passed only values to functions.
 The function has a local variable (a formal parameter) to hold its own
copy of the value passed in.
 When we make changes to this copy, the original (the corresponding
actual parameter) remains unchanged.
 This is known as calling (passing) by value.
22
Passing Arrays to Functions
 The function prototype:
void fillArray ( int ages[ ], int numElements );
 The function definition header:
void fillArray ( int ages[ ], int numElements )
 The function call:
fillArray ( ages, SIZE );
 Notice that we are passing only the name of the array (the address)
and that we aren’t returning anything (the function is void)
because we will be modifying the original array from within the
function.
23
Call (Pass) by Reference
 As demonstrated with arrays, we can pass addresses to functions. This is
known as calling (passing) by reference.
 When the function is passed an address, it can make changes to the
original (the corresponding actual parameter). There is no copy made.
 This is great for arrays, because arrays are usually very large. We really
don’t want to make a copy of an array. It would use too much memory.
24
Two-Dimensional Arrays
 Two dimensional array can be considered as a table that consist of rows and columns.
 Each element in 2-D array is referenced with the help of two indexes. One index is used to
indicate the row and the second index indicates the column of the element.
Syntax:
 The syntax of declaring 2-D array is as follows:
Data_Type identifier [Rows][Cols];
 Data_Type: It indicates the data types of the values to be sorted in the array.
 Identifier: It indicates the name of the array
 Rows: It indicates the number of rows in the array. It must be literal constant or symbolic
constant.
 Cols: It indicates the number of columns in the array. It must be literal constant or
symbolic constant.
25
Two-Dimensional Arrays
Examples:
 The follwing statement declares a 2-D array with four rows and three columns:
int Arr[4][3];
 The statement declares a two-dimensional array.
 The first index indicates array contains four rows. The index of row is 0 and
index of last row is 3.
 The second index indicates the each row in the array contains three columns.
The index of first column is 0 and the index of last column is 2.
 The total number of elements can be determined by multiplying rows and
columns. It means that above array contains twelve elements.
26
Two-Dimensional Arrays
Diagram:
Column Indexes
Rows Indexes
0
1
2
0
0,0
0,1
0,2
1
1,0
1,1
1,2
2
3
2,0
2,1
2,2
3,0
3,1
3,2
A 2-D array and its indexes
27
Accessing Individual Element of 2-D Array
 The array name and the indexes of row and column are used to access
an individual element of 2-D array.
 For example, the following statement will store 100 in the second
column of first row in the array.
Arr[0][1]=100;
 The index for row or column of 2-D array can be given variable. The
following examples will work similar to the line above:
R=0;
C=1;
Arr[R][C]=100;
28
Entering Data in 2-D Array
 You can enter data in any location of array by using the name of array
and index of the element. For example, the following statements enter
data in the first row of a 2-D array:
 Arr[0][0]=10 10 stored in first column of first row
 Arr[0][1]=10 20 stored in second column of first row
 Arr[0][2]=10 30 stored in third column of first row
 Arr[1][0]=10 40 stored in first column of second row
 Arr[1][1]=10 50 stored in second column of second row
 Arr[1][2]=10 60 stored in third column of second row
 The nested loops are frequently used to enter data in two-dimensional array.
The outer loop are normally used to refer to the rows in array. Similarly, the
inner loops are normally used to refer to the columns of the rows.
29
Initializing 2-D Arrays
 Example:
int Arr[3][4]={{12,5,22,84},{95,3,41,59},{77,6,53,62}};
 The above example declares an array of integers with three rows and
four columns.
 The first inner braces contains the value for rows 0.
 The second inner braces contain the values for row 1 and the third
braces contains the values of row 2.
30
Two-Dimensional Arrays
Example: write a program that initializes a two dimensional array of 2 rows and
3 columns and then display its values.
#include<iostream.h>
void main()
{
int i,j,arr[2][3]={15,21,9,84,33,72};
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
cout<<"arr["<<i<<"]["<<j<<"]="<<arr[i][j]<<"\t";
cout<<endl;
}
}
31
32
Merge two Arrays in Third Array
#include<iostream.h>
#include<conio.h>
void main()
{
int a[10],b[10],c[20],i;
clrscr();
cout<<"Enter Elements in 1st Array: ";
for(i=0;i<10;i++)
{
cin>>a[i];
}
cout<<"Enter Elements in 2nd Array: ";
for(i=0;i<10;i++) { cin>>b[i];
}
cout<<"\nElements of Array After Merge: ";
for(i=0;i<10;i++)
33
{
c[i]=a[i];
c[i+10]=b[i];
}
for(i=0;i<20;i++)
{
cout<<c[i];
}
getch();
}
34
35
Lab Tasks
1.
Write code in C++ for Bubble Sort using array
2.
Write C++ code for Binary search of an element from an array
3.
Write Code for Implementing SJF Non Preemptive Algorithm using Arrays.
4.
Write Code for Selection sort using Arrays.
5.
Write a program to find out GCD using arrays.
6.
Write a program to print number greater than previous number?
7.
Write a program to insert a value into an array.
8.
Write a program to delete a value from an array.
36
Download