Unit 2 (C++)
Unit 2 (C++)
#include <iostream>
int main() {
cout << "Enter automobile sales for each year from 1932 to 1984:\n";
int NUM = 0;
NUM++;
}
cout << "\nNumber of years with sales more than 300: " << NUM << endl;
cout << START_YEAR + i << " : " << AUTO[i] << " automobiles\n";
return 0;
1. Start
6. Shift all elements from position n-1 to pos one position to the right
8. Increment n by 1
10. End
1. Start
5. Shift all elements from pos+1 to n-1 one position to the left
6. Decrease n by 1
8. End
1. Start
4. For i = 0 to n-1, do
9. End
1. Start
10. End
✅ 1. Bubble Sort – Theoretical Algorithm
Definition:
Bubble Sort is a comparison-based sorting technique. It works by repeatedly comparing
adjacent elements and swapping them if they are in the wrong order. The largest elements
"bubble" to the end with each pass.
🔸 Steps:
1. Begin with the first element of the array.
2. Compare it with the next element.
3. If the current element is greater than the next, swap them.
4. Move to the next pair and repeat the comparison and swap if needed.
5. Continue this process until the end of the array. This completes one pass.
6. Repeat the process for all elements except the last sorted ones.
7. After each pass, the next largest element is placed in its correct position.
8. Continue the passes until no more swaps are needed or all elements are sorted.
🔸 Steps:
1. Start with the second element (since the first element is trivially sorted).
2. Compare it with the elements before it.
3. Shift all larger elements one position to the right.
4. Insert the current element into the position where it is larger than the one before and
smaller than the one after.
5. Repeat the process for all remaining elements.
6. The array gets divided into a sorted and an unsorted part, which gets merged during each
insertion.
7. Continue until all elements are inserted into their correct positions.
8. The array is now sorted.
🔸 Steps:
1. Start from the first element of the array.
2. Find the smallest element in the entire array.
3. Swap this smallest element with the first element.
4. Now, the first element is in the correct position.
5. Move to the second element and repeat the process for the remaining unsorted part.
6. Continue finding the minimum from the remaining unsorted elements and swapping with
the front.
7. Repeat until all elements are placed in their correct positions.
8. The array is now sorted in ascending order.
🔷 Sample Array:
We will use this array for all examples:
A = [5, 2, 9, 1]
Pass-by-Pass:
Pass 1:
Pass 2:
Pass 3:
Step-by-Step:
Step 1:
2 is compared with 5 → insert before 5 → [2, 5, 9, 1]
Step 2:
9 is compared with 5 → stays in place → [2, 5, 9, 1]
Step 3:
1 is compared with 9, 5, 2 → insert before all → [1, 2, 5, 9]
Step-by-Step:
Step 1:
Find min in [5, 2, 9, 1] → 1
Swap with A[0] → [1, 2, 9, 5]
Step 2:
Find min in [2, 9, 5] → 2
Swap with A[1] → [1, 2, 9, 5]
Step 3:
Find min in [9, 5] → 5
Swap with A[2] → [1, 2, 5, 9]
int main() {
if (A[i][j] % 2 == 0)
evenSum += A[i][j];
else
oddSum += A[i][j];
cout << "Sum of even numbers: " << evenSum << endl;
cout << "Sum of odd numbers: " << oddSum << endl;
return 0;
🔹 Sample Input:
Enter number of rows and columns: 2 3
123
456
🔹 Output:
Sum of even numbers: 12
Null pointer
We can create a null pointer by assigning null value during the pointer declaration. This method is useful
when you do not have any address assigned to the pointer. A null pointer always contains value 0.
#include <stdio.h>
int main()
return 0;
Output:
2. Void Pointer
In C programming, a void pointer is also called as a generic pointer. It does not have any standard data
type. A void pointer is created by using the keyword void. It can be used to store an address of any
variable.
#include <stdio.h>
int main()
Output:
3. Wild pointer
A pointer is said to be a wild pointer if it is not being initialized to anything. These types of pointers are not
efficient because they may point to some unknown memory location which may cause problems in our
program and it may lead to crashing of the program. One should always be careful while working with wild
pointers.
#include <stdio.h>
int main()
printf("\n%d",*p);
return 0;
Output:
✅ 1. Pointer Example
A pointer stores the address of another variable.
cpp
CopyEdit
#include <iostream>
int a = 10;
cout << "Value of ptr (address): " << ptr << endl;
cout << "Value at address ptr points to (*ptr): " << *ptr << endl;
return 0;
cpp
CopyEdit
#include <iostream>
void modify(int x) {
x = x + 10;
int main() {
int a = 5;
modify(a);
cout << "After pass by value, a = " << a << endl; // Output: 5
return 0;
cpp
CopyEdit
#include <iostream>
void modify(int* x) {
*x = *x + 10;
int main() {
int a = 5;
modify(&a);
cout << "After pass by address, a = " << a << endl; // Output: 15
return 0;
CopyEdit
#include <iostream>
void modify(int& x) {
x = x + 10;
int main() {
int a = 5;
modify(a);
cout << "After pass by reference, a = " << a << endl; // Output: 15
return 0;