Slide 1
Slide 1
COMPUTER PROGRAMMING
II
COURSE OUTLINE
• Introduction to Arrays
• Multidimensional Arrays
• Array Operations
• More Array Operations
• Dynamic Arrays (Pointers and Arrays)
• Array of Pointers and Pointers to Arrays
Day 1: Arrays and their Basics
You can initialize an array at the time of declaration.
int numbers[5] = {1, 2, 3, 4, 5}; // Array of 5 integers initialized
char vowels[5] = {'a', 'e', 'i', 'o', 'u'}; // Array of 5 characters
initialized
The number of values between braces { } cannot be larger than the
number of elements that we declare for the array between square
brackets [ ].
If you omit the size of the array, an array just big enough to hold
the initialization is created. Therefore, if you write:
double balance[] = {1000.0, 2.0, 3.4, 17.0, 50.0};
Default Initialization
If fewer initializers are provided than the size of the array, the
remaining elements are initialized to zero.
int numbers[5] = {1, 2}; // Remaining elements initialized to 0:
{1, 2, 0, 0, 0}
Empty Initialization
Initializing an array with empty braces sets all elements to
zero.
int numbers[5] = {}; // All elements initialized to 0: {0, 0, 0, 0, 0}
Initialize the elements in the program
The for loop is a perfect tool for looping through arrays
when you fill them with values.
Arrays can also be initialized using loops, which is useful
when you need to initialize elements based on some
condition or formula.
int numbers[5];
for (int i = 0; i < 5; i++) {
numbers[i] = i * 2;
}
string cars[5];
for (int i = 0; i < 5; i++) {
Cin >> cars[i];
}
Array Indexing in C++
Array indexing is the method used to access elements within an array using
their position within the array.
Understanding how to correctly index arrays is fundamental to working with
them effectively in C++.
Indexing Starts at 0: In C++, the first element of an array is at index 0, the
second element is at index 1, and so on.
Syntax: arrayName[index]
int numbers[5] = {10, 20, 30, 40, 50};
cout << numbers[0]; // Outputs 10
cout << numbers[4]; // Outputs 50
Accessing and Modifying Array Elements
Accessing Elements:
int numbers[3] = {10, 20, 30};
int first = numbers[0]; // first = 10
int second = numbers[1]; // second = 20
int third = numbers[2]; // third = 30
Modifying Elements:
numbers[1] = 25; // Changes the second element to 25
Iterating Through an Array
Using For Loop
int numbers[5] = {10, 20, 30, 40, 50};
for(int i = 0; i < 5; i++) {
cout << numbers[i] << " ";
}
// Outputs: 10 20 30 40 50
Lab Question
Question 1: Write a C++ program to perform the following
operations on an array:
Declare and initialize an array of 5 integers.
Access and print each element using a loop.
Modify the third element of the array.
Print the modified array.
Question 2: Write a C++ program to:
Declare and initialize an array of 10 integers.
Calculate and print the sum of all elements.
Calculate and print the average of all elements.
Question 3: Write a C++ program to:
Declare and initialize an array of 5 integers.
Reverse the elements of the array.
Print the reversed array.
Question: Write a C++ program to:
Declare and initialize an array of 6 integers.
Find and print the maximum element in the array.
Find and print the minimum element in the array.
Code Example 1: Array Sum and Average
• #include <iostream>
• using namespace std;
• int main() {
• const int size = 5;
• int numbers[size];
• int sum = 0;
•
• std::cout << "Enter " << size << " integers:\n";
•
• for (int i = 0; i < size; ++i) {
• cin >> numbers[i];
• sum += numbers[i];
• }
•
• double average = (sum) / size;
•
• std::cout << "Sum: " << sum << std::endl;
• std::cout << "Average: " << average << std::endl;
•
• return 0;
• }
Introduction to multidimensional arrays
• #include <iostream>
• using namespace std;
• Bubble sort repeatedly swaps adjacent elements if they are in the wrong order
until the entire array is sorted
• void bubbleSort(int arr[], int size) {
• int temp; // Temporary variable to swap with
• for (int i = 0; i < size - 1; ++i) {
• for (int j = 0; j < size - i - 1; ++j) {
• if (arr[j] > arr[j + 1]) {
temp = ara[i]; // pair is not in order.
arr[i] = arr[j+1];
ara[j+1] = temp; // “Float” the lowest to the highest.
• }
• }
• }
• }
Selection Sort
• #include <iostream>
•
• int main() {
• int x = 42;
• int* p = &x;
•
• std::cout << "Value of x: " << x << std::endl;
• std::cout << "Address of x: " << &x << std::endl;
• std::cout << "Value of p: " << p << std::endl;
• std::cout << "Value pointed by p: " << *p << std::endl;
• // Modifying x through the pointer p
• *p = 99;
• std::cout << "New value of x: " << x << std::endl;
•
• return 0;
• }
Dynamic Memory Allocation
• #include <iostream>
• #include <memory>
•
• int main() {
• std::unique_ptr<int> p = std::make_unique<int>(42);
• std::shared_ptr<int> q = std::make_shared<int>(10);
•
• std::cout << "Value of p: " << *p << std::endl;
• std::cout << "Value of q: " << *q << std::endl;
•
• return 0;
• }
Pointers and Functions
• #include <iostream>
•
• void swap(int* a, int* b) {
• int temp = *a;
• *a = *b;
• *b = temp;
• }
•
• int main() {
• int x = 10, y = 20;
•
• std::cout << "Before swapping: x = " << x << ", y = " << y << std::endl;
• swap(&x, &y);
• std::cout << "After swapping: x = " << x << ", y = " << y << std::endl;
•
• return 0;
• }
Pointers and Arrays