ScAs: C10 Explained for Beginners

Let’s go through Chapter 10 in a super beginner-friendly, step-by-step way, with clear examples and why things matter.


Chapter 10 – How to Work with Built-In Arrays and C Strings (Beginner Edition)


1. What Is a Built-In Array?

A built-in array in C++ is like a row of boxes (all the same size), and each box stores a value of the same type.

Example:
int numbers[5];  // An array that can hold 5 integers

Here’s what’s happening:

  • You made 5 "int" boxes in a row.

  • The index (or label) of the boxes goes from 0 to 4.

Why Index Starts at 0:

This is just how C++ and many other programming languages are built. It makes some operations simpler and faster under the hood.

Storing and Getting Values:
numbers[0] = 10;  // Store 10 in the first box
cout << numbers[0];  // Prints 10

Don’t do this:

numbers[5] = 100;  // This is out-of-bounds! Array only has index 0–4

Going beyond the array size causes undefined behavior, which means your program might crash or act weird.


2. How to Initialize Arrays

You can give starting values when creating the array:

int nums[3] = {1, 2, 3};

You can also skip the size:

int nums[] = {1, 2, 3};  // Compiler counts the values and sets size to 3

If you don’t give values for all elements:

int nums[5] = {1, 2};  // Remaining values (3, 4, 5) will be 0

If you want to zero-out the array:

int nums[5] = {0};  // All elements set to 0

3. Looping Through Arrays

To access all items in an array, you usually use a for loop.

int nums[3] = {10, 20, 30};

for (int i = 0; i < 3; i++) {
    cout << nums[i] << " ";
}

This will print:

10 20 30
Why You Need to Know the Size:

Built-in arrays don’t have a .size() method. You need to track the size yourself, often with a constant:

const int SIZE = 3;

4. Passing Arrays to Functions

When you pass an array to a function, you’re actually passing the memory address of the first element.

void show(int arr[], int size) {
    for (int i = 0; i < size; i++)
        cout << arr[i] << " ";
}

In main():

int data[3] = {4, 5, 6};
show(data, 3);  // Output: 4 5 6
Why You Need to Pass the Size Separately:

Because when the array is passed, the function does not know how many elements it has — only where the first element is.


5. You Can’t Use == to Compare Arrays

This doesn’t do what you think:

if (arr1 == arr2)  // ❌ Wrong!

This just compares memory addresses, not the contents. Instead, compare each element:

bool same = true;
for (int i = 0; i < size; i++) {
    if (arr1[i] != arr2[i]) {
        same = false;
        break;
    }
}

Same goes for copying — use a loop, not =.

for (int i = 0; i < size; i++)
    arr2[i] = arr1[i];

6. What Is a C String?

A C string is just a char array with a special ending character called the null terminator ('\0').

char name[] = "Alex";

This actually stores:

'A' 'l' 'e' 'x' '\0'

So, C strings must always end with '\0' or things break.

Don’t forget the null terminator if you're building a string manually:

char name[5] = {'A', 'l', 'e', 'x', '\0'};  // ✅ Correct

7. Input with C Strings

To get a full line of text (including spaces), use:

char fullName[50];
cin.getline(fullName, 50);

This reads a full line of input into a C string.


8. C String Utility Functions

From <cstring>:

  • strlen(str) – returns length (not counting \0)

  • strcpy(dest, src) – copies string

  • strncpy(dest, src, n) – safer version (copies n chars)

  • strcmp(a, b) – compares two strings (0 means equal)

  • cin.getline(str, size) – safe input

Example:

char name[10];
strcpy(name, "John");

9. Searching Arrays

Linear Search

Look at each element one-by-one:

int find(int arr[], int size, int value) {
    for (int i = 0; i < size; i++)
        if (arr[i] == value)
            return i;
    return -1;  // Not found
}
Binary Search

Faster, but array must be sorted.

int binary_search(int arr[], int size, int value) {
    int first = 0, last = size - 1;

    while (first <= last) {
        int mid = (first + last) / 2;
        if (arr[mid] == value)
            return mid;
        else if (arr[mid] > value)
            last = mid - 1;
        else
            first = mid + 1;
    }

    return -1;
}

10. Sorting Arrays

Bubble Sort

Keep swapping neighbors if they’re out of order.

void bubble_sort(int arr[], int size) {
    bool swapped;
    do {
        swapped = false;
        for (int i = 0; i < size - 1; i++) {
            if (arr[i] > arr[i + 1]) {
                swap(arr[i], arr[i + 1]);
                swapped = true;
            }
        }
    } while (swapped);
}
Selection Sort

Find the smallest item and move it to the front.

void selection_sort(int arr[], int size) {
    for (int i = 0; i < size - 1; i++) {
        int min_index = i;
        for (int j = i + 1; j < size; j++) {
            if (arr[j] < arr[min_index])
                min_index = j;
        }
        swap(arr[i], arr[min_index]);
    }
}

11. 2D Arrays

A 2D array is like a table (rows and columns):

int table[3][4];  // 3 rows, 4 columns

You use two indexes:

table[1][2] = 10;  // Row 1, column 2

Loop through:

for (int row = 0; row < 3; row++) {
    for (int col = 0; col < 4; col++) {
        cout << table[row][col];
    }
}

12. Passing 2D Arrays to Functions

To pass a 2D array, you must tell the compiler how many columns:

void show(int arr[][4], int rows) {
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < 4; j++)
            cout << arr[i][j];
    }
}

Use a constant for column size:

const int COLS = 4;

Final Summary

Concept

Why it matters

Arrays

Store multiple values in one variable

Indexing

Always starts at 0

C Strings

Character arrays ending in \0

Functions

Pass array + size; array becomes pointer

Can’t use ==

Need to compare/copy manually

Searching

Use loops (linear or binary)

Sorting

Bubble (simple), Selection (faster)

2D Arrays

Like a table: arr[row][col]

Utility functions

strlen, strcpy, strcmp, etc.