ScholarQuill logoScholarQuillUniversity Notes
  • Notes
  • Past Papers
  • Blogs
  • Todo
Login
ScholarQuill logoScholarQuillUniversity Notes
Login
NotesPast PapersBlogsTodo
More
SubjectsDiscussionCGPA CalculatorGPA CalculatorStudent PortalCourse Outline
About
About usPrivacy PolicyReportContact
Notes
Past Papers
Blogs
Todo
Analytics
    Current Subject
    🧩
    Programming Fundamentals
    COMP1112
    Progress0 / 19 topics
    Topics
    1. Introduction to Problem Solving2. Von-Neumann Architecture3. Introduction to Programming4. Role of Compiler and Linker5. Introduction to Algorithms6. Basic Data Types and Variables7. Input/Output Constructs8. Arithmetic, Comparison and Logical Operators9. Conditional Statements and Execution Flow10. Repetitive Statements and Execution Flow11. Lists and Memory Organization12. Multi-dimensional Lists13. Introduction to Modular Programming14. Function Definition and Calling15. Stack Rolling and Unrolling16. Strings and String Operations17. Pointers/References18. Static and Dynamic Memory Allocation19. File I/O Operations
    COMP1112›Multi-dimensional Lists
    Programming FundamentalsTopic 12 of 19

    Multi-dimensional Lists

    4 minread
    731words
    Beginnerlevel

    Multi-dimensional Lists in C++

    Multi-dimensional lists, often referred to as multi-dimensional arrays or matrices, allow you to store data in a structured format that can be accessed via multiple indices. This is useful in various applications, such as mathematical computations, graphics, and data representation.

    1. Definition of Multi-dimensional Arrays

    A multi-dimensional array is essentially an array of arrays. The most common type is the two-dimensional array, which can be visualized as a matrix with rows and columns. You can also have three-dimensional arrays and higher dimensions, although they are less common.

    2. Two-Dimensional Arrays

    Declaration: You can declare a two-dimensional array using the following syntax:

    dataType arrayName[rows][columns];
    

    Example:

    #include <iostream>
    using namespace std;
    
    int main() {
        const int rows = 3;
        const int columns = 4;
        int matrix[rows][columns] = {
            {1, 2, 3, 4},
            {5, 6, 7, 8},
            {9, 10, 11, 12}
        };
    
        // Display the matrix
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < columns; j++) {
                cout << matrix[i][j] << " ";
            }
            cout << endl;
        }
    
        return 0;
    }
    

    3. Memory Organization

    • Two-dimensional arrays are stored in row-major order. This means that the elements of each row are stored in contiguous memory locations.
    • The memory layout of a two-dimensional array can be visualized as follows:
    Matrix (3x4):
      1  2  3  4
      5  6  7  8
      9 10 11 12
    

    In memory, this would be stored sequentially as:

    1 2 3 4 5 6 7 8 9 10 11 12
    

    4. Three-Dimensional Arrays

    You can also create three-dimensional arrays, which can be visualized as an array of matrices.

    Declaration:

    dataType arrayName[depth][rows][columns];
    

    Example:

    #include <iostream>
    using namespace std;
    
    int main() {
        const int depth = 2;
        const int rows = 3;
        const int columns = 4;
        int threeD[depth][rows][columns] = {
            {
                {1, 2, 3, 4},
                {5, 6, 7, 8},
                {9, 10, 11, 12}
            },
            {
                {13, 14, 15, 16},
                {17, 18, 19, 20},
                {21, 22, 23, 24}
            }
        };
    
        // Display the 3D array
        for (int d = 0; d < depth; d++) {
            cout << "Layer " << d + 1 << ":" << endl;
            for (int i = 0; i < rows; i++) {
                for (int j = 0; j < columns; j++) {
                    cout << threeD[d][i][j] << " ";
                }
                cout << endl;
            }
            cout << endl;
        }
    
        return 0;
    }
    

    5. Higher-Dimensional Arrays

    Higher-dimensional arrays follow the same principles as two- and three-dimensional arrays, with additional indices for each dimension. However, the complexity increases, and they are less frequently used in practice.

    Example of a Four-Dimensional Array:

    int fourD[2][3][4][5]; // A 4D array
    

    6. Dynamic Multi-dimensional Arrays

    You can also create dynamic multi-dimensional arrays using pointers and new. This approach provides more flexibility, especially when the dimensions are not known at compile time.

    Example:

    #include <iostream>
    using namespace std;
    
    int main() {
        int rows = 3;
        int columns = 4;
    
        // Create a dynamic 2D array
        int** matrix = new int*[rows];
        for (int i = 0; i < rows; i++) {
            matrix[i] = new int[columns];
        }
    
        // Initialize and display the matrix
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < columns; j++) {
                matrix[i][j] = i * columns + j + 1;
                cout << matrix[i][j] << " ";
            }
            cout << endl;
        }
    
        // Cleanup
        for (int i = 0; i < rows; i++) {
            delete[] matrix[i];
        }
        delete[] matrix;
    
        return 0;
    }
    

    Summary

    Multi-dimensional lists (arrays) are powerful tools for organizing data in a structured manner. They allow for efficient storage and access, especially in mathematical and graphical applications. By understanding the declaration, memory organization, and both static and dynamic approaches to multi-dimensional arrays, you can effectively utilize them in your C++ programs.

    Previous topic 11
    Lists and Memory Organization
    Next topic 13
    Introduction to Modular Programming

    Past Papers

    Open this section to load past papers

    Click on Show Past Papers to see past papers.
    On This Page
      Reading Stats
      Est. reading time4 min
      Word count731
      Code examples0
      DifficultyBeginner