Professional Documents
Culture Documents
L1 Arrays
L1 Arrays
Lecture 1: Array
Contents
Introduction ................................................................................................................................ 1
One-Dimensional Array ............................................................................................................. 2
Declaration: ............................................................................................................................ 2
Default Values & Initialization .............................................................................................. 2
Accessing Array Elements: .................................................................................................... 5
Input Array Elements: ........................................................................................................ 5
Changing Array Elements: ................................................................................................. 6
Using Array Elements: ....................................................................................................... 6
Looping through an array........................................................................................................... 7
Practical...................................................................................................................................... 9
Introduction
Arrays are a fundamental data structure in computer science and are widely used in C++ and
other programming languages. An array is a collection of elements of the same data type,
stored in contiguous memory locations, that can be accessed using an index. The indexing of
elements in an array starts from 0, meaning that the first element in the array has an index of
0, the second element has an index of 1, and so on.
Arrays are used in many applications to store and process large amounts of data efficiently.
They provide a convenient way to store a fixed number of elements of the same data type,
allowing you to easily access and manipulate the elements. With arrays, you can perform
various operations such as sorting, searching, and processing elements in bulk, which would
be difficult to achieve using individual variables.
In C++, arrays can be declared as a variable of a specific data type, such as int, float, or char,
followed by square brackets indicating the size of the array. The size of the array must be
specified at the time of declaration and cannot be changed afterwards. The elements of the
array can be initialized at the time of declaration or later, and can be accessed using an index
operator, which specifies the position of the element in the array.
Arrays play a crucial role in many computer algorithms and data structures, making them an
essential topic for students to learn in computer science. They provide a foundation for
understanding more complex concepts such as dynamic programming, searching and sorting
algorithms, and data structures such as linked lists and trees
1
Ministry of Higher Education Subject: Programming Fundamentals II
and Scientific Research Semester: 2nd
University of Zakho Academic Year: 2022 – 2023
Faculty of Science Lecturer: Wahab Kh. Arabo
Dept. of Computer Science
One-Dimensional Array
Declaration:
In C++, arrays can be declared by specifying the data type, followed by the array name, and
the size of the array within square brackets. For example, to declare an array of 10 integers,
you could write the following code:
int myArray[10];
myArray
values ? ? ? ? ? ? ? ? ? ?
Index 0 1 2 3 4 5 6 7 8 9
The size of the array must be specified at the time of declaration and cannot be changed
afterwards. The size of the array determines the number of elements that can be stored in the
array.
2
Ministry of Higher Education Subject: Programming Fundamentals II
and Scientific Research Semester: 2nd
University of Zakho Academic Year: 2022 – 2023
Faculty of Science Lecturer: Wahab Kh. Arabo
Dept. of Computer Science
When declaring an array, you can also initialize it with values. This is done by providing a
comma-separated list of values within curly braces. For example, to declare and initialize an
array of 5 integers, you could write the following code:
values 18 82 103 -4 65
Index 0 1 2 3 4
In C++, when you initialize an array with fewer values than the declared size of the array, the
remaining elements are set to zero (0) by default. For example, in the following code:
The first four elements of the myArray are initialized to 18, 22, 103, and -4, respectively. The
last element, which is the fifth element, will be initialized to 0.
So, the myArray array will have the following values: {18, 22, 103, -4, 0}
In the same way note the values of that array in each of the following declarations:
3
Ministry of Higher Education Subject: Programming Fundamentals II
and Scientific Research Semester: 2nd
University of Zakho Academic Year: 2022 – 2023
Faculty of Science Lecturer: Wahab Kh. Arabo
Dept. of Computer Science
Initializing an array with more values than its declared size in C++ will result in a compiler
error ( an error indicating that too many initializers are provided) .
See the last page of this lecture to know how to find the size of the above array (A[ ] )
4
Ministry of Higher Education Subject: Programming Fundamentals II
and Scientific Research Semester: 2nd
University of Zakho Academic Year: 2022 – 2023
Faculty of Science Lecturer: Wahab Kh. Arabo
Dept. of Computer Science
values 18 82 103 -4 65
Index 0 1 2 3 4
Once you have declared and initialized an array, you can access individual elements of the
array by using the array name followed by the index of the element in square brackets[ ].
The index of the first element in the array is 0, the second element has an index of 1, and so
on.
For example, to access the second element of the array "myArray" declared in the above
example, you would write the following code:
// above line: read myArray[1] value which is ( 82 ) and store it in the variable secondElement
It is important to note that in C++, arrays are zero-indexed, meaning that the first element has
an index of 0, and not 1. Accessing an element outside the bounds of the array can result in an
error, so it is important to ensure that the index you are using to access an element is within the
bounds of the array.
5
Ministry of Higher Education Subject: Programming Fundamentals II
and Scientific Research Semester: 2nd
University of Zakho Academic Year: 2022 – 2023
Faculty of Science Lecturer: Wahab Kh. Arabo
Dept. of Computer Science
#include<iostream>
using namespace std;
int main()
{
int x[3] = { 25,50,75 };
cout << " the value of first element (Now): ";
cout << x[0] << endl;
//change it
x[0] = 100;
cout << " the value of first element (Now): ";
cout << x[0] << endl;
return 0;
}
//or
//marks[1] += 5;
return 0;
}
6
Ministry of Higher Education Subject: Programming Fundamentals II
and Scientific Research Semester: 2nd
University of Zakho Academic Year: 2022 – 2023
Faculty of Science Lecturer: Wahab Kh. Arabo
Dept. of Computer Science
#include<iostream>
using namespace std;
int main()
{
int arr[10]={11,-43,-64,23,74}; // remember the other values will be zeros
Write a C++ program that ask user to input 5 marks, find their summation and average.
Sol 1 : without using array
#include <iostream>
using namespace std;
int main()
{
float mark1, mark2, mark3, mark4, mark5, sum, average;
cout << "Enter the first mark: ";
cin >> mark1;
cout << "Enter the second mark: ";
cin >> mark2;
cout << "Enter the third mark: ";
cin >> mark3;
cout << "Enter the fourth mark: ";
cin >> mark4;
cout << "Enter the fifth mark: ";
cin >> mark5;
sum = mark1 + mark2 + mark3 + mark4 + mark5;
average = sum / 5;
cout << "The sum of the marks is: " << sum << endl;
cout << "The average of the marks is: " << average << endl;
return 0;
}
7
Ministry of Higher Education Subject: Programming Fundamentals II
and Scientific Research Semester: 2nd
University of Zakho Academic Year: 2022 – 2023
Faculty of Science Lecturer: Wahab Kh. Arabo
Dept. of Computer Science
average = (float)sum / 5;
cout << "Sum of marks: " << sum << endl;
cout << "Average of marks: " << average << endl;
return 0;
}
8
Ministry of Higher Education Subject: Programming Fundamentals II
and Scientific Research Semester: 2nd
University of Zakho Academic Year: 2022 – 2023
Faculty of Science Lecturer: Wahab Kh. Arabo
Dept. of Computer Science
//input first
cout << "Enter 5 marks: " << endl;
for (int i = 0; i < 5; i++) {
cout << "enter Mark" << (i + 1) << " "; //to print the index such as Mark1
cin >> marks[i];
}
//then find sum
for (int i = 0; i < 5; i++) {
sum += marks[i];
}
average = (float)sum / 5;
cout << "Sum of marks: " << sum << endl;
cout << "Average of marks: " << average << endl;
return 0;
}
Assume you been asked to:
• Input 100 marks not only 5
• Output the marks at the end of the program
• Find max and min of marks,
• Ignore negative values or those that are not in range (0,100)
• …. etc.
Practical
For the practical session, you will be required to implement and run all the programs in this
lecture.
Please make sure to follow the sequence and execute each program properly
Homework:
Use Boolean, Char, Double arrays as we have done with integer array.
Quiz: ???
Next Lecture: multi-dimensional arrays
9
Ministry of Higher Education Subject: Programming Fundamentals II
and Scientific Research Semester: 2nd
University of Zakho Academic Year: 2022 – 2023
Faculty of Science Lecturer: Wahab Kh. Arabo
Dept. of Computer Science
#include <iostream>
using namespace std;
int main() {
int x[] = { 25,65,14,53,23,12,64 };
cout << "size of each item " << sizeof(x[0]) <<endl; // integer size is 4 bytes
cout << "size of all items " << sizeof(x) << endl;// 3*integer size
int len = sizeof(x) / sizeof(x[0]);
return 0;
}
//outputs
10