Chpter One
Chpter One
• One-dimensional array
• Multi-dimensional array
• Working with string
• Structures in c++
• Passing Arrays to Functions in C++
• Arrays and Pointers
Array
• Arrays are fundamental data structures in programming that allow you to store and
organize multiple values of the same data type under a single variable name.
• They are essential for various programming tasks, from simple data
storage to complex algorithms.
One-Dimensional Arrays
•Specifies the data type of the elements and the size of the array.
Syntax: data_type array_name[size];
Example: int numbers[5];
•int: This specifies that the array will store integer values.
•numbers: This is the name given to the array.
•[5]: This indicates that the array can hold 5 integer elements.
• So, this declaration creates an array named numbers that can store 5 integers.
Array Initialization
• Assigning initial values to the array elements. Can be done during declaration or later
using assignment statements.
Accessing Elements:
•Uses indexing to refer to specific elements within the array.
•Indexing starts from 0.
Syntax: array_name[index]
Example:
int numbers[5] = {10, 20, 30, 40, 50};
Cont’d…
In this example:
•numbers is the array name.
•int indicates the data type of elements (integers).
•5 specifies the size of the array (it can hold 5 integers).
•The elements are initialized with values 10, 20, 30, 40, and 50.
Accessing Elements:
To access an element, you use its index:
•numbers[0] refers to the first element (value 10)
•numbers[1] refers to the second element (value 20) ... and so on.
Example
#include <iostream>
int main() {
// Declare and initialize a one-dimensional array of integers
int numbers[5] = {10, 20, 30, 40, 50};
std::cout << "First element: " << numbers[0] << std::endl; // Access and print the elements of the
array
std::cout << "Last element: " << numbers[4] << std::endl; // Modify an element numbers[2] = 100;
// Iterate through the array and print its elements
for (int i = 0; i < 5; i++) {
std::cout << numbers[i] << " ";
}
return 0;
}
Cont’d…
Operations on One-Dimensional Arrays:
•Traversing: Iterating through each element.
•Searching: Finding specific elements.
•Sorting: Arranging elements in order.
•Inserting: Adding new elements.
•Deleting: Removing elements.
Example of how to traverse a one-dimensional array in C++:
#include<iostream>
using namespace std;
int main() {
int arr[] = {10, 20, 30, 40, 50};
int n = sizeof(arr) / sizeof(arr[0]); // Traverse the array using a for loop
for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
cout << endl;
return 0;
}
Output:10 20 30 40 50
Cont’d…
Advantages of One-Dimensional Arrays:
• Simple to understand and implement.
Accessing Elements:
int element = cube[1][2][3]; // Accesses the element at layer 1, row 2, column 3 (value
24)
Example
#include <iostream>
int main() { // Declare and initialize a 3D array
int cube[2][2][3] = {
{{1, 2, 3}, {4, 5, 6}},
{{7, 8, 9}, {10, 11, 12}} }; // Access and print elements
std::cout << "Element at layer 1, row 1, column 2: " << cube[1][0][1] << std::endl; // Iterate through the
array and print its elements
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
for (int k = 0; k < 3; k++) {
std::cout << cube[i][j][k] << " ";
}
std::cout << std::endl;
}
std::cout << std::endl;
}
return 0;
}
Working with Strings in C++
• Strings in C++ are sequences of characters enclosed in double quotes.
• They are versatile and essential for various tasks, from simple text manipulation to
complex text processing.
Declaring and Initializing Strings
• There are two primary ways to declare and initialize strings in C++:
Cont’d…
1. Using the std::string Class:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str1 = "Hello, world!";
string str2; // Assign a value to str2 later
str2 = "Welcome to C++";
cout << str1 << endl;
cout << str2 << endl;
return 0;
}
Cont’d…
2. Using Character Arrays:
#include <iostream>
using namespace std;
int main()
{
char str1[20] = "Hello, world!";
char str2[30];
// Assign a value to str2 later
strcpy(str2, "Welcome to C++");
cout << str1 << endl;
cout << str2 << endl;
return 0;
}
String Operations
•This function takes an integer array arr and its size size as input.
•It iterates through the array using a for loop.
•Inside the loop, it prints each element of the array followed by a space.
•After printing all elements, it inserts a newline character using std::endl.
•An integer array arr is declared and initialized with values {1, 2, 3, 4, 5}.
•The size variable is calculated using sizeof(arr) / sizeof(arr[0]) to determine the
number of elements in the array.
•The printArray function is called with arr and size as arguments.
By Reference:
• When an array is passed by reference, a pointer to the array is passed to the function.
• This allows the function to modify the original array.
Example:
#include <iostream>
void modifyArray(int arr[], int size) {
for (int i = 0; i < size; i++) {
arr[i] *= 2; }}
int main() {
int arr[] = {1, 2, 3, 4, 5};
int size = sizeof(arr) / sizeof(arr[0]);
modifyArray(arr, size); // Pointer to arr is passed
for (int i = 0; i < size; i++)
{
std::cout << arr[i] << " ";
}
std::cout << std::endl;
return 0;
}
Cont’d…
•This function takes an integer array arr and its size size as input.
•It iterates through the array using a for loop.
•Inside the loop, it doubles each element of the array.
•An integer array arr is declared and initialized with values {1, 2, 3, 4, 5}.
•The size variable is calculated using sizeof(arr) / sizeof(arr[0]) to determine the number of
elements in the array.
•The modifyArray function is called with arr and size as arguments.
•After the function call, the original arr array is modified, and its elements are printed.
Cont’d…
Key points:
• By Value:
• Inefficient for large arrays.
• The original array remains unchanged within the function.
• By Reference:
• Efficient for large arrays as only a pointer is passed.
• The function can modify the original array.
Arrays and Pointers
• In C++, arrays and pointers are closely related.
• An array name can be treated as a constant pointer to its first element.
• This allows you to use pointer arithmetic to access and manipulate array elements.
Example:
#include <iostream>int main()
{
int arr[5] = {10, 20, 30, 40, 50};
int *ptr = arr; // Pointer to the first element of the array // Accessing elements using array indexing
std::cout << "Element at index 2: " << arr[2] << std::endl; // Accessing elements using pointer arithmetic
std::cout << "Element at index 2: " << *(ptr + 2) << std::endl; // Iterating through the array using pointer
arithmetic
for (int i = 0; i < 5; i++)
{
std::cout << *(ptr + i) << " ";
}
std::cout << std::endl;
return 0;
}