1D & 2D Arrays in C++ ProgLang - Bilal Ahmad Khan AKA Mr. BILRED - TechAmbitionX

1D & 2D Arrays - TechAmbitionX

Understanding 1D and 2D Arrays

Arrays are like containers that hold multiple values. Dusray alfaaz mein, An array is similar to a variable, but instead of storing a single value, it holds multiple values of the same type. These values are stored in consecutive memory locations. Let's dive into 1D and 2D arrays!

1D Arrays

A 1D (one-dimensional) array is like a simple list of values stored in memory. Example:

// 📂 This File can also be accessed through: https://github.com/BilalAhmadKhanKhattak/CppNotes/blob/main/Arrays/1dArray.cpp

// find the max in 1D array..... o bhai sab!

#include 
using namespace std;

int main() {
    int arr[5] = {23,4,12,6,64};  // This is what we call 1D array
    int maxNum = arr[0]; // for now, I'm gonna set it to 23 (just for now) watch and learn

    // I'll use loop to find the max in the array. Loops through the array and finds the max value
    for (int i = 1; i < 5; i++) {  
        if (arr[i] > maxNum) {
            maxNum = arr[i];
        }
    }
    cout << "Max Number in the array is: " << maxNum << endl;
    return 0;
}

// BILRED ;)
    
Note: This C++ file is located in https://github.com/BilalAhmadKhanKhattak/CppNotes/blob/main/Arrays/1dArray.cpp inside the GitHub repo.

2D Arrays

A **2D (two-dimensional) array** is like a table with **rows** and **columns**. Example:

// 📂 Stored in: https://github.com/BilalAhmadKhanKhattak/CppNotes/blob/main/Arrays/2dArray.cpp

// 2D ARRAYS

// A table with rows & columns.
// Access with two indexes: array[row][column].
// Process using nested loops.

#include 
using namespace std;

int main() {
    // datatype arrayname[row size][column size]
    int matrix[2][3] = { 
    {1,2,3}, 
    {4,5,6}
    };
    // You can type the matrix in single line, but for understanding i formatted it like tha'  ;)

    // Accessing a value in array
    cout << matrix[0][1] << endl;  // prints element at row 0, column 1 (value = 2)
    cout << matrix[1][2] << endl; // prints element at row 1 and [2] means column 2 ( value = 6)

    // printing whole matrix MANUALLY
    cout << matrix[0][0] << " " << matrix[0][1] << " " << matrix[0][2] << endl;
    cout << matrix[1][0] << " " << matrix[1][1] << " " << matrix[1][2] << endl;



    // Alternatively, you can use a for loop to display full array 
    // Loop through rows
    for (int i = 0; i < 2; i++) {  
        // Loop through columns
        for (int j = 0; j < 3; j++) {  
            cout << matrix[i][j] << " ";  // Print element
        }
        cout << endl;  // Move to the next line after a row
    }
}

// Acha suno aur yaad rakho....
// when creating arrays, in array[size], size is a NATURAL number(begins from 1)
// when accessing array, such as array[index], index is a WHOLE number (begins from 0)

// BILRED ;)
    
Note: This file is also stored in https://github.com/BilalAhmadKhanKhattak/CppNotes/blob/main/Arrays/2dArray.cpp inside the repo.

Quick Facts

  • A 1D array is like a list: {10, 20, 30}.
  • A 2D array is like a table:
    10  20  30
    40  50  60
                
  • Array indexing starts from **0**, so the first element is at index **0**.

Keep coding and stay curious!

Bilal Ahmad Khan AKA Mr. BILRED

Disclaimer:

Help provided as a courtesy. Outcomes and decisions are yours to own.

AI has been used to assist in creating this content. If any errors are found, please contact Bilal Ahmad Khan AKA Mr. BILRED ASAP.

GitHub: Some Notes Available

My GitHub: BilalAhmadKhanKhattak

  • AppliedPhysicsNotesByBilred
  • CalculusNotesByBilred
  • CppNotes

What I Think:

"Knowledge Should Be Shared Only With The One Who Knows Its True Worth! Not Everyone Deserves It."
- Bilal Ahmad Khan AKA Mr. BILRED

Comments

Popular posts from this blog

Bits, Bytes, Binary, and ASCII: The Fundamentals of Data Representation - Compiled By Bilal Ahmad Khan AKA Mr. BILRED - TechAmbitionX

C++ escape sequences with clear examples and outputs - Compiled By Bilal Ahmad Khan AKA Mr. BILRED

C++ Prog Lang In A Nutshell VOL #2 Compiled By Bilal Ahmad Khan AKA Mr. BILRED - TechAmbitonX