0% found this document useful (0 votes)
32 views28 pages

Chpter One

ch-3

Uploaded by

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

Chpter One

ch-3

Uploaded by

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

Chapter One

Array, String and Structure


Contents

• 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

• A one-dimensional array is a collection of elements of the same data type, stored in


contiguous memory locations.

• It's like a row of boxes, each holding a single value.


Array Declaration

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

• Efficient for storing and accessing data.

• Useful for various algorithms and data structures.

Limitations of One-Dimensional Arrays:


• Fixed size, cannot be resized dynamically.

• Less flexible for complex data structures


Multidimensional Arrays
• Multidimensional arrays are extensions of one-dimensional arrays, allowing you to
store data in a tabular or grid-like format.
• They are particularly useful for representing matrices, tables, or any data that can be
organized into rows and columns.
Two-Dimensional Arrays
• A two-dimensional array is the most common type of multidimensional array.
• It's essentially a matrix with rows and columns.
Declaration:
data_type array_name[rows][columns];
Example:
int matrix[3][4] =
{
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12}
};
This declares a 2D array named matrix with 3 rows and 4 columns.
Accessing Elements:
To access an element, you specify its row and column indices:
int element = matrix[1][2]; // Accesses the element at row 1, column 2 (value 7)
•matrix[1][2]: This expression accesses the element at row 1 (second row, as indexing starts
from 0) and column 2 (third column) of the matrix array.
•The value at this position is 7.
Example
#include <iostream>
int main() {
int matrix[2][3] = {{1, 2, 3}, {4, 5, 6}}; // Declare and initialize a 2D array
// Access and print elements
std::cout << "Element at row 1, column 2: " << matrix[1][1] << std::endl;
// Iterate through the array and print its elements
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 3; j++)
{
std::cout << matrix[i][j] << " ";
}
std::cout << std::endl;
}
return 0;
}
Three-Dimensional Arrays
• A three-dimensional array can be visualized as a cube with rows, columns, and layers.
• It can store and manipulate complex data structures in your C++ programs.
Declaration:
data_type array_name[layers][rows][columns];
Example:

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

• C++ provides a rich set of operations for manipulating strings:


1. Accessing Characters:
char ch = str1[0]; // Access the first character
2. String Concatenation:
string str3 = str1 + " " + str2;
Structures in C++
A structure in C++ is a user-defined data type that groups variables of different data types
under a single name. It's a way to create custom data types that suit specific needs.
Declaring a Structure:
struct Student
{
string name;
int age;
float gpa;
};
Accessing Members:
You can access the members of a structure using the dot (.)operator:
student1.name = "Alice"; student1.age = 20; student1.gpa = 3.8;
Cont’d…
Example: Printing Student Information
#include <iostream>
#include <string>
using namespace std;
struct Student
{
string name;
int age;
float gpa;
};
int main()
{
Student student1;
student1.name = "Alice";
student1.age = 20;
student1.gpa = 3.8;
cout << "Name: " << student1.name << endl;
cout << "Age: " << student1.age << endl;
cout << "GPA: " << student1.gpa << endl;
return 0;
}
Passing Arrays to Functions in C++
By Value:
• When an array is passed by value, a copy of the entire array is made and passed to the
function.
• This is inefficient for large arrays as it consumes extra memory and time.
Example:
#include <iostream>
void printArray(int arr[], int size)
{
for (int i = 0; i < size; i++)
{
std::cout << arr[i] << " ";
}
std::cout << std::endl;
}
int main()
{
int arr[] = {1, 2, 3, 4, 5};
int size = sizeof(arr) / sizeof(arr[0]);
printArray(arr, size);
// A copy of arr is passed
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 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;
}

You might also like