You are on page 1of 10

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

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.

Default Values & Initialization

The default value of an uninitialized one-dimensional integer array in C++ is undefined. In


other words, the elements in the array will contain whatever value happens to be stored in the
memory location assigned to them.
See the following example:
Code cppshell.com Visual studio
#include<iostream> 20912 -858993460
using namespace std;
int main()
21048 - 858993460
{ 21048 - 858993460
int x[5]; 0 - 858993460
cout << x[0] << endl; 5263976 - 858993460
cout << x[1] << endl;
cout << x[2] << endl;
cout << x[3] << endl;
cout << x[4] << endl;
return 0;
}
This could lead to unexpected results in your program, so it is generally RECOMMENDED
to initialize all elements in an array to a specific value or using an initializer list.

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:

int myArray[5] = {18,22, 103, -4, 65};

values 18 82 103 -4 65
Index 0 1 2 3 4

See the outputs

Code cppshell.com Visual studio


#include<iostream> 18 18
using namespace std; 22 22
int main() 103 103
{ - 4 - 4
int x[5]; 65 65
cout << x[0] << endl;
cout << x[1] << endl;
cout << x[2] << endl;
cout << x[3] << endl;
cout << x[4] << endl;
return 0;
}

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:

int myArray[5] = { 18, 22, 103, -4 };

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:

Declarations of array (size 5) Number of values Values


int myArray[5] = { 18, 22, 103, -4 ,6}; 5 { 18, 22, 103, -4 ,6};
int myArray[5] = { 18, 22, 103, -4 }; 4 { 18, 22, 103, -4 ,0};
int myArray[5] = { 18, 22, 103}; 3 { 18, 22, 103, 0 ,0};
int myArray[5] = { 18, 22 }; 2 { 18, 22, 0, 0 ,0 };
int myArray[5] = { 18 }; 1 { 18, 0, 0, 0 ,0};
1 { 0, 0, 0, 0 ,0};
Now we can initialize an array with zeros
as follow:
int myArray[5] = { 0 };

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) .

int arr[] ; // Error

int number[] = {}; // Error

int A[] = { 25,65,14,53,23,12,64 } // Size of Array is 7

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

Accessing Array Elements:

int myArray[5] = {18,22, 103, -4, 65};

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:

int secondElement = myArray[1];

// 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.

Input Array Elements:


To input a value for an element of array in C++, you can use the input stream operator cin in
to read the value from the user, and then assign it to the corresponding element of the array.
#include<iostream>
using namespace std;
int main()
{
int x[3] = { 0 };
cout << "Enter a Number: ";
cin >> x[0];
cout << "Enter a Number: ";
cin >> x[1];
cout << "Enter a Number: ";
cin >> x[2];
cout << " array values : \n";
cout << x[0] << endl;
cout << x[1] << endl;
cout << x[2] << endl;
return 0;
}

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

Changing Array Elements:


To change an element in a one-dimensional array in C++, you can access the desired element
of the array using its index and assign a new value to it.

#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;
}

Using Array Elements:


elements of an array can be used in operations just like any other variable. For example, you
can perform arithmetic operations using array elements, compare elements, use them in control
structures, etc.
#include<iostream>
using namespace std;
int main()
{
int marks[3] = { 85,50,74 };

int sum = marks[0] + marks[1] + marks[2];


cout << " Sum is : " << sum;
double average = sum / 3.0;
cout << "\n Average is : " << average;

//increase the second mark by 5


marks[1] = marks[1] + 5;

//or
//marks[1] += 5;

cout << "\n 2nd Mark " << marks[1];

return 0;
}

// try to input marks, and find if student is passed or not

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

Looping through an array


As it has been mentioned the arrays are a collection of elements of the same data type, stored
in contiguous memory locations, and these elements are indexed from 0 to sine -1. These
advantages can be utilized; meaning that each element in the array can be easily accessed and
manipulated using its index. This makes arrays a powerful tool for data processing, as they
allow developers to loop through the elements and perform operations on each one.

#include<iostream>
using namespace std;
int main()
{
int arr[10]={11,-43,-64,23,74}; // remember the other values will be zeros

for (int i = 0; i < 10; i++)


{
cout << arr[i] << endl;
}
return 0;
}

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

Sol 2 : using array but without loops


#include <iostream>
using namespace std;
int main()
{
float marks[5]={0}, sum, average;
cout << "Enter the first mark: ";
cin >> marks[0];
cout << "Enter the second mark: ";
cin >> marks[1];
cout << "Enter the third mark: ";
cin >> marks[2];
cout << "Enter the fourth mark: ";
cin >> marks[3];
cout << "Enter the fifth mark: ";
cin >> marks[4];
sum = marks[0] + marks[1] + marks[2] + marks[3] + marks[4];
average = sum / 5;
cout << "The sum of the marks is: " << sum << endl;
cout << "The average of the marks is: " << average << endl;
return 0;
}

Sol 3 : array and loops I


#include <iostream>
using namespace std;
int main() {
float marks[5];
float sum = 0;
float average;

cout << "Enter 5 marks: " << endl;


for (int i = 0; i < 5; i++) {
cout << "enter Mark" << (i + 1) << " ";
cin >> marks[i];
sum += marks[i];
}

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

Sol 3 : array and loops II


#include <iostream>
using namespace std;
int main() {
float marks[5];
float sum = 0;
float average;

//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]);

cout << "Number of Items " << len << endl;


for (int i = 0; i < len; i++) {
cout << "x[" << i << "] is ";
cout << x[i] << endl;
}

return 0;
}
//outputs

10

You might also like