Arrays
Arrays
PROGRAMMING FUNDAMENTALS
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
4
WHAT IS ARRAY
[email protected] 01/22/2025 5
ARRAYS
6
ARRAYS
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
One-Dimensional Multi-Dimensional
Array Array
9
ONE-DIMENSIONAL ARRAY
Name
Data Type
Size
11
DECLARING ONE-DIMENSIONAL ARRAY
Name
Data Type
Size
Elements
13
INITIALIZING ONE-DIMENSIONAL ARRAY
33.3
INITIALIZING ONE-DIMENSIONAL ARRAY
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
23
TWO-DIMENSIONAL ARRAY
nums
57 74 11
10 14 87
47 48 98
24
DECLARING TWO-DIMENSIONAL ARRAY
Name
Data Type
Size for both the dimensions
25
DECLARING TWO-DIMENSIONAL ARRAY
26
INITIALIZING TWO-DIMENSIONAL ARRAY
Name
Data Type
Size for both the dimensions
Elements
27
INITIALIZING TWO-DIMENSIONAL ARRAY
28
INITIALIZING TWO-DIMENSIONAL ARRAY
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
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
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
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