0% found this document useful (0 votes)
4 views

Braa

Uploaded by

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

Braa

Uploaded by

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

‫‪Array in c++‬‬

‫براء صالح‬
‫طلبة المرحلة ‪ -‬األولى‬
‫المادة ‪ -‬أساسيات البرمجة‬
‫القسم ‪ -‬الهندسة التقنية‬
‫الكلية ‪ -‬كلية الهندسة التقنية‬

‫بأشراف ‪ -‬أ‪.‬د يحيى مهدي هادي الميالي‬


Abstract

Arrays in C++ are a fundamental data structure that enables the storage and
manipulation of a fixed-size collection of elements of the same type. This report
delves into the intricacies of arrays in C++, covering single-dimensional, multi-
dimensional, and dynamic arrays. It provides a comprehensive overview of how
arrays are declared, initialized, and accessed. Through detailed explanations and
practical code examples, the report illustrates the utility and efficiency of arrays in
various programming scenarios. By understanding arrays, C++ programmers can
manage and process large sets of data effectively, making them a crucial tool in the
programmer's toolkit.

1. Introduction
Arrays in C++ are a fundamental data structure used to store a fixed-size sequential
collection of elements of the same type. They are widely used for their simplicity
and efficiency in accessing elements. This report will provide an overview of
arrays, their types, how to declare, initialize, and access them, and some practical
examples.
2. Types of Arrays
• Single-Dimensional Arrays: A linear collection of elements of the same type.
• Multi-Dimensional Arrays: Arrays of arrays, typically used for matrices and
higher-dimensional data structures.
• Dynamic Arrays: Arrays whose size can be determined at runtime using dynamic
memory allocation.
2.1 Single-Dimensional Arrays
 Declaration and Initialization
In C++, arrays are declared by specifying the type of its elements and the number
of elements it can hold. The syntax is as follows:
 Accessing Elements
Elements in an array are accessed using the index, with the first element at index 0.
Example Program
Here is a simple example that demonstrates the declaration, initialization, and
accessing of elements in a single-dimensional array:
#include <iostream>

using namespace std;

int main() {

int numbers[5] = {1, 2, 3, 4, 5}; // Accessing and printing elements

for(int i = 0; i < 5; i++) {

cout << "Element at index " << i << " is: " << numbers[i] << endl;

return 0;

2.2 Multi-Dimensional Arrays


Multi-dimensional arrays are arrays of arrays. The most commonly used is the
two-dimensional array, which can be visualized as a matrix.
 Declaration and Initialization

type arrayName[size1][size2];

 Accessing Elements

int element = matrix[1][1]; // Accessing element at row 1, column 1

matrix[0][1] = 5; // Changing the element at row 0, column 1

Example Program
Here is an example that demonstrates the use of a two-dimensional array:

#include <iostream>

using namespace std;

int main() {

int matrix[2][2] = {{1, 2}, {3, 4}};

// Accessing and printing elements

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

for(int j = 0; j < 2; j++) {

cout << "Element at matrix[" << i << "][" << j << "] is: " << matrix[i][j] << endl;

return 0;

}
2.3 Dynamic Arrays
Dynamic arrays can be created using pointers and dynamic memory allocation
with new and delete operators.
 Declaration and Initialization

int* array = new int[size];


 Accessing and Deleting
Accessing elements is similar to static arrays, but deletion must be handled
manually to free memory:

numbers[0] = 10; // Assigning value

delete[] numbers; // Freeing the dynamically allocated memory

Example Program
Here's an example demonstrating dynamic array usage:

#include <iostream>

using namespace std;

int main() {

int size;

cout << "Enter the size of the array: ";

cin >> size; // Dynamically allocating array

int* numbers = new int[size]; // Initializing array

for(int i = 0; i < size; i++) {

numbers[i] = i + 1;

} // Accessing and printing elements

for(int i = 0; i < size; i++) {

cout << "Element at index " << i << " is: " << numbers[i] << endl;

} // Deleting dynamically allocated array

delete[] numbers;

return 0;

3. Conclusion
Arrays in C++ are a powerful and essential tool for managing collections of data.
They provide a simple and efficient way to store and access multiple values of the
same type. Understanding how to declare, initialize, and manipulate arrays is
crucial for any C++ programmer. This report covered single-dimensional arrays,
multi-dimensional arrays, and dynamic arrays with examples, demonstrating their
usage and importance in C++ programming.

You might also like