0% found this document useful (0 votes)
7 views71 pages

Arrays

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views71 pages

Arrays

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 71

LECTURE – ARRAYS

PROGRAMMING FUNDAMENTALS

MR. IRFAN AHMED


LECTURER
DEPARTMENT OF COMPUTER SCIENCE

Dawood University of Engineering and Technology,


Karachi
CONTENTS

 Arrays
 Types of Arrays
 One-Dimensional Array
 Declaring One-Dimensional Array
 Initializing One-Dimensional Array
 Accessing elements of One-Dimensional Array
 Inputting elements of One-Dimensional Array
 Outputting elements of One-Dimensional Array
 Summing all the elements of One-Dimensional Array
 Averaging all the elements of One-Dimensional Array
2
CONTENTS

 Multi-Dimensional Array
 Two-Dimensional Array
 Declaring Two-Dimensional Array
 Initializing Two-Dimensional Array
 Accessing elements of Two-Dimensional Array
 Inputting elements of Two-Dimensional Array
 Outputting elements of Two-Dimensional Array
 Summing all the elements of Two-Dimensional Array
 Averaging all the elements of Two-Dimensional Array

3
CONTENTS

 Searching array element using Linear Search


 Sorting array element using Bubble Sort
 Program Examples

4
WHAT IS ARRAY

[email protected] 01/22/2025 5
ARRAYS

 An array is the collection of homogeneous (same) data items.


 It is a collection of a fixed number of components all of the same data type.
 An array is a consecutive group of memory locations that all have the same type.
 An array is a data structure that can hold multiple values at a time unlike simple
variables who can only store single value at a time.
 In an array all the elements have one or more indices through which they are
accessed.

6
ARRAYS

In order to create an array we need to define the followings for an array:

 Name
 Data Type
 Size
 Elements

7
ARRAYS

 The name of the array is identifier, which follows all the rules of identifier in C++.
 The data type of array specifies the type of the data every element of its will have.
 The size specifies the total number of elements, the array can store at maximum.
 The elements are the actual data items (values) that array will hold.

8
TYPES OF ARRAYS

 There are two types of arrays:

One-Dimensional Multi-Dimensional
Array Array

9
ONE-DIMENSIONAL ARRAY

 One-dimensional array is just the list of items of same data type.


nums
 All the items are stored in memory consecutively.
50
10
0
-8
73
56
74
48 10
DECLARING ONE-DIMENSIONAL ARRAY

 In order to declare one dimensional array, we need to specify:

Name
Data Type
Size

11
DECLARING ONE-DIMENSIONAL ARRAY

int nums [10] ;


float temps [7] ;
char alphabets [50] ;
double voltage [250] ;
12
INITIALIZING ONE-DIMENSIONAL ARRAY

 In order to initialize one dimensional array, we need to specify:

Name
Data Type
Size
Elements
13
INITIALIZING ONE-DIMENSIONAL ARRAY

int nums [5] = { 50,98,74,82,35};


nums
50
98
74
82
35
14
INITIALIZING ONE-DIMENSIONAL ARRAY
float temps [7] =
{ 37.5,34.4,32.8,30.1,33.8,34.8,33.3};
temps
37.5
34.4
32.8
30.1
33.8
34.8
15

33.3
INITIALIZING ONE-DIMENSIONAL ARRAY

char options [4] = { ’A’ , ’B’ , ’C’ , ’D’ };


options
A
B
C
D
16
ACCESSING ELEMENTS OF ONE-DIMENSIONAL
ARRAY

 In one dimensional array, every element has one index number.


 The index number starts from 0.
 The index of the element is always one less than the element number. For example
the index of 5th item is 4 and the index of 1st item is 0.
 For accessing any item of the array we just specify the array name along with the
element index inside the square brackets.

17
ACCESSING ELEMENTS OF ONE-DIMENSIONAL
ARRAY

nums
0 50
1 98
int nums [5] = { 50,98,74,82,35};
cout<<nums[2] ; // Displays 74 2 74
cout<<nums[4] ; //Displays 35 3 82
4 35

18
INPUTTING ELEMENTS OF ONE-DIMENSIONAL
ARRAY

nums
0
int nums [5];
1
for( int i = 0 ; i<5; i++)
{ 2
cin>>nums [ i ] ; 3
}
4

19
OUTPUTTING ELEMENTS OF ONE-DIMENSIONAL
ARRAY

nums
0 50
int nums [5] = { 50,98,74,82,35};
1 98
for( int i = 0 ; i<5; i++)
{ 2 74
cout<<nums [ i ] << endl; 3 82
}
4 35

20
SUMMING ALL THE ELEMENTS OF ONE-
DIMENSIONAL ARRAY

nums
0 50
int sum = 0 ;
int nums [5] = { 50,98,74,82,35}; 1 98
for( int i = 0 ; i<5; i++) 2 74
{
sum += nums [ i ] ;
3 82
} 4 35
cout<<“Sum = ” << sum ; //Displays 339

21
AVERAGING ALL THE ELEMENTS OF ONE-
DIMENSIONAL ARRAY
const int SIZE = 5 ;
int sum = 0 ;
nums
float average = 0.0 ; 0 50
int nums [5] = { 50,98,74,82,35};
1 98
for( int i = 0 ; i < SIZE ; i++)
{ 2 74
sum += nums [ i ] ; 3 82
}
4 35
average = (float) ( sum / SIZE ) ;
cout<<“Average = ” << average ; //Displays
67.8 22
MULTI-DIMENSIONAL ARRAY

 Multi-dimensional array is an array having more than one dimensions.


 It require more than one index to access the items.
 Every dimension has its own index.
 A two dimensional array is the arrangement of elements in rows and columns. First
index = row, second index = column.
 A three dimensional array is arrangement of data in the form of a cube. First index =
cube face, second index = row, third index = column.

23
TWO-DIMENSIONAL ARRAY

 Two-dimensional array is the arrangement of elements in rows and columns.


 It has two indices, one for row and other for column.

nums
57 74 11
10 14 87
47 48 98
24
DECLARING TWO-DIMENSIONAL ARRAY

 In order to declare two dimensional array, we need to specify:

Name
Data Type
Size for both the dimensions

25
DECLARING TWO-DIMENSIONAL ARRAY

int nums [3] [3] ;


float temps [5] [7];
char alphabets [2] [3] ;

26
INITIALIZING TWO-DIMENSIONAL ARRAY

 In order to initialize two dimensional array, we need to specify:

Name
Data Type
Size for both the dimensions
Elements
27
INITIALIZING TWO-DIMENSIONAL ARRAY

int nums [3] [3] = {{57,74,11},{10,14,87},


{47,48,98}};
nums
57 74 11
10 14 87
47 48 98

28
INITIALIZING TWO-DIMENSIONAL ARRAY

float temps [3] [7] = {{34.5,33.2,38.4,36.4,37.7,34.4,38.8},


{37.4,34.1,34.1,38.1,36.5,30.4,30.8}, {38.1,38.0,30.8,37.4,38.1,33.5,34.8}};

temps
34. 33. 38. 36. 37. 34. 38.
5 2 4 4 7 4 8
37. 34. 34. 38. 36. 30. 30.
4 1 1 1 5 4 8
38. 38. 30. 37. 38. 33. 34.
1 0 8 4 1 5 8 29
INITIALIZING TWO-DIMENSIONAL ARRAY

char alphabets [2] [3] = {{ ‘C’ , ’D’ , ’W’ }, { ‘1’ , ’?’ , ’V’ }};

alphabets
C D W
1 ? V

30
ACCESSING ELEMENTS OF TWO-DIMENSIONAL
ARRAY

 In two dimensional array, every element has two index numbers.


 One for row and other for column. Both the indices start from 0.
 The row index of the element is always one less than the row number and column
index is one less than the column number of that item.
 For accessing any item of the array we just specify the array name along with the row
index and column index inside the square brackets.

31
ACCESSING ELEMENTS OF TWO-DIMENSIONAL
ARRAY

nums
int nums [3] [3] = {{57,74,11},{10,14,87}, {47,48,98}};
cout<<nums [1] [2] ; // Displays 87 0 1 2
cout<<nums [2] [2] ; //Displays 98
0 57 74 1
1
1 10 14 8
7
2 47 48 9
8
32
INPUTTING ELEMENTS OF TWO-DIMENSIONAL
ARRAY

int nums [3] [3] ; nums


for( int i = 0 ; i<3; i++)
0 1 2
{

for( int j = 0 ; j<3; j++) 0


{ 1
cin>>nums [ i ] [ j ] ; 2
}
}
33
OUTPUTTING ELEMENTS OF TWO-DIMENSIONAL
ARRAY
int nums [3] [3] = {{57,74,11},{10,14,87},
{47,48,98}}; nums
for( int i = 0 ; i<3; i++)
0 1 2
{
for( int j = 0 ; j<3; j++) 0 57 74 1
{ 1
cout<<nums [ i ] [ j ] << “ “; 1 10 14 8
} 7
cout<< endl; 2 47 48 9
} 8
34
SUMMING ALL THE ELEMENTS OF TWO-
DIMENSIONAL ARRAY

int sum = 0 ; nums


int nums [3] [3] = {{57,74,11},{10,14,87},
{47,48,98}}; 0 1 2
for( int i = 0 ; i<3; i++)
0 57 74 1
{
for( int j = 0 ; j<3; j++)
1
{ 1 10 14 8
sum += nums [ i ] [ j ] ;
7
}
} 2 47 48 9
8
35
cout<<“Sum = ” << sum ; //Displays 446
AVERAGING ALL THE ELEMENTS OF TWO-
DIMENSIONAL ARRAY
const int ROWS = 3 ;
const int COLS = 3 ;
int sum = 0 ;
float average = 0.0 ; nums
int nums [3] [3] = {{57,74,11},{10,14,87},
{47,48,98}};
0 1 2
for( int i = 0 ; i < 3 ; i++) 0 57 74 1
{
for( int j = 0 ; j < 3 ; j++)
1
{
sum += nums [ i ] [ j ] ;
1 10 14 8
} 7
}

average = (float) ( sum / (ROWS * COLS )) ;


2 47 48 9
8
36
cout<<“Average = ” << average ; //Displays
49.55
SEARCHING ARRAY ELEMENT USING LINEAR
SEARCH

 Linear search also called as sequential search is a method for finding a particular
value in a list, that consists of checking every one of its elements, one at a time and
in sequence, until the desired one is found.

 Lets consider a program that input all the elements of a one dimensional integer
array and then asks the user to enter an element to find inside the array.

37
SEARCHING ARRAY ELEMENT USING LINEAR
SEARCH

38
SEARCHING ARRAY ELEMENT USING LINEAR
SEARCH

39
SEARCHING ARRAY ELEMENT USING LINEAR
SEARCH
nums
0 8
1 9
2 4
3 5
4 7
5 8
6 6
7 5
8 8
9 9 40
SORTING ARRAY ELEMENT USING BUBBLE SORT

 Bubble sort, sometimes referred to as sinking sort, is a simple sorting


algorithm that works by repeatedly stepping through the list to be sorted, comparing
each pair of adjacent items and swapping them if they are in the wrong order. The
pass through the list is repeated until no swaps are needed, which indicates that the
list is sorted. The algorithm gets its name from the way smaller elements "bubble" to
the top of the list.
 Lets consider a program that initializes the elements of a one dimensional integer
array and then sorts it using bubble sort.

41
SORTING ARRAY ELEMENT USING BUBBLE SORT

42
SORTING ARRAY ELEMENT USING BUBBLE SORT

43
SORTING ARRAY ELEMENT USING BUBBLE SORT
nums
0 10
1 98
2 32
3 -8
4 -41
5 58
6 74
7 82
8 97
9 12 44
PROGRAM EXAMPLES
ARRAYS

45
PROGRAM EXAMPLE 01
Problem Statement:
Write a program in C++ that initializes two arrays A and B. The program should
create, calculate and display the contents of array C as following.
A B C
87.2
25 87
5
14.1
14 11
1
12.1
12 10
0
81.7
74 81
4
67.5
58 67
8
94.7
74 94
4
Note that every item in array
98
A and74
B is of 298.7
digit number.
4 46
84.8
84 82
2
PROGRAM EXAMPLE 01

47
PROGRAM EXAMPLE 01

48
PROGRAM EXAMPLE 01

49
PROGRAM EXAMPLE 02
Problem Statement:
Write a program in C++ that inputs the temperature values of entire week and
displays the day on which we had maximum and minimum temperature.

Temperatu
re
Monday 35.41
Tuesday 34.15
Wednesda
33.51
y
Thursday 33.12
Friday 35.45
Saturday 36.74
Sunday 34.88
50
PROGRAM EXAMPLE 02

51
PROGRAM EXAMPLE 02

52
PROGRAM EXAMPLE 02

53
PROGRAM EXAMPLE 03
Problem Statement:
Write a program in C++ that inputs the elements of an integer array from the
user and displays the number of positive, negative and zero values; sum of
positive numbers and sum of negative numbers present in the array.
Numbers
85
74
-81
Positive Numbers = 5
15
Negative Numbers = 4
-74
Zeros = 1
0
Sum of Positive Numbers = 277
-36
Sum of Negative Numbers = 216
-25
54
49 54
PROGRAM EXAMPLE 03

55
PROGRAM EXAMPLE 03

56
PROGRAM EXAMPLE 03

57
PROGRAM EXAMPLE 03

58
PROGRAM EXAMPLE 04

Problem Statement:
Write a program in C++ that performs the addition of two matrices of size 3 x
3. Program should create three matrices namely A, B and C. Matrix A and B are
define below. The matrix C is the defined as C = A + B.

59
PROGRAM EXAMPLE 04

60
PROGRAM EXAMPLE 04

61
PROGRAM EXAMPLE 04

62
PROGRAM EXAMPLE 05

Problem Statement:
Write a program in C++ that creates a matrix A and generates its transpose in matrix T.

63
PROGRAM EXAMPLE 05

64
PROGRAM EXAMPLE 05

65
PROGRAM EXAMPLE 05

66
PROGRAM EXAMPLE 06
Problem Statement:
Three sessional test are conducted in the subject of Computer Programming.
Each of which having 05 maximum marks. The total sessional marks out of 10,
for every student, are calculated by adding two best test marks. Write a
program in C++ that inputs the roll number (Just the number not string) and
marks of three sessional tests for five students and displays the total marks for
each of the student.
Roll Test Test Test
Total
No 01 02 03
Student 01
4 5 3 9
1
Student 03
5 5 4 10
2
Student 05
2 4 3 7
3
Student 07 67
0 4 1 5
4
Student 09
PROGRAM EXAMPLE 06

68
PROGRAM EXAMPLE 06

69
PROGRAM EXAMPLE 06

70
Thanks 
[email protected] 01/22/2025 71

You might also like